From 1617b571f094d8120171d80b625d6c9d236ee6c2 Mon Sep 17 00:00:00 2001 From: Martin Kleppmann Date: Wed, 17 Jun 2009 21:08:18 +0100 Subject: [PATCH] NodeMixin#update now has strict mode which removes properties not mentioned in hash --- lib/neo4j/mixins/node.rb | 13 +++++++++++-- test/neo4j/node_mixin_spec.rb | 1 + 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/neo4j/mixins/node.rb b/lib/neo4j/mixins/node.rb index 866d72327..01d6a29cc 100644 --- a/lib/neo4j/mixins/node.rb +++ b/lib/neo4j/mixins/node.rb @@ -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 @@ -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 diff --git a/test/neo4j/node_mixin_spec.rb b/test/neo4j/node_mixin_spec.rb index 999c2e384..91eef8fbd 100644 --- a/test/neo4j/node_mixin_spec.rb +++ b/test/neo4j/node_mixin_spec.rb @@ -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