Class: Attributor::Time

Inherits:
Temporal show all
Includes:
Type
Defined in:
lib/attributor/types/time.rb

Class Method Summary collapse

Methods included from Type

included

Methods inherited from Temporal

dump, family

Class Method Details

.example(context = nil, options: {}) ⇒ Object



12
13
14
# File 'lib/attributor/types/time.rb', line 12

def self.example(context=nil, options: {})
  return self.load(/[:time:]/.gen, context)
end

.load(value, context = Attributor::DEFAULT_ROOT_CONTEXT, **options) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/attributor/types/time.rb', line 16

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

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

  case value
  when ::Integer
    return ::Time.at(value)
  when ::String
    begin
      return ::Time.parse(value)
    rescue ArgumentError => e
      raise Attributor::DeserializationError, context: context, from: value.class, encoding: "Time" , value: value
    end
  else
    raise CoercionError, context: context, from: value.class, to: self, value: value
  end
end

.native_typeObject



8
9
10
# File 'lib/attributor/types/time.rb', line 8

def self.native_type
  return ::Time
end