Skip to content

Instantly share code, notes, and snippets.

@tdg5
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tdg5/01f89d149e8a02ca8e55 to your computer and use it in GitHub Desktop.
Save tdg5/01f89d149e8a02ca8e55 to your computer and use it in GitHub Desktop.
iterative pbt_deep_dup_build
def self.pbt_deep_dup_build(clone, original)
mapped = Set.new
edges = [[clone, original]]
while edges.any?
builder, twin = edges.shift
pair_hash = "#{builder.class.name}:#{builder.hash}~#{twin.hash}".hash
next if mapped.include?(pair_hash)
mapped << pair_hash
pbt_dup_build(builder, twin)
map_association_edges(builder, twin, edges)
end
clone
end
def self.map_association_edges(clone, original, edges)
PolyBelongsTo::Pbt::Reflects[original].each do |ref|
child = original.send(ref)
PolyBelongsTo::Pbt::AsCollectionProxy[clone, original].each do |builder|
if child.respond_to?(:build)
child.each { |spawn| edges << [builder, spawn] }
else
edges << [builder, child]
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment