Module: Formatters::FieldsFormatter

Includes:
DefaultFieldFormatterHelper
Included in:
DbmlTablesFormatter
Defined in:
lib/schema_to_dbml/formatters/fields_formatter.rb

Constant Summary collapse

COMMENT_MAPPER =
[
  { from: "'", to: "\\\\'" },
  { from: '\"', to: '"' }
].freeze
TYPE_MAPPER =
{
  string: 'varchar',
  integer: 'int',
  boolean: 'bool',
  datetime: 'timestamp'
}.freeze

Constants included from DefaultFieldFormatterHelper

DefaultFieldFormatterHelper::DEFAULT_ARRAY_REGEX, DefaultFieldFormatterHelper::DEFAULT_BOOLEAN_REGEX, DefaultFieldFormatterHelper::DEFAULT_HASH_REGEX, DefaultFieldFormatterHelper::DEFAULT_LAMBDA_REGEX, DefaultFieldFormatterHelper::DEFAULT_NUMBER_REGEX, DefaultFieldFormatterHelper::DEFAULT_PATTERNS, DefaultFieldFormatterHelper::DEFAULT_STRING_REGEX

Instance Method Summary collapse

Instance Method Details

#format_comment(comment:) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/schema_to_dbml/formatters/fields_formatter.rb', line 47

def format_comment(comment:)
  return '' if comment.to_s.empty?

  note = comment.dup
  COMMENT_MAPPER.each do |mapper|
    note.gsub!(mapper[:from], mapper[:to])
  end

  note = "#{note} " if note[-1, 1] == "'"

  "note: '''#{note}'''"
end

#format_default(default:) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/schema_to_dbml/formatters/fields_formatter.rb', line 31

def format_default(default:)
  return '' if default.to_s.empty?

  DEFAULT_PATTERNS.each do |pattern, replacement|
    default = default.gsub(pattern, replacement)
  end

  default
end

#format_null(null:) ⇒ Object



41
42
43
44
45
# File 'lib/schema_to_dbml/formatters/fields_formatter.rb', line 41

def format_null(null:)
  return '' if null.to_s.empty?

  'not null'
end

#format_type(type:, array:, limit:) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/schema_to_dbml/formatters/fields_formatter.rb', line 20

def format_type(type:, array:, limit:)
  return '' if type.to_s.empty?

  parsed = TYPE_MAPPER[type.to_sym] || type.to_s

  return "#{parsed}[]" if array == 'true'
  return "#{parsed}(#{limit})" if limit

  parsed
end