Skip to content

Commit

Permalink
allowing Node to be loaded from Cypher
Browse files Browse the repository at this point in the history
  • Loading branch information
maxdemarzi committed Jun 16, 2013
1 parent b921317 commit da3081a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/neography/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 16 additions & 4 deletions lib/neography/property_container.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 13 additions & 0 deletions spec/integration/node_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/node_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit da3081a

Please sign in to comment.