Created
February 14, 2012 17:59
-
-
Save vangberg/1828617 to your computer and use it in GitHub Desktop.
This file contains 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
class Dumper | |
attr_accessor :object, :tables | |
def initialize(object, tables=nil) | |
@object = object | |
@tables = tables || Hash.new {|hash,key| hash[key] = []} | |
end | |
def dump | |
puts "Dumping #{object.class.name}##{object.id}" | |
object.class.reflect_on_all_associations.each do |assoc| | |
case assoc.macro | |
when :has_many | |
object.send(assoc.name).each do |child| | |
if !tables[child.class.table_name].include?(child.id) | |
tables[assoc.table_name] << child.id | |
Dumper.new(child, tables).dump | |
end | |
end | |
when :belongs_to | |
child = object.send(assoc.name) | |
if child && !tables[child.class.table_name].include?(child.id) | |
tables[child.class.table_name] << child.id | |
Dumper.new(child, tables).dump | |
end | |
end | |
end | |
self | |
end | |
def out | |
tables.each do |table, ids| | |
puts "#{table}: #{ids}" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment