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

Instance Method Details

#_node(dir, type) ⇒ Object

Same as #node but instead returns an unwrapped native java node.

Parameters:

  • dir (:both, :incoming, :outgoing)

    the direction of the relationship

  • type (Symbol, String)

    the type of relationship, see Neo4j::Core::Relationship#rel_type



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.

Parameters:

  • dir (:both, :incoming, :outgoing)

    the direction of the relationship

  • types (String, Symbol)

    the requested relationship types we want look for, if none it gets relationships of any type

Returns:

See Also:



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.

Parameters:

  • dir (:both, :incoming, :outgoing)

    the direction of the relationship

  • type (Symbol, String)

    the type of relationship, see Neo4j::Core::Relationship#rel_type

Returns:

See Also:



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).

Parameters:

  • dir (:both, :incoming, :outgoing) (defaults to: :both)

    the direction of the relationship

  • types (String, Symbol)

    the requested relationship types we want look for, if none it gets relationships of any type

Returns:

  • (Enumerable)

    of Neo4j::Relationship objects



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.”

Parameters:

  • dir (:both, :incoming, :outgoing)

    the direction of the relationship

  • type (Symbol, String)

    the type of relationship, see Neo4j::Core::Relationship#rel_type

See Also:

  • #wrapper - The method used to wrap the node in a Ruby object if the node was found


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

Parameters:

  • dir (:both, :incoming, :outgoing)

    the direction of the relationship

  • types (String, Symbol)

    the requested relationship types we want look for, if none it gets relationships of any type

Returns:

  • (Enumerable)

    an Enumeration of either Neo4j::Node objects or wrapped Neo4j::Node objects

See Also:

  • #wrapper - The method used wrap to the node in a Ruby object if the node was found


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.

Parameters:

  • dir (:both, :incoming, :outgoing)

    the direction of the relationship

  • type (Symbol, String)

    the type of relationship, see Neo4j::Core::Relationship#rel_type

Returns:

  • (Neo4j::Relationship, nil, Object)

    the Relationship or wrapper for the Relationship or nil

See Also:



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.

Parameters:

  • dir (:both, :incoming, :outgoing) (defaults to: :both)

    optional default :both (either, :outgoing, :incoming, :both)

  • type (String, Symbol) (defaults to: nil)

    the key and value to be set, default any type

Returns:

  • (Boolean)

    true if one or more relationships exists for the given type and dir otherwise false



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.

Examples:

Return both incoming and outgoing relationships

me.rels(:both, :friends, :work).each {|relationship|...}

Only return outgoing relationship of given type

me.rels(:outgoing, :friends).first.end_node # => my friend node

All the relationships between me and another node of given dir & type

me.rels(:outgoing, :friends).to_other(node)

Delete all relationships between me and another node of given dir & type

me.rels(:outgoing, :friends).to_other(node).del

Parameters:

  • dir (:both, :incoming, :outgoing) (defaults to: :both)

    the direction of the relationship

  • types (String, Symbol)

    the requested relationship types we want look for, if none it gets relationships of any type

Returns:

Raises:

  • an exception if the first parameter is not :both, ;outgoing or :incoming

See Also:

  • #wrapper - The method used wrap to the node in a Ruby object if the node was found
  • Relationship#rel_type


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