Skip to content

Commit

Permalink
Allow overwriting attributes with an additional hash.
Browse files Browse the repository at this point in the history
  • Loading branch information
phiggins committed Oct 17, 2010
1 parent 3e63ed0 commit 08b4c7b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib/mini_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ def self.define name, opts={}, &block
factories[name] = new(name, opts, block)
end

def self.create name
def self.create name, opts={}
name = name.to_s.downcase.to_sym
factory = factories[name]
factory.create
factory.create(opts)
end

def initialize name, opts={}, block
Expand All @@ -24,10 +24,11 @@ def initialize name, opts={}, block
@model = opts[:class] || model
end

def create
def create opts
real_object = @model.new
proxy = Proxy.new(real_object)
@block.call(proxy)
opts.each {|k,v| proxy.send(k,v) }
real_object.save
end

Expand All @@ -53,5 +54,5 @@ def method_missing method, *args, &block
end

def MiniFactory(name, attrs={})
MiniFactory.create(name)
MiniFactory.create(name, attrs)
end
9 changes: 9 additions & 0 deletions spec/mini_factory_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ class User < Sequel::Model ; end
end

describe ".define" do
it "should allow over-writing the default values as extra options" do
MiniFactory.define :user do |u|
u.first_name "Frank"
u.last_name "Rizzo"
end

MiniFactory(:user, :last_name => "Sinatra").last_name.must_equal "Sinatra"
end

it "should allow lazy attributes with a block" do
email = "user.email@example.com"

Expand Down

0 comments on commit 08b4c7b

Please sign in to comment.