Skip to content

Commit

Permalink
Allow passing of block to Document#initialize
Browse files Browse the repository at this point in the history
  • Loading branch information
durran committed Dec 19, 2009
1 parent 0bac9da commit ae5d839
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/mongoid/document.rb
Expand Up @@ -181,7 +181,8 @@ def initialize(attrs = {})
@attributes = {}.with_indifferent_access
process(defaults.merge(attrs))
@new_record = true if id.nil?
generate_key
document = yield self if block_given?
generate_key; document
end

# Returns the class name plus its attributes.
Expand Down
13 changes: 13 additions & 0 deletions spec/unit/mongoid/document_spec.rb
Expand Up @@ -338,6 +338,19 @@

describe "#new" do

context "when passed a block" do

it "yields self to the block" do
person = Person.new do |p|
p.title = "Sir"
p.age = 60
end
person.title.should == "Sir"
person.age.should == 60
end

end

context "with no attributes" do

it "sets default attributes" do
Expand Down

0 comments on commit ae5d839

Please sign in to comment.