Sha256: d23b9d021e293d3178bf51736d28e8a184b91770963c2fe6abfd87385152aa7e

Contents?: true

Size: 1.59 KB

Versions: 1

Compression:

Stored size: 1.59 KB

Contents

require "bigdecimal"
require "date"

module Timber
  module Contexts
    class DynamicValues
      class UnrecognizedObjectTypeError < StandardError; end

      include Patterns::ToJSON

      BOOLEAN_TYPES = [FalseClass, TrueClass].freeze
      DATE_TYPES    = [Date, Time].freeze
      FLOAT_TYPES   = [BigDecimal, Float].freeze
      INTEGER_TYPES = [Fixnum].freeze
      NIL_TYPES     = [NilClass].freeze
      STRING_TYPES  = [String].freeze

      def initialize(values_array = nil)
        @values_array = values_array
      end

      private
        def json_payload
          @json_payload ||= values_array.collect do |value|
            to_item(value)
          end
        end

        def to_item(value)
          {
            :name  => value[:name],
            :type  => type(value[:value]),
            :value => value[:value]
          }
        end

        def type(value)
          # Using is_a? because it checks the entire hierarchy, unlike a case statement.
          if BOOLEAN_TYPES.any? { |type| value.is_a?(type) }
            APISettings::BOOLEAN_TYPE
          elsif DATE_TYPES.any? { |type| value.is_a?(type) }
            APISettings::DATE_TYPE
          elsif FLOAT_TYPES.any? { |type| value.is_a?(type) }
            APISettings::FLOAT_TYPE
          elsif INTEGER_TYPES.any? { |type| value.is_a?(type) }
            APISettings::INTEGER_TYPE
          elsif NIL_TYPES.any? { |type| value.is_a?(type) }
            APISettings::NIL_TYPE
          else
            APISettings::STRING_TYPE
          end
        end

        def values_array
          @values_array
        end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
timberio-1.0.0.beta1 lib/timber/contexts/dynamic_values.rb