class Neo4j::ActiveRel::RelatedNode

A container for ActiveRel’s :inbound and :outbound methods. It provides lazy loading of nodes. It’s important (or maybe not really IMPORTANT, but at least worth mentioning) that calling method_missing will result in a query to load the node if the node is not already loaded.

Public Class Methods

new(node = nil) click to toggle source

ActiveRel’s related nodes can be initialized with nothing, an integer, or a fully wrapped node.

Initialization with nothing happens when a new, non-persisted ActiveRel object is first initialized.

Initialization with an integer happens when a relationship is loaded from the database. It loads using the ID because that is provided by the Cypher response and does not require an extra query.

   # File lib/neo4j/active_rel/related_node.rb
14 def initialize(node = nil)
15   @node = valid_node_param?(node) ? node : (fail Neo4j::InvalidParameterError, 'RelatedNode must be initialized with either a node ID or node')
16 end

Public Instance Methods

==(other) click to toggle source

Loads the node if needed, then conducts comparison.

   # File lib/neo4j/active_rel/related_node.rb
19 def ==(other)
20   loaded if @node.is_a?(Integer)
21   @node == other
22 end
class() click to toggle source
   # File lib/neo4j/active_rel/related_node.rb
69 def class
70   loaded.send(:class)
71 end
cypher_representation(clazz) click to toggle source

@param [String, Symbol, Array] clazz An alternate label to use in the event the node is not present or loaded

   # File lib/neo4j/active_rel/related_node.rb
36 def cypher_representation(clazz)
37   case
38   when !set?
39     "(#{formatted_label_list(clazz)})"
40   when set? && !loaded?
41     "(Node with neo_id #{@node})"
42   else
43     node_class = self.class
44     id_name = node_class.id_property_name
45     labels = ':' + node_class.mapped_label_names.join(':')
46 
47     "(#{labels} {#{id_name}: #{@node.id.inspect}})"
48   end
49 end
loaded() click to toggle source

Loads a node from the database or returns the node if already laoded

   # File lib/neo4j/active_rel/related_node.rb
30 def loaded
31   fail UnsetRelatedNodeError, 'Node not set, cannot load' if @node.nil?
32   @node = @node.respond_to?(:neo_id) ? @node : Neo4j::Node.load(@node)
33 end
loaded?() click to toggle source

@return [Boolean] indicates whether a node has or has not been fully loaded from the database

   # File lib/neo4j/active_rel/related_node.rb
52 def loaded?
53   @node.respond_to?(:neo_id)
54 end
method_missing(*args, &block) click to toggle source
   # File lib/neo4j/active_rel/related_node.rb
60 def method_missing(*args, &block)
61   loaded.send(*args, &block)
62 end
neo_id() click to toggle source

Returns the neo_id of a given node without loading.

   # File lib/neo4j/active_rel/related_node.rb
25 def neo_id
26   loaded? ? @node.neo_id : @node
27 end
respond_to_missing?(method_name, include_private = false) click to toggle source
Calls superclass method
   # File lib/neo4j/active_rel/related_node.rb
64 def respond_to_missing?(method_name, include_private = false)
65   loaded if @node.is_a?(Numeric)
66   @node.respond_to?(method_name) ? true : super
67 end
set?() click to toggle source
   # File lib/neo4j/active_rel/related_node.rb
56 def set?
57   !@node.nil?
58 end

Private Instance Methods

formatted_label_list(list) click to toggle source
   # File lib/neo4j/active_rel/related_node.rb
75 def formatted_label_list(list)
76   list.is_a?(Array) ? list.join(' || ') : list
77 end
valid_node_param?(node) click to toggle source
   # File lib/neo4j/active_rel/related_node.rb
79 def valid_node_param?(node)
80   node.nil? || node.is_a?(Integer) || node.respond_to?(:neo_id)
81 end