Skip to content

Commit

Permalink
yield newly spawned object to block if given
Browse files Browse the repository at this point in the history
  • Loading branch information
dchelimsky committed Sep 11, 2008
1 parent 82f165b commit e890848
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/object_daddy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ module ClassMethods
def spawn(args = {})
gather_exemplars
generate_values(args)
new(args)
instance = new(args)
yield instance if block_given?
instance
end

# register a generator for an attribute of this class
Expand Down
8 changes: 8 additions & 0 deletions spec/object_daddy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,14 @@
@class.stubs(:name).returns('Widget')
end

it "should yield the instance to a block if given" do
yielded_object = nil
@class.spawn do |obj|
yielded_object = obj
end
@class.should === yielded_object
end

it "should register exemplars for the target class" do
@class.expects(:gather_exemplars)
@class.spawn
Expand Down

0 comments on commit e890848

Please sign in to comment.