Class: Infold::DbSchema

Inherits:
Object
  • Object
show all
Defined in:
lib/infold/db_schema.rb

Instance Method Summary collapse

Constructor Details

#initialize(content = nil) ⇒ DbSchema

Returns a new instance of DbSchema.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/infold/db_schema.rb', line 6

def initialize(content=nil)
  @tables = []
  return unless content
  content.split("\n").each.each do |row|
    row = row.strip
    if row.start_with?('create_table')
      table_name = row.split('"').second.strip
      table = Table.new(table_name)
      @tables << table
      table.add_columns('id', 'bigint') unless row.include?('id: false')
    elsif @tables.present? && row.start_with?('t.')
      table = @tables.last
      name = row.split('"').second.strip
      type = row.match(/^t\..*? /).to_s.gsub('t.', '').strip
      table.add_columns(name, type)
    end
  end
end

Instance Method Details

#table(name) ⇒ Object



25
26
27
# File 'lib/infold/db_schema.rb', line 25

def table(name)
  @tables.find { |t| t.name.underscore.singularize == name.underscore.singularize }
end