lib/neo4j/active_node/unpersisted.rb in neo4j-6.0.0.rc.4 vs lib/neo4j/active_node/unpersisted.rb in neo4j-6.0.0
- old
+ new
@@ -1,53 +1,48 @@
module Neo4j
module ActiveNode
module Unpersisted
- def pending_associations
- @pending_associations ||= []
+ # 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 pending_associations?
- !@pending_associations.blank?
+ 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
- private
+ def deferred_nodes_for_association(association_name)
+ deferred_create_cache[association_name.to_sym] ||= []
+ end
- # 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.uniq.each do |association_name|
- nodes_for_creation = association_proxy(association_name)
- nodes_for_creation = nodes_for_creation.reject(&:persisted?) if self.persisted?
-
- deferred_nodes[association_name] = nodes_for_creation
- end
- end
+ def pending_deferred_creations?
+ !deferred_create_cache.values.all?(&:empty?)
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|
- save_and_associate_queue(k, v)
- end
+ def clear_deferred_nodes_for_association(association_name)
+ deferred_nodes_for_association(association_name.to_sym).clear
end
+ private
- def save_and_associate_queue(association_name, node_queue)
- association_proc = proc { |node| save_and_associate_node(association_name, node) }
- node_queue.each do |element|
- element.is_a?(Array) ? element.each { |node| association_proc.call(node) } : association_proc.call(element)
- end
- end
+ def process_unpersisted_nodes!
+ deferred_create_cache.each do |association_name, nodes|
+ association_proxy = association_proxy(association_name)
- def save_and_associate_node(association_name, 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?
+ 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
+
+ @deferred_create_cache = {}
end
- association_proxy(association_name) << node
end
end
end
end