Class: SchemaConverter

Inherits:
Object
  • Object
show all
Includes:
Helpers::Constants
Defined in:
lib/schema_to_dbml/build_dbml_file.rb,
lib/schema_to_dbml/schema_converter.rb

Constant Summary

Constants included from Helpers::Constants

Helpers::Constants::COLUMNS_REGEXP, Helpers::Constants::INDEXES_REGEXP, Helpers::Constants::RELATIONS_REGEXP, Helpers::Constants::TAB, Helpers::Constants::TABLES_REGEXP

Instance Method Summary collapse

Constructor Details

#initialize(dbml_relations_formatter: DbmlRelationsFormatter.new, dbml_tables_formatter: DbmlTablesFormatter.new) ⇒ SchemaConverter

Returns a new instance of SchemaConverter.



10
11
12
13
14
15
16
# File 'lib/schema_to_dbml/build_dbml_file.rb', line 10

def initialize(
  dbml_relations_formatter: DbmlRelationsFormatter.new,
  dbml_tables_formatter: DbmlTablesFormatter.new
)
  @dbml_relations_formatter = dbml_relations_formatter
  @dbml_tables_formatter = dbml_tables_formatter
end

Instance Method Details

#convert(schema_content:) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/schema_to_dbml/build_dbml_file.rb', line 18

def convert(schema_content:)
  tables = []
  relations = []
  schema_content.scan(TABLES_REGEXP).each do |table_name, table_comment, table_attributes|
    tables << dbml_tables_formatter.format(table_name:, table_comment:, table_attributes:)
  end

  schema_content.scan(RELATIONS_REGEXP).each do |from_table, to_table, column, on_delete|
    relations << dbml_relations_formatter.format(from_table:, to_table:, column:, on_delete:)
  end

  { tables:, relations: }
end