Class: Attributor::DateTime

Inherits:
Object
  • Object
show all
Includes:
Temporal
Defined in:
lib/attributor/types/date_time.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



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

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

.json_schema_string_formatObject



34
35
36
# File 'lib/attributor/types/date_time.rb', line 34

def self.json_schema_string_format
  :'date-time'
end

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



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 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?(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
    ::DateTime.parse(value)
  rescue ArgumentError
    raise Attributor::DeserializationError.new(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
  ::DateTime
end