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.
-
#extensions ⇒ Object
readonly
Returns the value of attribute extensions.
-
#final ⇒ Object
Returns the value of attribute final.
-
#header ⇒ Object
Returns the value of attribute header.
-
#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.
-
#types ⇒ Object
readonly
Returns the value of attribute types.
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 19 20 21 |
# File 'lib/schema_plus/core/schema_dump.rb', line 11 def initialize(dumper) @dumper = dumper @dependencies = Hash.new { |h, k| h[k] = [] } @header = '' @initial = [] @extensions = [] @types = [] @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 |
#extensions ⇒ Object (readonly)
Returns the value of attribute extensions.
8 9 10 |
# File 'lib/schema_plus/core/schema_dump.rb', line 8 def extensions @extensions 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 |
#header ⇒ Object
Returns the value of attribute header.
9 10 11 |
# File 'lib/schema_plus/core/schema_dump.rb', line 9 def header @header 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 |
#types ⇒ Object (readonly)
Returns the value of attribute types.
8 9 10 |
# File 'lib/schema_plus/core/schema_dump.rb', line 8 def types @types end |
Instance Method Details
#assemble(stream) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/schema_plus/core/schema_dump.rb', line 28 def assemble(stream) stream.puts @header stream.puts @initial.join("\n") if initial.any? stream.puts @extensions.join("\n") if extensions.any? stream.puts @types.join("\n") if types.any? assemble_tables(stream) final.each do |statement| stream.puts " #{statement}" end stream.puts @trailer end |
#assemble_tables(stream) ⇒ Object
40 41 42 43 44 |
# File 'lib/schema_plus/core/schema_dump.rb', line 40 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_plus/core/schema_dump.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
50 51 52 |
# File 'lib/schema_plus/core/schema_dump.rb', line 50 def tsort_each_child(tablename, &block) @dependencies[tablename].sort.uniq.reject{|t| @dumper.ignored? t}.each(&block) end |
#tsort_each_node(&block) ⇒ Object
46 47 48 |
# File 'lib/schema_plus/core/schema_dump.rb', line 46 def tsort_each_node(&block) @tables.keys.sort.each(&block) end |