Class: Neo4j::ActiveRel::RelatedNode
- Inherits:
-
Object
- Object
- Neo4j::ActiveRel::RelatedNode
- Defined in:
- lib/neo4j/active_rel/related_node.rb
Overview
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.
Defined Under Namespace
Classes: UnsetRelatedNodeError
Instance Method Summary collapse
-
#==(other) ⇒ Object
Loads the node if needed, then conducts comparison.
- #class ⇒ Object
- #cypher_representation(clazz) ⇒ Object
-
#initialize(node = nil) ⇒ RelatedNode
constructor
ActiveRel’s related nodes can be initialized with nothing, an integer, or a fully wrapped node.
-
#loaded ⇒ Object
Loads a node from the database or returns the node if already laoded.
-
#loaded? ⇒ Boolean
Indicates whether a node has or has not been fully loaded from the database.
- #method_missing(*args, &block) ⇒ Object
-
#neo_id ⇒ Object
Returns the neo_id of a given node without loading.
- #respond_to_missing?(method_name, include_private = false) ⇒ Boolean
- #set? ⇒ Boolean
Constructor Details
#initialize(node = nil) ⇒ RelatedNode
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.
14 15 16 |
# File 'lib/neo4j/active_rel/related_node.rb', line 14 def initialize(node = nil) @node = valid_node_param?(node) ? node : (fail Neo4j::InvalidParameterError, 'RelatedNode must be initialized with either a node ID or node') end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(*args, &block) ⇒ Object
64 65 66 |
# File 'lib/neo4j/active_rel/related_node.rb', line 64 def method_missing(*args, &block) loaded.send(*args, &block) end |
Instance Method Details
#==(other) ⇒ Object
Loads the node if needed, then conducts comparison.
19 20 21 22 |
# File 'lib/neo4j/active_rel/related_node.rb', line 19 def ==(other) loaded if @node.is_a?(Integer) @node == other end |
#class ⇒ Object
73 74 75 |
# File 'lib/neo4j/active_rel/related_node.rb', line 73 def class loaded.send(:class) end |
#cypher_representation(clazz) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/neo4j/active_rel/related_node.rb', line 40 def cypher_representation(clazz) case when !set? "(#{formatted_label_list(clazz)})" when set? && !loaded? "(Node with neo_id #{@node})" else node_class = self.class id_name = node_class.id_property_name labels = ':' + node_class.mapped_label_names.join(':') "(#{labels} {#{id_name}: #{@node.id.inspect}})" end end |
#loaded ⇒ Object
Loads a node from the database or returns the node if already laoded
30 31 32 33 34 35 36 37 |
# File 'lib/neo4j/active_rel/related_node.rb', line 30 def loaded fail UnsetRelatedNodeError, 'Node not set, cannot load' if @node.nil? @node = if @node.respond_to?(:neo_id) @node else Neo4j::ActiveBase.new_query.match(:n).where(n: {neo_id: @node}).pluck(:n).first end end |
#loaded? ⇒ Boolean
Returns indicates whether a node has or has not been fully loaded from the database.
56 57 58 |
# File 'lib/neo4j/active_rel/related_node.rb', line 56 def loaded? @node.respond_to?(:neo_id) end |
#neo_id ⇒ Object
Returns the neo_id of a given node without loading.
25 26 27 |
# File 'lib/neo4j/active_rel/related_node.rb', line 25 def neo_id loaded? ? @node.neo_id : @node end |
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
68 69 70 71 |
# File 'lib/neo4j/active_rel/related_node.rb', line 68 def respond_to_missing?(method_name, include_private = false) loaded if @node.is_a?(Numeric) @node.respond_to?(method_name) ? true : super end |
#set? ⇒ Boolean
60 61 62 |
# File 'lib/neo4j/active_rel/related_node.rb', line 60 def set? !@node.nil? end |