Class: Attributor::Date

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

Class Method Summary collapse

Methods included from Type

get_memoized_collection_class, set_memoized_collection_class

Class Method Details

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



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

def self.example(context = nil, options: {})
  load(Faker::Date.in_date_period, context)
end

.json_schema_string_formatObject



33
34
35
# File 'lib/attributor/types/date.rb', line 33

def self.json_schema_string_format
  :date
end

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



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/attributor/types/date.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_date if value.respond_to?(:to_date)

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

.native_typeObject



7
8
9
# File 'lib/attributor/types/date.rb', line 7

def self.native_type
  ::Date
end