Class: AnnotateRb::ModelAnnotator::ColumnAnnotation::TypeBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/annotate_rb/model_annotator/column_annotation/type_builder.rb

Constant Summary collapse

NO_LIMIT_COL_TYPES =

Don’t show limit (#) on these column types Example: show “integer” instead of “integer(4)”

%w[integer bigint boolean].freeze

Instance Method Summary collapse

Constructor Details

#initialize(column, options, column_defaults) ⇒ TypeBuilder

Returns a new instance of TypeBuilder.



11
12
13
14
15
# File 'lib/annotate_rb/model_annotator/column_annotation/type_builder.rb', line 11

def initialize(column, options, column_defaults)
  # Passing `column_defaults` for posterity, don't actually need it here since it's not used
  @column = ColumnWrapper.new(column, column_defaults, options)
  @options = options
end

Instance Method Details

#buildObject

Returns the formatted column type as a string.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/annotate_rb/model_annotator/column_annotation/type_builder.rb', line 18

def build
  column_type = @column.column_type_string

  formatted_column_type = column_type

  is_special_type = %w[spatial geometry geography].include?(column_type)
  is_decimal_type = column_type == "decimal"

  if is_decimal_type
    formatted_column_type = "decimal(#{@column.precision}, #{@column.scale})"
  elsif is_special_type
    # Do nothing. Kept as a code fragment in case we need to do something here.
  elsif @column.limit && !@options[:format_yard]
    # Unsure if Column#limit will ever be an array. May be safe to remove.
    if !@column.limit.is_a?(Array) && !hide_limit?
      formatted_column_type = column_type + "(#{@column.limit})"
    end
  end

  formatted_column_type
end