diff --git a/README.md b/README.md index c336082..0b86c38 100644 --- a/README.md +++ b/README.md @@ -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... @@ -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 @@ -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 @@ -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 ~~~ @@ -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) diff --git a/lib/cranky/factory.rb b/lib/cranky/factory.rb index 49dec89..4c6cd2c 100644 --- a/lib/cranky/factory.rb +++ b/lib/cranky/factory.rb @@ -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) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 5679db6..551ac03 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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 = "fred@home.com" - 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) { "fred@home.com" } + u.address = fetch(:address) { Factory.build(:address) } u.required_attr = true u end