Sha256: 671c8ba5b1bfc397671d689a2b2e15573b9a77cc62bae5662d81f0b820564f61

Contents?: true

Size: 799 Bytes

Versions: 2

Compression:

Stored size: 799 Bytes

Contents

require 'date'

module Attributor
  class Date < Temporal
    def self.native_type
      ::Date
    end

    def self.example(context = nil, options: {})
      load(Randgen.date, context)
    end

    def self.load(value, context = Attributor::DEFAULT_ROOT_CONTEXT, **_options)
      return value if value.is_a?(native_type)
      return nil if value.nil?

      return value.to_date if value.respond_to?(:to_date)

      case value
      when ::String
        begin
          return ::Date.parse(value)
        rescue ArgumentError
          raise Attributor::DeserializationError.new(context: context, from: value.class, encoding: 'Date', value: value)
        end
      else
        raise CoercionError.new(context: context, from: value.class, to: self, value: value)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
attributor-5.4 lib/attributor/types/date.rb
attributor-5.3 lib/attributor/types/date.rb