Class: Attributor::Date

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

Class Method Summary collapse

Methods inherited from Temporal

dump, family

Methods included from Type

included

Class Method Details

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



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

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

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



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

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_date if value.respond_to?(:to_date)

  case value
  when ::String
    begin
      return ::Date.parse(value)
    rescue ArgumentError
      raise Attributor::DeserializationError, context: context, from: value.class, encoding: 'Date', value: value
    end
  else
    raise CoercionError, context: context, from: value.class, to: self, value: value
  end
end

.native_typeObject



5
6
7
# File 'lib/attributor/types/date.rb', line 5

def self.native_type
  ::Date
end