Class: Neo4j::ActiveRel::RelatedNode

Inherits:
Object
  • Object
show all
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

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



60
61
62
# File 'lib/neo4j/active_rel/related_node.rb', line 60

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

#classObject



69
70
71
# File 'lib/neo4j/active_rel/related_node.rb', line 69

def class
  loaded.send(:class)
end

#cypher_representation(clazz) ⇒ Object

Parameters:

  • clazz (String, Symbol, Array)

    An alternate label to use in the event the node is not present or loaded



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/neo4j/active_rel/related_node.rb', line 36

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

#loadedObject

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



30
31
32
33
# File 'lib/neo4j/active_rel/related_node.rb', line 30

def loaded
  fail UnsetRelatedNodeError, 'Node not set, cannot load' if @node.nil?
  @node = @node.respond_to?(:neo_id) ? @node : Neo4j::Node.load(@node)
end

#loaded?Boolean

Returns indicates whether a node has or has not been fully loaded from the database.

Returns:

  • (Boolean)

    indicates whether a node has or has not been fully loaded from the database



52
53
54
# File 'lib/neo4j/active_rel/related_node.rb', line 52

def loaded?
  @node.respond_to?(:neo_id)
end

#neo_idObject

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

Returns:



64
65
66
67
# File 'lib/neo4j/active_rel/related_node.rb', line 64

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

Returns:



56
57
58
# File 'lib/neo4j/active_rel/related_node.rb', line 56

def set?
  !@node.nil?
end