Skip to content

Commit

Permalink
Merge pull request ginty#8 from beorc/fetch
Browse files Browse the repository at this point in the history
Add fetch method to factory
  • Loading branch information
ginty authored Nov 6, 2016
2 parents 7c8d54d + 2721c5a commit 42d72cd
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 29 deletions.
51 changes: 27 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ The only rules are:

1. Your factory must reopen the Cranky::Factory class
2. Your factory method must return the object you wanted to create (or an array containing a collection of them)
3. You can access the overrides passed in via options[:key]. (not really a rule!)
3. You can access the overrides passed in via `options[:key]` or `fetch(:key)`. (not really a rule!)

So for example to create a simple user factory...

Expand All @@ -120,11 +120,14 @@ class Cranky::Factory

# Simple factory method to create a user instance, you would call this via crank(:user)
def user
u = User.new
u.name = options[:name] || "Jimmy" # Use the passed in name if present, or the default
u.email = options[:email] || "jimmy#{n}@home.com" # Give each user a unique email address
u.role = options[:role] || "pleb"
u
User.new do |u|
u.name = options[:name] || "Jimmy" # Use the passed in name if present, or the default
u.nickname = fetch(:nickname, 'Silencer') # Use the passed in nickname if present, or the default
u.phone = fetch(:phone, 'phoneless') # Use the passed in phone if present, or the default

u.email = fetch(:email) { "jimmy#{n}@home.com" } # Give each user a unique email address
u.role = fetch(:role, 'pleb')
end
end

end
Expand All @@ -144,12 +147,12 @@ class Cranky::Factory
end

def user
u = User.new
u.name = options[:name] || "Jimmy"
u.email = options[:email] || "jimmy#{n}@home.com"
u.role = options[:role] || "pleb"
u.address = default_address
u
User.new do |u|
u.name = fetch(:name, "Jimmy")
u.email = fetch(:email) { "jimmy#{n}@home.com" }
u.role = fetch(:role, "pleb")
u.address = fetch(:address) { default_address }
end
end

# Create the address factory in the same way
Expand All @@ -175,12 +178,12 @@ You can pass additional arguments to your factories via the overrides hash...
crank(:user, :new_address => true)

def user
u = User.new
u.name = options[:name] || "Jimmy"
u.email = options[:email] || "jimmy#{n}@home.com"
u.role = options[:role] || "pleb"
u.address = options[:new_address] ? create(:address) : default_address
u
User.new do |u|
u.name = fetch(:name, "Jimmy")
u.email = fetch(:email) { "jimmy#{n}@home.com" }
u.role = fetch(:role, "pleb")
u.address = options[:new_address] ? create(:address) : default_address
end
end
~~~

Expand All @@ -190,12 +193,12 @@ You can use traits...
crank(:user, traits: [:admin, :manager])

def user
u = User.new
u.name = options[:name] || "Jimmy"
u.email = options[:email] || "jimmy#{n}@home.com"
u.roles = options[:roles] || []
u.address = options[:new_address] ? create(:address) : default_address
u
User.new do |u|
u.name = fetch(:name, "Jimmy")
u.email = fetch(:email) { "jimmy#{n}@home.com" }
u.role = fetch(:role, "pleb")
u.address = options[:new_address] ? create(:address) : default_address
end
end

def apply_trait_admin_to_user(user)
Expand Down
8 changes: 8 additions & 0 deletions lib/cranky/factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ def traits_for(factory_name)
trait_methods.map {|m| regexp.match(m)[1] }
end

def fetch(*args)
if block_given?
options.fetch(*args, &Proc.new)
else
options.fetch(*args)
end
end

private

def apply_traits(what, item)
Expand Down
10 changes: 5 additions & 5 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ class Cranky::Factory

def user_manually
u = User.new
u.name = "Fred"
u.role = options[:role] || :user
u.unique = "value#{n}"
u.email = "[email protected]"
u.address = Factory.build(:address)
u.name = fetch(:name, 'Fred')
u.role = fetch(:role) { :user }
u.unique = fetch(:unique) { "value#{n}" }
u.email = fetch(:email) { "[email protected]" }
u.address = fetch(:address) { Factory.build(:address) }
u.required_attr = true
u
end
Expand Down

0 comments on commit 42d72cd

Please sign in to comment.