Sha256: 310cb551bcae0c134a820aa77598d7613974ae44f4bd3d9ad2fd6138b1f4ce5c

Contents?: true

Size: 1.76 KB

Versions: 8

Compression:

Stored size: 1.76 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_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

        # 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

8 entries across 8 versions & 1 rubygems

Version Path
annotaterb-4.13.0 lib/annotate_rb/model_annotator/column_annotation/type_builder.rb
annotaterb-4.12.0 lib/annotate_rb/model_annotator/column_annotation/type_builder.rb
annotaterb-4.11.0 lib/annotate_rb/model_annotator/column_annotation/type_builder.rb
annotaterb-4.10.2 lib/annotate_rb/model_annotator/column_annotation/type_builder.rb
annotaterb-4.10.1 lib/annotate_rb/model_annotator/column_annotation/type_builder.rb
annotaterb-4.10.0 lib/annotate_rb/model_annotator/column_annotation/type_builder.rb
annotaterb-4.9.0 lib/annotate_rb/model_annotator/column_annotation/type_builder.rb
annotaterb-4.7.0 lib/annotate_rb/model_annotator/column_annotation/type_builder.rb