Sha256: e8a2986e0d9645bfa5c2e59940e748deb07c06690e7034eee0a847f755cb252c

Contents?: true

Size: 1.89 KB

Versions: 11

Compression:

Stored size: 1.89 KB

Contents

module Neo4j
  module ActiveNode
    module Unpersisted
      def pending_associations
        @pending_associations ||= {}
      end

      def pending_associations?
        !@pending_associations.blank?
      end

      private

      # TODO: Change this method's name.
      # Takes the pending_associations hash, which is in the format { cache_key => [:association_name, :association_operator]},
      # and returns them as { association_name => [[nodes_for_persistence], :operator] }
      def pending_associations_with_nodes
        return unless pending_associations?
        {}.tap do |deferred_nodes|
          pending_associations.each_pair do |cache_key, (association_name, operator)|
            nodes_for_creation = self.persisted? ? association_proxy_cache[cache_key].select { |n| !n.persisted? } : association_proxy_cache[cache_key]
            deferred_nodes[association_name] = [nodes_for_creation, operator]
          end
        end
      end

      # @param [Hash] deferred_nodes A hash created by :pending_associations_with_nodes
      def process_unpersisted_nodes!(deferred_nodes)
        deferred_nodes.each_pair do |k, (v, o)|
          save_and_associate_queue(k, v, o)
        end
      end


      def save_and_associate_queue(association_name, node_queue, operator)
        association_proc = proc { |node| save_and_associate_node(association_name, node, operator) }
        node_queue.each do |element|
          element.is_a?(Array) ? element.each { |node| association_proc.call(node) } : association_proc.call(element)
        end
      end

      def save_and_associate_node(association_name, node, operator)
        node.save if node.changed? || !node.persisted?
        fail "Unable to defer node persistence, could not save #{node.inspect}" unless node.persisted?
        operator == :<< ? send(association_name).send(operator, node) : send(:"#{association_name}=", node)
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
neo4j-5.2.2 lib/neo4j/active_node/unpersisted.rb
neo4j-5.2.1 lib/neo4j/active_node/unpersisted.rb
neo4j-5.2.0 lib/neo4j/active_node/unpersisted.rb
neo4j-5.1.4 lib/neo4j/active_node/unpersisted.rb
neo4j-5.1.3 lib/neo4j/active_node/unpersisted.rb
neo4j-5.1.2 lib/neo4j/active_node/unpersisted.rb
neo4j-5.1.1 lib/neo4j/active_node/unpersisted.rb
neo4j-5.1.0 lib/neo4j/active_node/unpersisted.rb
neo4j-5.1.0.rc.3 lib/neo4j/active_node/unpersisted.rb
neo4j-5.1.0.rc.2 lib/neo4j/active_node/unpersisted.rb
neo4j-5.1.0.rc.1 lib/neo4j/active_node/unpersisted.rb