From da3081afa210b469795fd12592098556c979ba86 Mon Sep 17 00:00:00 2001 From: Max De Marzi Date: Sun, 16 Jun 2013 18:51:02 -0500 Subject: [PATCH] allowing Node to be loaded from Cypher --- lib/neography/node.rb | 2 +- lib/neography/property_container.rb | 20 ++++++++++++++++---- spec/integration/node_spec.rb | 13 +++++++++++++ spec/unit/node_spec.rb | 2 +- 4 files changed, 31 insertions(+), 6 deletions(-) diff --git a/lib/neography/node.rb b/lib/neography/node.rb index 4840654..8e191bb 100644 --- a/lib/neography/node.rb +++ b/lib/neography/node.rb @@ -20,7 +20,7 @@ def create(props = nil, db = Neography::Rest.new) def load(node, db = Neography::Rest.new) raise ArgumentError.new("syntax deprecated") if node.is_a?(Neography::Rest) - node = db.get_node(node) + node = db.get_node(node) if (node.to_s.match(/^\d+$/) or node.to_s.split("/").last.match(/^\d+$/)) if node node = self.new(node) node.neo_server = db diff --git a/lib/neography/property_container.rb b/lib/neography/property_container.rb index 2ab0dbc..054955d 100644 --- a/lib/neography/property_container.rb +++ b/lib/neography/property_container.rb @@ -5,11 +5,23 @@ class PropertyContainer < OpenStruct def initialize(hash=nil) @table = {} if hash - @neo_id = hash["self"].split('/').last - for k,v in hash["data"] - @table[k.to_sym] = v - new_ostruct_member(k) + if hash["self"] # coming from REST API + @neo_id = hash["self"].split('/').last + data = hash["data"] + elsif hash.is_a? Neography::Node # is already a Neography::Node + @neo_id = hash.neo_id + data = Hash[*hash.attributes.collect{|x| [x.to_sym, hash.send(x)]}.flatten] + elsif hash["data"] # coming from CYPHER + @neo_id = hash["data"].first.first["self"].split('/').last + data = hash["data"].first.first["data"] end + else + data = [] + end + + for k,v in data + @table[k.to_sym] = v + new_ostruct_member(k) end end diff --git a/spec/integration/node_spec.rb b/spec/integration/node_spec.rb index 6937e3e..4cd4b31 100644 --- a/spec/integration/node_spec.rb +++ b/spec/integration/node_spec.rb @@ -68,6 +68,19 @@ existing_node = Neography::Node.load(@neo, new_node) }.to raise_error(ArgumentError) end + + it "can get a node that exists via cypher" do + new_node = Neography::Node.create("age" => 31, "name" => "Max") + cypher = "START n = node({id}) return n" + @neo = Neography::Rest.new + results = @neo.execute_query(cypher, {:id => new_node.neo_id.to_i}) + existing_node = Neography::Node.load(results) + existing_node.should_not be_nil + existing_node.neo_id.should_not be_nil + existing_node.neo_id.should == new_node.neo_id + end + + end describe "del" do diff --git a/spec/unit/node_spec.rb b/spec/unit/node_spec.rb index e52e575..47fb128 100644 --- a/spec/unit/node_spec.rb +++ b/spec/unit/node_spec.rb @@ -66,7 +66,7 @@ module Neography it "loads by node" do node = Node.new - @db.should_receive(:get_node).with(node) + @db.should_not_receive(:get_node).with(node) Node.load(node) end