Sha256: ef37a36a28afe18dea1630e47b7c1455eb13e044568c08015200db77bb431d84

Contents?: true

Size: 1.49 KB

Versions: 16

Compression:

Stored size: 1.49 KB

Contents

# frozen_string_literal: true

require 'active_support/time_with_zone'

module Avromatic
  module Model
    module Types
      class AbstractTimestampType < AbstractType
        VALUE_CLASSES = [::Time].freeze
        INPUT_CLASSES = [::Time, ::DateTime, ::ActiveSupport::TimeWithZone].freeze

        def value_classes
          VALUE_CLASSES
        end

        def input_classes
          INPUT_CLASSES
        end

        def coerce(input)
          if input.nil? || coerced?(input)
            input
          elsif input.is_a?(::Time) || input.is_a?(::DateTime)
            coerce_time(input)
          else
            raise ArgumentError.new("Could not coerce '#{input.inspect}' to #{name}")
          end
        end

        def coercible?(input)
          input.nil? || input.is_a?(::Time) || input.is_a?(::DateTime)
        end

        def coerced?(value)
          # ActiveSupport::TimeWithZone overrides is_a? is to make it look like a Time
          # even though it's not which can lead to unexpected behavior if we don't force
          # a coercion
          value.is_a?(::Time) && value.class != ActiveSupport::TimeWithZone && truncated?(value)
        end

        def serialize(value, _strict)
          value
        end

        private

        def truncated?(_value)
          raise "#{__method__} must be overridden by #{self.class.name}"
        end

        def coerce_time(_input)
          raise "#{__method__} must be overridden by #{self.class.name}"
        end
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
avromatic-5.2.0 lib/avromatic/model/types/abstract_timestamp_type.rb
avromatic-5.1.1 lib/avromatic/model/types/abstract_timestamp_type.rb
avromatic-5.1.0 lib/avromatic/model/types/abstract_timestamp_type.rb
avromatic-5.0.0 lib/avromatic/model/types/abstract_timestamp_type.rb
avromatic-4.3.0 lib/avromatic/model/types/abstract_timestamp_type.rb
avromatic-4.2.0 lib/avromatic/model/types/abstract_timestamp_type.rb
avromatic-4.1.1 lib/avromatic/model/types/abstract_timestamp_type.rb
avromatic-4.1.0 lib/avromatic/model/types/abstract_timestamp_type.rb
avromatic-4.0.0 lib/avromatic/model/types/abstract_timestamp_type.rb
avromatic-3.0.2 lib/avromatic/model/types/abstract_timestamp_type.rb
avromatic-3.0.1 lib/avromatic/model/types/abstract_timestamp_type.rb
avromatic-3.0.0 lib/avromatic/model/types/abstract_timestamp_type.rb
avromatic-2.4.0 lib/avromatic/model/types/abstract_timestamp_type.rb
avromatic-2.3.0 lib/avromatic/model/types/abstract_timestamp_type.rb
avromatic-2.2.6 lib/avromatic/model/types/abstract_timestamp_type.rb
avromatic-2.2.5 lib/avromatic/model/types/abstract_timestamp_type.rb