Skip to content

Commit

Permalink
Refactoring Hash#insert
Browse files Browse the repository at this point in the history
  • Loading branch information
durran committed Dec 9, 2009
1 parent cef1a60 commit c235672
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 9 deletions.
12 changes: 4 additions & 8 deletions lib/mongoid/extensions/hash/accessors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,11 @@ module Extensions #:nodoc:
module Hash #:nodoc:
module Accessors #:nodoc:
def insert(key, attrs)
if key.singular?
self[key] = attrs unless self[key]
self[key] = self[key].merge(attrs) if self[key]
elements = self[key]
if elements
elements.update(attrs)
else
if elements = self[key]
elements.update(attrs)
else
self[key] = [attrs]
end
self[key] = key.singular? ? attrs : [attrs]
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/mongoid/extensions/hash/accessors_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
@new = { :_id => 2, :first_name => "Test2", :last_name => "User2" }
end

it "updates the existing attribute" do
it "adds the new attribute" do
@hash.insert(:name, @new)
@hash[:name].should == @new
end
Expand Down

0 comments on commit c235672

Please sign in to comment.