Last active
August 29, 2015 14:17
-
-
Save tdg5/01f89d149e8a02ca8e55 to your computer and use it in GitHub Desktop.
iterative pbt_deep_dup_build
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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