From bd65e78d32a08ca7ade476e4e4c9f5924eeb3783 Mon Sep 17 00:00:00 2001 From: Roel van Dijk Date: Mon, 1 Oct 2012 20:31:59 +0200 Subject: [PATCH] Spec #attributes. --- lib/neography/property.rb | 18 ++++++++++++------ lib/neography/relationship.rb | 5 ----- spec/unit/properties_spec.rb | 14 ++++++++++++++ spec/unit/relationship_spec.rb | 11 +---------- 4 files changed, 27 insertions(+), 21 deletions(-) diff --git a/lib/neography/property.rb b/lib/neography/property.rb index 41f4443..69cb24b 100644 --- a/lib/neography/property.rb +++ b/lib/neography/property.rb @@ -2,25 +2,27 @@ module Neography module Property def [](key) + key = key.to_sym return unless respond_to?(key) @table[key] end def []=(key, value) - k = key.to_s + key = key.to_sym + k_str = key.to_s if value.nil? if self.is_a? Neography::Node - neo_server.remove_node_properties(self.neo_id, [k]) + neo_server.remove_node_properties(self.neo_id, [k_str]) else - neo_server.remove_relationship_properties(self.neo_id, [k]) + neo_server.remove_relationship_properties(self.neo_id, [k_str]) end else if self.is_a? Neography::Node - neo_server.set_node_properties(self.neo_id, {k => value}) + neo_server.set_node_properties(self.neo_id, {k_str => value}) else - neo_server.set_relationship_properties(self.neo_id, {k => value}) + neo_server.set_relationship_properties(self.neo_id, {k_str => value}) end - new_ostruct_member(k) unless self.respond_to?(key) + new_ostruct_member(key) unless self.respond_to?(key) end @table[key] = value end @@ -50,5 +52,9 @@ def method_missing(method_sym, *arguments, &block) end end + def attributes + @table.keys + end + end end diff --git a/lib/neography/relationship.rb b/lib/neography/relationship.rb index 64e704f..df4d9b6 100644 --- a/lib/neography/relationship.rb +++ b/lib/neography/relationship.rb @@ -58,11 +58,6 @@ def exist? end end - def attributes - attrs = self.methods - OpenStruct.instance_methods - Neography::Relationship.instance_methods - attrs.values_at(*attrs.each_index.select {|i| i.even?}) - end - def other_node(node) if node == @start_node @end_node diff --git a/spec/unit/properties_spec.rb b/spec/unit/properties_spec.rb index 66e3f05..ae5af1d 100644 --- a/spec/unit/properties_spec.rb +++ b/spec/unit/properties_spec.rb @@ -60,6 +60,13 @@ module Neography node.key = "value2" end + it "knows its attributes" do + @db.stub(:"set_node_properties") + node.key = "value" + node["key2"] = "value" + node.attributes.should =~ [ :key, :key2 ] + end + end context "Relationship" do @@ -117,6 +124,13 @@ module Neography relationship.key = "value2" end + it "knows its attributes" do + @db.stub(:"set_relationship_properties") + relationship.key = "value" + relationship["key2"] = "value" + relationship.attributes.should =~ [ :key, :key2 ] + end + end end end diff --git a/spec/unit/relationship_spec.rb b/spec/unit/relationship_spec.rb index 91a4630..c140d6a 100644 --- a/spec/unit/relationship_spec.rb +++ b/spec/unit/relationship_spec.rb @@ -83,17 +83,8 @@ module Neography describe "#other_node" do - let(:rel) do - { - "self" => "/1", - "start" => "/2", - "end" => "/3", - "data" => {} - } - end - before do - db.stub(:create_relationship) { rel } + db.stub(:create_relationship) { relationship_hash } end subject(:relationship) { Relationship.create("type", from, to, props) }