Class: SchemaPlus::Core::SchemaDump

Inherits:
Object
  • Object
show all
Includes:
TSort
Defined in:
lib/schema_plus/core/schema_dump.rb

Defined Under Namespace

Classes: Table

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dumper) ⇒ SchemaDump

Returns a new instance of SchemaDump.



9
10
11
12
13
14
15
16
# File 'lib/schema_plus/core/schema_dump.rb', line 9

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

#dataObject (readonly)

Returns the value of attribute data.



6
7
8
# File 'lib/schema_plus/core/schema_dump.rb', line 6

def data
  @data
end

#dependenciesObject (readonly)

Returns the value of attribute dependencies.



6
7
8
# File 'lib/schema_plus/core/schema_dump.rb', line 6

def dependencies
  @dependencies
end

#finalObject

Returns the value of attribute final.



7
8
9
# File 'lib/schema_plus/core/schema_dump.rb', line 7

def final
  @final
end

#initialObject (readonly)

Returns the value of attribute initial.



6
7
8
# File 'lib/schema_plus/core/schema_dump.rb', line 6

def initial
  @initial
end

#tablesObject (readonly)

Returns the value of attribute tables.



6
7
8
# File 'lib/schema_plus/core/schema_dump.rb', line 6

def tables
  @tables
end

#trailerObject

Returns the value of attribute trailer.



7
8
9
# File 'lib/schema_plus/core/schema_dump.rb', line 7

def trailer
  @trailer
end

Instance Method Details

#assemble(stream) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/schema_plus/core/schema_dump.rb', line 23

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



32
33
34
35
36
# File 'lib/schema_plus/core/schema_dump.rb', line 32

def assemble_tables(stream)
  tsort().each do |table|
    @tables[table].assemble(stream) if @tables[table]
  end
end

#depends(tablename, dependents) ⇒ Object



18
19
20
21
# File 'lib/schema_plus/core/schema_dump.rb', line 18

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



42
43
44
# File 'lib/schema_plus/core/schema_dump.rb', line 42

def tsort_each_child(tablename, &block)
  @dependencies[tablename].sort.uniq.reject{|t| @dumper.ignored? t}.each(&block)
end

#tsort_each_node(&block) ⇒ Object



38
39
40
# File 'lib/schema_plus/core/schema_dump.rb', line 38

def tsort_each_node(&block)
  @tables.keys.sort.each(&block)
end