Module: Neo4j::Core::Rels
- Included in:
- Node
- Defined in:
- lib/neo4j-core/rels/rels.rb,
lib/neo4j-core/rels/traverser.rb
Overview
Contains methods for traversing relationship object of depth one from one node.
Defined Under Namespace
Classes: Traverser
Instance Method Summary collapse
-
#_node(dir, type) ⇒ Object
Same as #node but instead returns an unwrapped native java node.
-
#_nodes(dir, *types) ⇒ Enumerable<Neo4j::Node>
Works like #rels method but instead returns the nodes.
-
#_rel(dir, type) ⇒ Neo4j::Relationship?
Same as rel but does not return a ruby wrapped object but instead returns the Java object.
-
#_rels(dir = :both, *types) ⇒ Enumerable
Finds relationship starting from this node given a direction and/or relationship type(s).
-
#node(dir, type) ⇒ Object
This method reflects that semantics and returns either: * nil if there are zero relationships of the given type and direction, * the relationship if there’s exactly one, or * throws an unchecked exception in all other cases.
-
#nodes(dir, *types) ⇒ Enumerable
Works like #rels method but instead returns the nodes.
-
#rel(dir, type) ⇒ Neo4j::Relationship, ...
Returns the only relationship of a given type and direction that is attached to this node, or null.
-
#rel?(dir = :both, type = nil) ⇒ Boolean
Check if the given relationship exists Returns true if there are one or more relationships from this node to other nodes with the given relationship.
-
#rels(dir = :both, *types) ⇒ Neo4j::Core::Rels::Traverser
Returns an enumeration of relationship objects using the builder DSL pattern.
Instance Method Details
#_node(dir, type) ⇒ Object
Same as #node but instead returns an unwrapped native java node.
28 29 30 31 |
# File 'lib/neo4j-core/rels/rels.rb', line 28 def _node(dir, type) r = _rel(dir, type) r && r._other_node(self._java_node) end |
#_nodes(dir, *types) ⇒ Enumerable<Neo4j::Node>
Works like #rels method but instead returns the nodes.
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/neo4j-core/rels/rels.rb', line 37 def _nodes(dir, *types) r = _rels(dir, *types) case dir when :outgoing then Neo4j::Core::LazyMap.new(r, &:_end_node) when :incoming then Neo4j::Core::LazyMap.new(r, &:_start_node) when :both then Neo4j::Core::LazyMap.new(r) { |x| x._other_node(self) } end end |
#_rel(dir, type) ⇒ Neo4j::Relationship?
Same as rel but does not return a ruby wrapped object but instead returns the Java object.
112 113 114 |
# File 'lib/neo4j-core/rels/rels.rb', line 112 def _rel(dir, type) get_single_relationship(ToJava.type_to_java(type), ToJava.dir_to_java(dir)) end |
#_rels(dir = :both, *types) ⇒ Enumerable
Finds relationship starting from this node given a direction and/or relationship type(s).
119 120 121 122 123 124 125 126 127 |
# File 'lib/neo4j-core/rels/rels.rb', line 119 def _rels(dir=:both, *types) if types.size > 1 get_relationships(ToJava.dir_to_java(dir), ToJava.types_to_java(types)).iterator elsif types.size == 1 get_relationships(ToJava.type_to_java(types[0]), ToJava.dir_to_java(dir)).iterator else get_relationships(ToJava.dir_to_java(dir)).iterator end end |
#node(dir, type) ⇒ Object
This method reflects that semantics and returns either:
-
nil if there are zero relationships of the given type and direction,
-
the relationship if there’s exactly one, or
-
throws an unchecked exception in all other cases.
This method should be used only in situations with an invariant as described above. In those situations, a “state-checking” method (e.g. #rel?) is not required, because this method behaves correctly “out of the box.”
21 22 23 24 |
# File 'lib/neo4j-core/rels/rels.rb', line 21 def node(dir, type) n = _node(dir, type) n && n.wrapper end |
#nodes(dir, *types) ⇒ Enumerable
Works like #rels method but instead returns the nodes. It does try to load a Ruby wrapper around each node
55 56 57 |
# File 'lib/neo4j-core/rels/rels.rb', line 55 def nodes(dir, *types) Neo4j::Core::LazyMap.new(_nodes(dir, *types), &:wrapper) end |
#rel(dir, type) ⇒ Neo4j::Relationship, ...
Returns the only relationship of a given type and direction that is attached to this node, or null. This is a convenience method that is used in the commonly occuring situation where a node has exactly zero or one relationships of a given type and direction to another node. Typically this invariant is maintained by the rest of the code: if at any time more than one such relationships exist, it is a fatal error that should generate an unchecked exception. This method reflects that semantics and returns either:
-
nil if there are zero relationships of the given type and direction,
-
the relationship if there’s exactly one, or
-
raise an exception in all other cases.
103 104 105 106 |
# File 'lib/neo4j-core/rels/rels.rb', line 103 def rel(dir, type) result = _rel(dir, type) result && result.wrapper end |
#rel?(dir = :both, type = nil) ⇒ Boolean
Check if the given relationship exists Returns true if there are one or more relationships from this node to other nodes with the given relationship.
137 138 139 140 141 142 143 |
# File 'lib/neo4j-core/rels/rels.rb', line 137 def rel?(dir=:both, type=nil) if type has_relationship(ToJava.type_to_java(type), ToJava.dir_to_java(dir)) else has_relationship(ToJava.dir_to_java(dir)) end end |
#rels(dir = :both, *types) ⇒ Neo4j::Core::Rels::Traverser
Returns an enumeration of relationship objects using the builder DSL pattern. It always returns relationships of depth one.
82 83 84 85 |
# File 'lib/neo4j-core/rels/rels.rb', line 82 def rels(dir=:both, *types) raise "Illegal argument, first argument must be :both, :incoming or :outgoing, got #{dir.inspect}" unless [:incoming, :outgoing, :both].include?(dir) Neo4j::Core::Rels::Traverser.new(self, types, dir) end |