Module: Neo4j::Rails::Relationships

Extended by:
ActiveSupport::Concern
Included in:
Model
Defined in:
lib/neo4j/rails/relationships/relationships.rb,
lib/neo4j/rails/relationships/storage.rb,
lib/neo4j/rails/relationships/rels_dsl.rb,
lib/neo4j/rails/relationships/node_dsl.rb

Overview

This module overrides the Neo4j::Core::Rels and Neo4j::Core::Traversal modules so that it can handle unpersisted relationships of depth one.

Defined Under Namespace

Classes: AllRelsDsl, NodesDSL, RelsDSL, Storage

Instance Method Summary (collapse)

Instance Method Details

- (Object) _incoming(rel_type)

Same as Neo4j::Core::Node#incoming Since the incoming method is redefined we might need the neo4j-core version of this method.

See Also:



31
32
33
# File 'lib/neo4j/rails/relationships/relationships.rb', line 31

def _incoming(rel_type)
  _core_incomig(rel_type)
end

- (Object) _outgoing(rel_type)

Same as Neo4j::Core::Node#outgoing Since the incoming method is redefined we might need the neo4j-core version of this method.

See Also:



23
24
25
# File 'lib/neo4j/rails/relationships/relationships.rb', line 23

def _outgoing(rel_type)
  _core_outgoing(rel_type)
end

- (NodesDSL) incoming(rel_type)

Similar to Neo4j::Core::Node#incoming but allows traversing both persisted and unpersisted relationship of depth one. See #outgoing See, Neo4j::NodeRelationship#outgoing (when node is persisted) which returns a Neo4j::NodeTraverser

Parameters:

  • rel_type (String, Symbol)

    the relationship type we want to create or traverse

Returns:



70
71
72
73
# File 'lib/neo4j/rails/relationships/relationships.rb', line 70

def incoming(rel_type)
  storage = _create_or_get_storage(rel_type)
  NodesDSL.new(storage, :incoming)
end

- (Object) node(dir, rel_type)

Returns the only node of a given type and direction that is attached to this node, or nil. 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 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

  • 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

  • rel_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


160
161
162
163
164
# File 'lib/neo4j/rails/relationships/relationships.rb', line 160

def node(dir, rel_type)
  raise "Illegal argument, first argument must be :both, :incoming or :outgoing, got #{dir.inspect}" unless [:incoming, :outgoing, :both].include?(dir)
  storage = _create_or_get_storage(rel_type)
  storage.single_node(dir)
end

- (Enumerable) nodes(dir, rel_type)

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

  • rel_type (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



141
142
143
144
145
# File 'lib/neo4j/rails/relationships/relationships.rb', line 141

def nodes(dir, rel_type)
  raise "Illegal argument, first argument must be :both, :incoming or :outgoing, got #{dir.inspect}" unless [:incoming, :outgoing, :both].include?(dir)
  storage = _create_or_get_storage(rel_type)
  NodesDSL.new(storage, dir)
end

- (NodesDSL) outgoing(rel_type)

Similar to Neo4j::Core::Node#outgoing but allows traversing both persisted and unpersisted relationship of depth one. For deeper or more advanced traversals see #_outgoing

Examples:

create a relationship between two nodes which is not persisted

node.outgoing(:foo) << other_node

create and persist a relationship between two nodes with given node

node.outgoing(:foo) << other_node
node.save

create a new node with given attributes and persist the relationship

node.outgoing(:foo).create(:name => 'bla')

create a new node with given attributes and but do not persist the relationship

node.outgoing(:foo).new(:name => 'bla')

Parameters:

  • rel_type (String, Symbol)

    the relationship type we want to create or traverse

Returns:

See Also:



57
58
59
60
# File 'lib/neo4j/rails/relationships/relationships.rb', line 57

def outgoing(rel_type)
  storage = _create_or_get_storage(rel_type)
  NodesDSL.new(storage, :outgoing)
end

- (Neo4j::Relationship, ...) rel(dir, rel_type)

Note:

it can return an unpersisted relationship

Returns the only relationship of a given type and direction that is attached to this node, or nil. 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

  • rel_type (Symbol, String)

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

Returns:

Raises:

  • an exception if more then one relationship of given type and direction was found



116
117
118
119
120
# File 'lib/neo4j/rails/relationships/relationships.rb', line 116

def rel(dir, rel_type)
  raise "Illegal argument, first argument must be :both, :incoming or :outgoing, got #{dir.inspect}" unless [:incoming, :outgoing, :both].include?(dir)
  storage = _create_or_get_storage(rel_type)
  storage.single_relationship(dir)
end

- (Boolean) rel?(dir = :both, rel_type = nil)

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)

  • rel_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



129
130
131
132
133
# File 'lib/neo4j/rails/relationships/relationships.rb', line 129

def rel?(dir=:both, rel_type=nil)
  raise "Illegal argument, first argument must be :both, :incoming or :outgoing, got #{dir.inspect}" unless [:incoming, :outgoing, :both].include?(dir)
  storage = _create_or_get_storage(rel_type)
  !!storage.single_relationship(dir)
end

- (RelsDSL, AllRelsDsl) rels(dir = :both, rel_type = nil)

Note:

it can return an unpersisted relationship

Allow to access persisted and unpersisted relationships - like the #outgoing and #incoming method. To only find all the persisted relationship, node._java_entity.rels

Examples:

node.outgoing(:foo) << node2
node.rels(:outgoing, :foo).outgoing #=> [node2]
node.rels(:foo).incoming #=> []
node.rels(:both) #=> [node2] - incoming and outgoing of any type

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

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

Parameters:

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

    the direction of the relationship

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

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

Returns:



91
92
93
94
95
96
97
98
99
# File 'lib/neo4j/rails/relationships/relationships.rb', line 91

def rels(dir=:both, rel_type=nil)
  raise "Illegal argument, first argument must be :both, :incoming or :outgoing, got #{dir.inspect}" unless [:incoming, :outgoing, :both].include?(dir)
  if rel_type.nil?
    AllRelsDsl.new(@_relationships, _java_node, dir)
  else
    storage = _create_or_get_storage(rel_type)
    RelsDSL.new(storage, dir)
  end
end