Class: Attributor::DateTime

Inherits:
Temporal show all
Defined in:
lib/attributor/types/date_time.rb

Class Method Summary collapse

Methods inherited from Temporal

dump, family

Methods included from Type

included

Class Method Details

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



13
14
15
# File 'lib/attributor/types/date_time.rb', line 13

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

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



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/attributor/types/date_time.rb', line 17

def self.load(value, context = Attributor::DEFAULT_ROOT_CONTEXT, **_options)
  # We assume that if the value is already in the right type, we've decoded it already
  return value if value.is_a?(native_type)
  return value.to_datetime if value.respond_to?(:to_datetime)
  return nil unless value.is_a?(::String)
  # TODO: we should be able to convert not only from String but Time...etc
  # Else, we'll decode it from String.
  begin
    return ::DateTime.parse(value)
  rescue ArgumentError
    raise Attributor::DeserializationError, context: context, from: value.class, encoding: 'DateTime', value: value
  end
end

.native_typeObject



9
10
11
# File 'lib/attributor/types/date_time.rb', line 9

def self.native_type
  ::DateTime
end