class Attributor::Time

Public Class Methods

example(context = nil, options: {}) click to toggle source
# File lib/attributor/types/time.rb, line 11
def self.example(context = nil, options: {})
  load(Faker::Time.between(from: ::DateTime.now - 1, to: ::DateTime.now), context)
end
json_schema_string_format() click to toggle source
# File lib/attributor/types/time.rb, line 39
def self.json_schema_string_format
  :time
end
load(value, context = Attributor::DEFAULT_ROOT_CONTEXT, **_options) click to toggle source
# File lib/attributor/types/time.rb, line 15
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
native_type() click to toggle source
# File lib/attributor/types/time.rb, line 7
def self.native_type
  ::Time
end
parse(value, context) click to toggle source
# File lib/attributor/types/time.rb, line 24
def self.parse(value, context)
  case value
  when ::Integer
    ::Time.at(value)
  when ::String
    begin
      ::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