diff --git a/lib/neography/index.rb b/lib/neography/index.rb index 6c73532..94ca153 100644 --- a/lib/neography/index.rb +++ b/lib/neography/index.rb @@ -7,34 +7,43 @@ def self.included(base) def add_to_index(index, key, value) if self.is_a? Neography::Node - neo_server.add_node_to_index(index, key, value, self.neo_id) + self.neo_server.add_node_to_index(index, key, value, self.neo_id) else - neo_server.add_relationship_to_index(index, key, value, self.neo_id) + self.neo_server.add_relationship_to_index(index, key, value, self.neo_id) end end def remove_from_index(*args) if self.is_a? Neography::Node - neo_server.remove_node_from_index(*args) + self.neo_server.remove_node_from_index(*args) else - neo_server.remove_relationship_from_index(*args) + self.neo_server.remove_relationship_from_index(*args) end end module ClassMethods def find(*args) - if name == "Neography::Node" - if args.size > 1 - neo_server.find_node_index(*args) - else - neo_server.get_node_index(*args) + db = args[3] ? args.pop : Neography::Rest.new + + if self.inspect == "Neography::Node" + nodes = [] + results = args.size > 1 ? db.find_node_index(*args) : db.get_node_index(*args) + return nil unless results + results.each do |r| + node = self.new(r) + node.neo_server = db + nodes << node end + nodes.size > 1 ? nodes : nodes.first else - if args.size > 1 - neo_server.find_relationship_index(*args) - else - neo_server.get_relationship_index(*args) + rels = [] + results = args.size > 1 ? db.find_relationship_index(*args) : db.get_relationship_index(*args) + return nil unless results + results.each do |r| + rel = self.new(r, db) + rels << rel end + rels.size > 1 ? rels : rels.first end end end diff --git a/lib/neography/node.rb b/lib/neography/node.rb index 29592a2..4840654 100644 --- a/lib/neography/node.rb +++ b/lib/neography/node.rb @@ -29,6 +29,11 @@ def load(node, db = Neography::Rest.new) end #alias_method :new, :create + end + + def find(*args) + node = self.new + node.find(args) end def del diff --git a/spec/integration/index_spec.rb b/spec/integration/index_spec.rb index 7b1e96e..c751c90 100644 --- a/spec/integration/index_spec.rb +++ b/spec/integration/index_spec.rb @@ -18,4 +18,38 @@ r.add_to_index("relationship_test_index", key, value) end + it "can find a node in an index" do + value = generate_text + new_node = Neography::Node.create("name" => value) + new_node.add_to_index("node_test_index", "name", value) + existing_node = Neography::Node.find("node_test_index", "name", value) + existing_node.name.should == value + end + + it "can find a relationship in an index" do + value = generate_text + node1 = Neography::Node.create + node2 = Neography::Node.create + r = Neography::Relationship.create(:friends, node1, node2, {"name" => value}) + r.add_to_index("relationship_test_index", "name", value) + existing_r = Neography::Relationship.find("relationship_test_index", "name", value) + existing_r.name.should == value + end + + it "can find multiple nodes in an index" do + value1 = generate_text + value2 = generate_text + value3 = generate_text + node1 = Neography::Node.create("first_name" => value1, "last_name" => value2) + node1.add_to_index("node_test_index", "first_name", value1) + node2 = Neography::Node.create("first_name" => value1, "last_name" => value3) + node2.add_to_index("node_test_index", "first_name", value1) + + existing_nodes = Neography::Node.find("node_test_index", "first_name", value1) + existing_nodes.size.should == 2 + existing_nodes.first.last_name.should == value2 + existing_nodes.last.last_name.should == value3 + end + + end