Sha256: 824e277cfbaceea0ecb37c450fb99365f9affda3d7aceab65d4aad9b565dfeaa

Contents?: true

Size: 1.5 KB

Versions: 26

Compression:

Stored size: 1.5 KB

Contents

require 'active_support/inflector'
require 'active_graph/core/node'

wrapping_proc = proc do |node|
  found_class = ActiveGraph::NodeWrapping.class_to_wrap(node.labels)
  next node if not found_class

  found_class.new.tap do |wrapped_node|
    wrapped_node.init_on_load(node, node.properties)
  end
end
Neo4j::Driver::Types::Node.wrapper_callback(wrapping_proc)

module ActiveGraph
  module NodeWrapping
    # Only load classes once for performance
    CONSTANTS_FOR_LABELS_CACHE = {}

    class << self
      def class_to_wrap(labels)
        load_classes_from_labels(labels)
        ActiveGraph::Node::Labels.model_for_labels(labels).tap do |model_class|
          populate_constants_for_labels_cache(model_class, labels)
        end
      end

      private

      def load_classes_from_labels(labels)
        labels.each { |label| constant_for_label(label) }
      end

      def constant_for_label(label)
        CONSTANTS_FOR_LABELS_CACHE[label] || CONSTANTS_FOR_LABELS_CACHE[label] = constantized_label(label)
      end

      def constantized_label(label)
        "#{association_model_namespace}::#{label}".constantize
      rescue NameError, LoadError
        nil
      end

      def populate_constants_for_labels_cache(model_class, labels)
        labels.each do |label|
          CONSTANTS_FOR_LABELS_CACHE[label] = model_class if CONSTANTS_FOR_LABELS_CACHE[label].nil?
        end
      end

      def association_model_namespace
        ActiveGraph::Config.association_model_namespace_string
      end
    end
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
activegraph-11.4.0 lib/active_graph/node/node_wrapper.rb
activegraph-11.3.1 lib/active_graph/node/node_wrapper.rb
activegraph-11.3.0 lib/active_graph/node/node_wrapper.rb
activegraph-11.2.0 lib/active_graph/node/node_wrapper.rb
activegraph-11.1.0 lib/active_graph/node/node_wrapper.rb
activegraph-11.1.0.beta.1 lib/active_graph/node/node_wrapper.rb
activegraph-11.1.0.alpha.4 lib/active_graph/node/node_wrapper.rb
activegraph-11.1.0.alpha.3 lib/active_graph/node/node_wrapper.rb
activegraph-11.1.0.alpha.2 lib/active_graph/node/node_wrapper.rb
activegraph-11.1.0.alpha.1 lib/active_graph/node/node_wrapper.rb
activegraph-10.2.0.beta.1 lib/active_graph/node/node_wrapper.rb
activegraph-11.0.2-java lib/active_graph/node/node_wrapper.rb
activegraph-10.1.1 lib/active_graph/node/node_wrapper.rb
activegraph-11.0.1-java lib/active_graph/node/node_wrapper.rb
activegraph-11.0.0-java lib/active_graph/node/node_wrapper.rb
activegraph-10.1.0 lib/active_graph/node/node_wrapper.rb
activegraph-11.0.0.beta.4-java lib/active_graph/node/node_wrapper.rb
activegraph-10.0.2 lib/active_graph/node/node_wrapper.rb
activegraph-11.0.0.beta.3-java lib/active_graph/node/node_wrapper.rb
activegraph-11.0.0.beta.2-java lib/active_graph/node/node_wrapper.rb