Sha256: 402347817bd824b891140fd317f04bcec0a885101ac7c6603f2088063dad1c63

Contents?: true

Size: 1.39 KB

Versions: 3

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true

module AnnotateRb
  module ModelAnnotator
    module ColumnAnnotation
      class DefaultValueBuilder
        def initialize(value)
          @value = value
        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)
          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

3 entries across 3 versions & 1 rubygems

Version Path
annotaterb-4.6.0 lib/annotate_rb/model_annotator/column_annotation/default_value_builder.rb
annotaterb-4.5.0 lib/annotate_rb/model_annotator/column_annotation/default_value_builder.rb
annotaterb-4.4.1 lib/annotate_rb/model_annotator/column_annotation/default_value_builder.rb