module Neo4jAncestry::NodeInstanceMethods

Public Instance Methods

ancestors() click to toggle source
# File lib/models/neo4j_ancestry/node_instance_methods.rb, line 37
def ancestors
  find_related_nodes_via_cypher("
    match (ancestors)-[:is_parent_of*1..100]->(self)
    return ancestors
  ").uniq
end
children() click to toggle source
# File lib/models/neo4j_ancestry/node_instance_methods.rb, line 31
def children
  find_related_nodes_via_cypher("
    match (self)-[:is_parent_of]->(children)
    return children
  ")
end
descendants() click to toggle source
# File lib/models/neo4j_ancestry/node_instance_methods.rb, line 43
def descendants
  find_related_nodes_via_cypher("
    match (self)-[:is_parent_of*1..100]->(descendants)
    return descendants
  ").uniq
end
neo_id() click to toggle source

The unique id of the neo4j node that corresponds to this object.

# File lib/models/neo4j_ancestry/node_instance_methods.rb, line 19
def neo_id
  neo_node.try(:neo_id)
end
neo_node() click to toggle source

The neoid gem provides a neo_node method, which returns an object representing the node in the neo4j database that corresponds to this object.

Overriding the neo_node method ensures that for STI the same neo_node is returned for the same object regardless of the subclass.

That means: group.neo_node == group.becomes(SpecialGroup).neo_node

Calls superclass method
# File lib/models/neo4j_ancestry/node_instance_methods.rb, line 13
def neo_node
  super || self.becomes(self.class.base_class).neo_node
end
parents() click to toggle source

Methods to query the neo4j database for parents, children, ancestors, descendants and siblings.

# File lib/models/neo4j_ancestry/node_instance_methods.rb, line 25
def parents
  find_related_nodes_via_cypher("
    match (parents)-[:is_parent_of]->(self)
    return parents
  ")
end
siblings() click to toggle source
# File lib/models/neo4j_ancestry/node_instance_methods.rb, line 49
def siblings
  find_related_nodes_via_cypher("
    match (self)<-[:is_parent_of]-(parent)-[:is_parent_of]->(siblings)
    return siblings
  ").uniq
end

Private Instance Methods

cypher_results_to_objects(cypher_results) click to toggle source

This method returns the ActiveRecord objects that match the given cypher query result.

For an example, have a look at the method

find_related_nodes_via_cypher.
# File lib/models/neo4j_ancestry/node_instance_methods.rb, line 85
def cypher_results_to_objects(cypher_results)
  cypher_results["data"].collect do |result|
    result.first["data"]["ar_type"].constantize.find(result.first["data"]["ar_id"])
  end
end