Skip to content

Commit

Permalink
NodeMixin#update now has strict mode which removes properties not men…
Browse files Browse the repository at this point in the history
…tioned in hash
  • Loading branch information
Martin Kleppmann committed Jun 17, 2009
1 parent 9bc7cb8 commit 1617b57
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
13 changes: 11 additions & 2 deletions lib/neo4j/mixins/node.rb
Expand Up @@ -182,6 +182,8 @@ def value_object

#
# Updates this node's properties by using the provided struct/hash.
# If strict is true, any properties present on the node but not present in
# the hash will be removed from the node.
#
# ==== Parameters
# struct_or_hash<#each_pair>:: the key and value to be set
Expand All @@ -190,12 +192,19 @@ def value_object
# self
#
# :api: public
def update(struct_or_hash)
def update(struct_or_hash, strict=false)
keys_to_delete = props.keys - %w(id classname) if strict
struct_or_hash.each_pair do |key, value|
next if %w(id classname).include? key.to_s # do not allow special properties to be mass assigned
keys_to_delete.delete(key) if strict
method = "#{key}=".to_sym
self.send(method, value) if self.respond_to?(method)
if self.respond_to?(method)
self.send(method, value)
else
set_property(key.to_s, value)
end
end
keys_to_delete.each{|key| remove_property(key) } if strict
self
end

Expand Down
1 change: 1 addition & 0 deletions test/neo4j/node_mixin_spec.rb
Expand Up @@ -157,6 +157,7 @@ class TestNode
t.update({:name=>'123', :oj=>'hoj'})
t.name.should == '123'
t.age.should == nil
t['oj'].should == 'hoj'
end

it "should be able to update a node by using a hash" do
Expand Down

0 comments on commit 1617b57

Please sign in to comment.