Sha256: 31e974c5727d6df7c2ebdba0601bca6b2ad7a0e60ddcc973df1c90297c09901c

Contents?: true

Size: 1.92 KB

Versions: 7

Compression:

Stored size: 1.92 KB

Contents

module Neo4j::ActiveRel
  # 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.
  class RelatedNode

    class InvalidParameterError < StandardError; end

    # 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.
    #
    # Initialization with a node doesn't appear to happen in the code. TODO: maybe find out why this is an option.
    def initialize(node = nil)
      @node = valid_node_param?(node) ? node : (raise InvalidParameterError, 'RelatedNode must be initialized with either a node ID or node' )
    end

    # Loads the node if needed, then conducts comparison.
    def == (obj)
      loaded if @node.is_a?(Fixnum)
      @node == obj
    end

    # Returns the neo_id of a given node without loading.
    def neo_id
      loaded? ? @node.neo_id : @node
    end

    # Loads a node from the database or returns the node if already laoded
    def loaded
      @node = @node.respond_to?(:neo_id) ? @node : Neo4j::Node.load(@node)
    end

    # @return [Boolean] indicates whether a node has or has not been fully loaded from the database
    def loaded?
      @node.respond_to?(:neo_id)
    end

    def method_missing(*args, &block)
      loaded.send(*args, &block)
    end

    def class
      loaded.send(:class)
    end

    private

    def valid_node_param?(node)
      node.nil? || node.is_a?(Integer) || node.respond_to?(:neo_id)
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
neo4j-4.0.0 lib/neo4j/active_rel/related_node.rb
neo4j-4.0.0.rc.4 lib/neo4j/active_rel/related_node.rb
neo4j-4.0.0.rc.3 lib/neo4j/active_rel/related_node.rb
neo4j-4.0.0.rc.1 lib/neo4j/active_rel/related_node.rb
neo4j-3.0.4 lib/neo4j/active_rel/related_node.rb
neo4j-3.0.3 lib/neo4j/active_rel/related_node.rb
neo4j-3.0.2 lib/neo4j/active_rel/related_node.rb