Sha256: 6da2fc9b39ad81573238cc392f08cba95d1299ad68d2c4d0ae923189b202bb8a

Contents?: true

Size: 1.73 KB

Versions: 6

Compression:

Stored size: 1.73 KB

Contents

class Neo4j::Node
  # The wrapping process is what transforms a raw CypherNode or EmbeddedNode from Neo4j::Core into a healthy ActiveNode (or ActiveRel) object.
  module Wrapper
    # this is a plugin in the neo4j-core so that the Ruby wrapper will be wrapped around the Neo4j::Node objects
    def wrapper
      self.props.symbolize_keys!
      most_concrete_class = sorted_wrapper_classes
      return self unless most_concrete_class
      wrapped_node = most_concrete_class.new
      wrapped_node.init_on_load(self, self.props)
      wrapped_node
    end

    def checked_labels_set
      @@_checked_labels_set ||= Set.new
    end

    def check_label(label_name)
      unless checked_labels_set.include?(label_name)
        load_class_from_label(label_name)
        # do this only once
        checked_labels_set.add(label_name)
      end
    end

    # Makes the determination of whether to use <tt>_classname</tt> (or whatever is defined by config) or the node's labels.
    def sorted_wrapper_classes
      if self.props.is_a?(Hash) && self.props.key?(Neo4j::Config.class_name_property)
        self.props[Neo4j::Config.class_name_property].constantize
      else
        wrappers = _class_wrappers
        return self if wrappers.nil?
        wrapper_classes = wrappers.map { |w| Neo4j::ActiveNode::Labels._wrapped_labels[w] }
        wrapper_classes.sort.first
      end
    end

    def load_class_from_label(label_name)
      label_name.to_s.split('::').inject(Kernel) { |container, name| container.const_get(name.to_s) }
    rescue NameError
      nil
    end

    def _class_wrappers
      labels.find_all do |label_name|
        check_label(label_name)
        Neo4j::ActiveNode::Labels._wrapped_labels[label_name].class == Class
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
neo4j-4.1.5 lib/neo4j/active_node/node_wrapper.rb
neo4j-4.1.4 lib/neo4j/active_node/node_wrapper.rb
neo4j-4.1.3 lib/neo4j/active_node/node_wrapper.rb
neo4j-4.1.2 lib/neo4j/active_node/node_wrapper.rb
neo4j-4.1.1 lib/neo4j/active_node/node_wrapper.rb
neo4j-4.1.0 lib/neo4j/active_node/node_wrapper.rb