class ActiveGraph::Relationship::RelatedNode
A container for Relationship'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
Relationship's related nodes can be initialized with nothing, an integer, or a fully wrapped node.
Initialization with nothing happens when a new, non-persisted Relationship
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/active_graph/relationship/related_node.rb 14 def initialize(node = nil) 15 @node = valid_node_param?(node) ? node : (fail ActiveGraph::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/active_graph/relationship/related_node.rb 19 def ==(other) 20 loaded if @node.is_a?(Integer) 21 @node == other 22 end
# File lib/active_graph/relationship/related_node.rb 73 def class 74 loaded.send(:class) 75 end
@param [String, Symbol, Array] clazz An alternate label to use in the event the node is not present or loaded
# File lib/active_graph/relationship/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
Loads a node from the database or returns the node if already laoded
# File lib/active_graph/relationship/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 ActiveGraph::Base.new_query.match(:n).where(n: {neo_id: @node}).pluck(:n).first 36 end 37 end
@return [Boolean] indicates whether a node has or has not been fully loaded from the database
# File lib/active_graph/relationship/related_node.rb 56 def loaded? 57 @node.respond_to?(:neo_id) 58 end
# File lib/active_graph/relationship/related_node.rb 64 def method_missing(*args, &block) 65 loaded.send(*args, &block) 66 end
Returns the neo_id
of a given node without loading.
# File lib/active_graph/relationship/related_node.rb 25 def neo_id 26 loaded? ? @node.neo_id : @node 27 end
# File lib/active_graph/relationship/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
# File lib/active_graph/relationship/related_node.rb 60 def set? 61 !@node.nil? 62 end
Private Instance Methods
# File lib/active_graph/relationship/related_node.rb 79 def formatted_label_list(list) 80 list.is_a?(Array) ? list.join(' || ') : list 81 end
# File lib/active_graph/relationship/related_node.rb 83 def valid_node_param?(node) 84 node.nil? || node.is_a?(Integer) || node.respond_to?(:neo_id) 85 end