Module: Neo4jAncestry::NodeInstanceMethods
- Defined in:
- lib/models/neo4j_ancestry/node_instance_methods.rb
Instance Method Summary collapse
- #ancestors ⇒ Object
- #children ⇒ Object
- #descendants ⇒ Object
-
#find_related_nodes_via_cypher(query_string) ⇒ Object
This method returns all ActiveRecord objects found by a cypher neo4j query defined through the given query_string.
-
#neo_id ⇒ Object
The unique id of the neo4j node that corresponds to this object.
-
#neo_node ⇒ Object
The neoid gem provides a neo_node method, which returns an object representing the node in the neo4j database that corresponds to this object.
-
#parents ⇒ Object
Methods to query the neo4j database for parents, children, ancestors, descendants and siblings.
- #siblings ⇒ Object
Instance Method Details
#ancestors ⇒ Object
37 38 39 40 41 42 |
# File 'lib/models/neo4j_ancestry/node_instance_methods.rb', line 37 def ancestors (" match (ancestors)-[:is_parent_of*1..100]->(self) return ancestors ").uniq end |
#children ⇒ Object
31 32 33 34 35 36 |
# File 'lib/models/neo4j_ancestry/node_instance_methods.rb', line 31 def children (" match (self)-[:is_parent_of]->(children) return children ") end |
#descendants ⇒ Object
43 44 45 46 47 48 |
# File 'lib/models/neo4j_ancestry/node_instance_methods.rb', line 43 def descendants (" match (self)-[:is_parent_of*1..100]->(descendants) return descendants ").uniq end |
#find_related_nodes_via_cypher(query_string) ⇒ Object
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.("
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 (query_string) query_string = " start self=node(#{neo_id}) #{query_string} " cypher_results_to_objects( Neoid.db.execute_query(query_string) ) end |
#neo_id ⇒ Object
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_node ⇒ Object
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 |
#parents ⇒ Object
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 (" match (parents)-[:is_parent_of]->(self) return parents ") end |
#siblings ⇒ Object
49 50 51 52 53 54 |
# File 'lib/models/neo4j_ancestry/node_instance_methods.rb', line 49 def siblings (" match (self)<-[:is_parent_of]-(parent)-[:is_parent_of]->(siblings) return siblings ").uniq end |