Class: SchemaPlus::Core::SchemaDump
- Inherits:
-
Object
- Object
- SchemaPlus::Core::SchemaDump
- Includes:
- TSort
- Defined in:
- lib/schema_plus/core/schema_dump.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.
-
#final ⇒ Object
Returns the value of attribute final.
-
#initial ⇒ Object
readonly
Returns the value of attribute initial.
-
#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) ⇒ SchemaDump
constructor
A new instance of SchemaDump.
- #tsort_each_child(tablename, &block) ⇒ Object
- #tsort_each_node(&block) ⇒ Object
Constructor Details
#initialize(dumper) ⇒ SchemaDump
Returns a new instance of SchemaDump.
11 12 13 14 15 16 17 18 |
# File 'lib/schema_plus/core/schema_dump.rb', line 11 def initialize(dumper) @dumper = dumper @dependencies = Hash.new { |h, k| h[k] = [] } @initial = [] @tables = {} @final = [] @data = OpenStruct.new # a place for middleware to leave data end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
8 9 10 |
# File 'lib/schema_plus/core/schema_dump.rb', line 8 def data @data end |
#dependencies ⇒ Object (readonly)
Returns the value of attribute dependencies.
8 9 10 |
# File 'lib/schema_plus/core/schema_dump.rb', line 8 def dependencies @dependencies end |
#final ⇒ Object
Returns the value of attribute final.
9 10 11 |
# File 'lib/schema_plus/core/schema_dump.rb', line 9 def final @final end |
#initial ⇒ Object (readonly)
Returns the value of attribute initial.
8 9 10 |
# File 'lib/schema_plus/core/schema_dump.rb', line 8 def initial @initial end |
#tables ⇒ Object (readonly)
Returns the value of attribute tables.
8 9 10 |
# File 'lib/schema_plus/core/schema_dump.rb', line 8 def tables @tables end |
#trailer ⇒ Object
Returns the value of attribute trailer.
9 10 11 |
# File 'lib/schema_plus/core/schema_dump.rb', line 9 def trailer @trailer end |
Instance Method Details
#assemble(stream) ⇒ Object
25 26 27 28 29 30 31 32 |
# File 'lib/schema_plus/core/schema_dump.rb', line 25 def assemble(stream) stream.puts @initial.join("\n") if initial.any? assemble_tables(stream) final.each do |statement| stream.puts " #{statement}" end stream.puts @trailer end |
#assemble_tables(stream) ⇒ Object
34 35 36 37 38 |
# File 'lib/schema_plus/core/schema_dump.rb', line 34 def assemble_tables(stream) tsort().each do |table| @tables[table].assemble(stream) if @tables[table] end end |
#depends(tablename, dependents) ⇒ Object
20 21 22 23 |
# File 'lib/schema_plus/core/schema_dump.rb', line 20 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
44 45 46 |
# File 'lib/schema_plus/core/schema_dump.rb', line 44 def tsort_each_child(tablename, &block) @dependencies[tablename].sort.uniq.reject{|t| @dumper.ignored? t}.each(&block) end |
#tsort_each_node(&block) ⇒ Object
40 41 42 |
# File 'lib/schema_plus/core/schema_dump.rb', line 40 def tsort_each_node(&block) @tables.keys.sort.each(&block) end |