Sha256: 2a5603267e59169a15ff5a5dc0327fa63190245b291a1703b11f0ed548701123

Contents?: true

Size: 1.49 KB

Versions: 8

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, **)
          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

8 entries across 8 versions & 1 rubygems

Version Path
avromatic-2.2.4 lib/avromatic/model/types/abstract_timestamp_type.rb
avromatic-2.2.3 lib/avromatic/model/types/abstract_timestamp_type.rb
avromatic-2.2.2 lib/avromatic/model/types/abstract_timestamp_type.rb
avromatic-2.2.1 lib/avromatic/model/types/abstract_timestamp_type.rb
avromatic-2.2.0 lib/avromatic/model/types/abstract_timestamp_type.rb
avromatic-2.1.0 lib/avromatic/model/types/abstract_timestamp_type.rb
avromatic-2.0.1 lib/avromatic/model/types/abstract_timestamp_type.rb
avromatic-2.0.0 lib/avromatic/model/types/abstract_timestamp_type.rb