Sha256: c2d178648f0cafcdff93b32f3108f5ccd2e768d9b2c2b0c0264d5f6407e81732

Contents?: true

Size: 943 Bytes

Versions: 2

Compression:

Stored size: 943 Bytes

Contents

require 'date'

module Attributor
  class Time < Temporal
    include Type

    def self.native_type
      ::Time
    end

    def self.example(context = nil, options: {})
      load(Randgen.time, 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_time if value.respond_to?(:to_time)

      self.parse(value, context)
    end

    def self.parse(value, context)
      case value
      when ::Integer
        return ::Time.at(value)
      when ::String
        begin
          return ::Time.parse(value)
        rescue ArgumentError
          raise Attributor::DeserializationError.new(context: context, from: value.class, encoding: 'Time', value: value)
        end
      else
        raise CoercionError, 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/time.rb
attributor-5.3 lib/attributor/types/time.rb