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


15
16
17
# File 'lib/attributor/types/date_time.rb', line 15

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

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


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

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?(self.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 => e
    raise Attributor::DeserializationError, context: context, from: value.class, encoding: "DateTime" , value: value
  end
end

.native_typeObject


11
12
13
# File 'lib/attributor/types/date_time.rb', line 11

def self.native_type
  return ::DateTime
end