Class: SchemaMonkey::ActiveRecord::SchemaDumper::Dump
- Inherits:
-
Object
- Object
- SchemaMonkey::ActiveRecord::SchemaDumper::Dump
- Includes:
- TSort
- Defined in:
- lib/schema_monkey/active_record/schema_dumper.rb
Defined Under Namespace
Classes: Table
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#dependencies ⇒ Object
readonly
Returns the value of attribute dependencies.
-
#extensions ⇒ Object
readonly
Returns the value of attribute extensions.
-
#foreign_keys ⇒ Object
Returns the value of attribute foreign_keys.
-
#tables ⇒ Object
readonly
Returns the value of attribute tables.
-
#trailer ⇒ Object
Returns the value of attribute trailer.
Instance Method Summary collapse
- #assemble(stream) ⇒ Object
- #assemble_tables(stream) ⇒ Object
- #depends(tablename, dependents) ⇒ Object
-
#initialize(dumper) ⇒ Dump
constructor
A new instance of Dump.
- #tsort_each_child(tablename, &block) ⇒ Object
- #tsort_each_node(&block) ⇒ Object
Constructor Details
#initialize(dumper) ⇒ Dump
Returns a new instance of Dump.
14 15 16 17 18 19 20 21 |
# File 'lib/schema_monkey/active_record/schema_dumper.rb', line 14 def initialize(dumper) @dumper = dumper @dependencies = Hash.new { |h, k| h[k] = [] } @extensions = [] @tables = {} @foreign_keys = [] @data = OpenStruct.new # a place for middleware to leave data end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
11 12 13 |
# File 'lib/schema_monkey/active_record/schema_dumper.rb', line 11 def data @data end |
#dependencies ⇒ Object (readonly)
Returns the value of attribute dependencies.
11 12 13 |
# File 'lib/schema_monkey/active_record/schema_dumper.rb', line 11 def dependencies @dependencies end |
#extensions ⇒ Object (readonly)
Returns the value of attribute extensions.
11 12 13 |
# File 'lib/schema_monkey/active_record/schema_dumper.rb', line 11 def extensions @extensions end |
#foreign_keys ⇒ Object
Returns the value of attribute foreign_keys.
12 13 14 |
# File 'lib/schema_monkey/active_record/schema_dumper.rb', line 12 def foreign_keys @foreign_keys end |
#tables ⇒ Object (readonly)
Returns the value of attribute tables.
11 12 13 |
# File 'lib/schema_monkey/active_record/schema_dumper.rb', line 11 def tables @tables end |
#trailer ⇒ Object
Returns the value of attribute trailer.
12 13 14 |
# File 'lib/schema_monkey/active_record/schema_dumper.rb', line 12 def trailer @trailer end |
Instance Method Details
#assemble(stream) ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/schema_monkey/active_record/schema_dumper.rb', line 28 def assemble(stream) stream.puts @extensions.join("\n") if extensions.any? assemble_tables(stream) foreign_keys.each do |statement| stream.puts " #{statement}" end stream.puts @trailer end |
#assemble_tables(stream) ⇒ Object
37 38 39 40 41 |
# File 'lib/schema_monkey/active_record/schema_dumper.rb', line 37 def assemble_tables(stream) tsort().each do |table| @tables[table].assemble(stream) if @tables[table] end end |
#depends(tablename, dependents) ⇒ Object
23 24 25 26 |
# File 'lib/schema_monkey/active_record/schema_dumper.rb', line 23 def depends(tablename, dependents) @tables[tablename] ||= false # placeholder for dependencies applied before defining the table @dependencies[tablename] += Array.wrap(dependents) end |
#tsort_each_child(tablename, &block) ⇒ Object
47 48 49 |
# File 'lib/schema_monkey/active_record/schema_dumper.rb', line 47 def tsort_each_child(tablename, &block) @dependencies[tablename].sort.uniq.reject{|t| @dumper.ignored? t}.each(&block) end |
#tsort_each_node(&block) ⇒ Object
43 44 45 |
# File 'lib/schema_monkey/active_record/schema_dumper.rb', line 43 def tsort_each_node(&block) @tables.keys.sort.each(&block) end |