Sha256: 748d06ce72253ab39b82d90bf8e04d19471c381d8b1c9777e4eee0ae2c8d39c3

Contents?: true

Size: 1.52 KB

Versions: 8

Compression:

Stored size: 1.52 KB

Contents

# frozen_string_literal: true

module AnnotateRb
  module ModelAnnotator
    module ColumnAnnotation
      class DefaultValueBuilder
        def initialize(value, options)
          @value = value
          @options = options
        end

        # @return [String]
        # Returns the value to get written to file by file.puts. Strings get written to file so escaped quoted strings
        # get written as quoted. For example, if `value: "\"some_string\""` then "some_string" gets written.
        # Same with arrays, if `value: "[\"a\", \"b\", \"c\"]"` then `["a", "b", "c"]` gets written.
        #
        # @example "\"some_string\""
        # @example "NULL"
        # @example "1.2"
        def build
          if @value.is_a?(Array)
            quote_array(@value)
          else
            quote(@value)
          end
        end

        private

        def quote(value)
          return value.to_s.inspect if @options[:classes_default_to_s]&.include?(value.class.name)

          case value
          when NilClass then "NULL"
          when TrueClass then "TRUE"
          when FalseClass then "FALSE"
          when Float, Integer then value.to_s
          # BigDecimals need to be output in a non-normalized form and quoted.
          when BigDecimal then value.to_s("F")
          when String then value.inspect
          else
            value.inspect
          end
        end

        def quote_array(value)
          content = value.map { |v| quote(v) }.join(", ")
          "[" + content + "]"
        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/default_value_builder.rb
annotaterb-4.12.0 lib/annotate_rb/model_annotator/column_annotation/default_value_builder.rb
annotaterb-4.11.0 lib/annotate_rb/model_annotator/column_annotation/default_value_builder.rb
annotaterb-4.10.2 lib/annotate_rb/model_annotator/column_annotation/default_value_builder.rb
annotaterb-4.10.1 lib/annotate_rb/model_annotator/column_annotation/default_value_builder.rb
annotaterb-4.10.0 lib/annotate_rb/model_annotator/column_annotation/default_value_builder.rb
annotaterb-4.9.0 lib/annotate_rb/model_annotator/column_annotation/default_value_builder.rb
annotaterb-4.7.0 lib/annotate_rb/model_annotator/column_annotation/default_value_builder.rb