Sha256: e0b6c052df29cffe8692c905247296468146cc3c38784548127ec74c6f453602

Contents?: true

Size: 1.62 KB

Versions: 2

Compression:

Stored size: 1.62 KB

Contents

# frozen_string_literal: true

module AnnotateRb
  module ModelAnnotator
    module ColumnAnnotation
      class TypeBuilder
        # Don't show limit (#) on these column types
        # Example: show "integer" instead of "integer(4)"
        NO_LIMIT_COL_TYPES = %w[integer bigint boolean].freeze

        def initialize(column, options)
          @column = ColumnWrapper.new(column)
          @options = options
        end

        # Returns the formatted column type as a string.
        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

        private

        def hide_limit?
          excludes =
            if @options[:hide_limit_column_types].blank?
              NO_LIMIT_COL_TYPES
            else
              @options[:hide_limit_column_types].split(",")
            end

          excludes.include?(@column.column_type_string)
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
annotaterb-4.3.0 lib/annotate_rb/model_annotator/column_annotation/type_builder.rb
annotaterb-4.2.0 lib/annotate_rb/model_annotator/column_annotation/type_builder.rb