Module: Neo4jAncestry::NodeInstanceMethods

Defined in:
lib/models/neo4j_ancestry/node_instance_methods.rb

Instance Method Summary collapse

Instance Method Details

#ancestorsObject



37
38
39
40
41
42
# 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

#childrenObject



31
32
33
34
35
36
# 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

#descendantsObject



43
44
45
46
47
48
# 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

This method returns all ActiveRecord objects found by a cypher neo4j query defined through the given query_string.

Within the query_string, no START expression is needed, because the start node is given by the neo_node of this structureable object. It is referred to just by ‘self’.

Example:

group.find_related_nodes_via_cypher("
  match (self)-[:is_parent_of]->(children)
  return children
")  # =>  [child_group1, child_group2, ...]


69
70
71
72
73
74
75
76
77
# File 'lib/models/neo4j_ancestry/node_instance_methods.rb', line 69

def find_related_nodes_via_cypher(query_string)
  query_string = "
    start self=node(#{neo_id})
    #{query_string}
  "
  cypher_results_to_objects(
    Neoid.db.execute_query(query_string)
  )
end

#neo_idObject

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



19
20
21
# File 'lib/models/neo4j_ancestry/node_instance_methods.rb', line 19

def neo_id
  neo_node.try(:neo_id)
end

#neo_nodeObject

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



13
14
15
# File 'lib/models/neo4j_ancestry/node_instance_methods.rb', line 13

def neo_node
  super || self.becomes(self.class.base_class).neo_node
end

#parentsObject

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



25
26
27
28
29
30
# 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

#siblingsObject



49
50
51
52
53
54
# 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