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
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
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
# File lib/neo4j/active_rel/related_node.rb 69 def class 70 loaded.send(:class) 71 end
@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
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
@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
# File lib/neo4j/active_rel/related_node.rb 60 def method_missing(*args, &block) 61 loaded.send(*args, &block) 62 end
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
# 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
# File lib/neo4j/active_rel/related_node.rb 56 def set? 57 !@node.nil? 58 end
Private Instance Methods
# File lib/neo4j/active_rel/related_node.rb 75 def formatted_label_list(list) 76 list.is_a?(Array) ? list.join(' || ') : list 77 end
# 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