Sha256: 2f9ea8be19fe5619fd2c50c019d34a44c888b90819425143b5f68c82ba58db94

Contents?: true

Size: 996 Bytes

Versions: 11

Compression:

Stored size: 996 Bytes

Contents

require 'date'

module Attributor
  class Time
    include Temporal

    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

    def self.json_schema_string_format
      :time
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
attributor-7.1 lib/attributor/types/time.rb
attributor-7.0 lib/attributor/types/time.rb
attributor-6.5 lib/attributor/types/time.rb
attributor-6.4 lib/attributor/types/time.rb
attributor-6.3 lib/attributor/types/time.rb
attributor-6.2 lib/attributor/types/time.rb
attributor-6.1 lib/attributor/types/time.rb
attributor-6.0 lib/attributor/types/time.rb
attributor-5.7 lib/attributor/types/time.rb
attributor-5.6 lib/attributor/types/time.rb
attributor-5.5 lib/attributor/types/time.rb