Sha256: 5afccec35af7288afb89e4c8dd8153ad4a95e0086a7c9b6156af01ac91c7977c

Contents?: true

Size: 1.42 KB

Versions: 4

Compression:

Stored size: 1.42 KB

Contents

module ActiveGraph
  module Node
    module Unpersisted
      # The values in this Hash are returned and used outside by reference
      # so any modifications to the Array should be in-place
      def deferred_create_cache
        @deferred_create_cache ||= {}
      end

      def defer_create(association_name, object, options = {})
        clear_deferred_nodes_for_association(association_name) if options[:clear]

        deferred_nodes_for_association(association_name) << object
      end

      def deferred_nodes_for_association(association_name)
        deferred_create_cache[association_name.to_sym] ||= []
      end

      def pending_deferred_creations?
        !deferred_create_cache.values.all?(&:empty?)
      end

      def clear_deferred_nodes_for_association(association_name)
        deferred_nodes_for_association(association_name.to_sym).clear
      end

      private

      def process_unpersisted_nodes!
        deferred_create_cache.dup.each do |association_name, nodes|
          association_proxy = association_proxy(association_name)

          nodes.each do |node|
            if node.respond_to?(:changed?)
              node.save if node.changed? || !node.persisted?
              fail "Unable to defer node persistence, could not save #{node.inspect}" unless node.persisted?
            end

            association_proxy << node
          end
        end

        @deferred_create_cache = {}
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
activegraph-10.0.0.pre.beta.4 lib/active_graph/node/unpersisted.rb
activegraph-10.0.0.pre.beta.3 lib/active_graph/node/unpersisted.rb
activegraph-10.0.0.pre.beta.2 lib/active_graph/node/unpersisted.rb
activegraph-10.0.0.pre.beta.1 lib/active_graph/node/unpersisted.rb