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
73 def class
74   loaded.send(:class)
75 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
40 def cypher_representation(clazz)
41   case
42   when !set?
43     "(#{formatted_label_list(clazz)})"
44   when set? && !loaded?
45     "(Node with neo_id #{@node})"
46   else
47     node_class = self.class
48     id_name = node_class.id_property_name
49     labels = ':' + node_class.mapped_label_names.join(':')
50 
51     "(#{labels} {#{id_name}: #{@node.id.inspect}})"
52   end
53 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 = if @node.respond_to?(:neo_id)
33             @node
34           else
35             Neo4j::ActiveBase.new_query.match(:n).where(n: {neo_id: @node}).pluck(:n).first
36           end
37 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
56 def loaded?
57   @node.respond_to?(:neo_id)
58 end
method_missing(*args, &block) click to toggle source
   # File lib/neo4j/active_rel/related_node.rb
64 def method_missing(*args, &block)
65   loaded.send(*args, &block)
66 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
68 def respond_to_missing?(method_name, include_private = false)
69   loaded if @node.is_a?(Numeric)
70   @node.respond_to?(method_name) ? true : super
71 end
set?() click to toggle source
   # File lib/neo4j/active_rel/related_node.rb
60 def set?
61   !@node.nil?
62 end

Private Instance Methods

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