Sha256: 01202fd761d4d88b004dd88395cfe293f4c057d4a5246c9427d9e61c6c4a66ea

Contents?: true

Size: 971 Bytes

Versions: 3

Compression:

Stored size: 971 Bytes

Contents

# frozen_string_literal: true

require 'avromatic/model/types/abstract_type'

module Avromatic
  module Model
    module Types
      class DateType < AbstractType
        VALUE_CLASSES = [::Date].freeze

        def value_classes
          VALUE_CLASSES
        end

        def name
          'date'
        end

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

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

        alias_method :coerced?, :coercible?

        def serialize(value, **)
          value
        end

        def referenced_model_classes
          EMPTY_ARRAY
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
avromatic-2.2.4 lib/avromatic/model/types/date_type.rb
avromatic-2.2.3 lib/avromatic/model/types/date_type.rb
avromatic-2.2.2 lib/avromatic/model/types/date_type.rb