Class: Attributor::DateTime
- Inherits:
-
Object
- Object
- Attributor::DateTime
- Includes:
- Temporal
- Defined in:
- lib/attributor/types/date_time.rb
Class Method Summary collapse
- .example(context = nil, options: {}) ⇒ Object
- .json_schema_string_format ⇒ Object
- .load(value, context = Attributor::DEFAULT_ROOT_CONTEXT, **_options) ⇒ Object
- .native_type ⇒ Object
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(Randgen.date, context) end |
.json_schema_string_format ⇒ Object
33 34 35 |
# File 'lib/attributor/types/date_time.rb', line 33 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 |
# File 'lib/attributor/types/date_time.rb', line 19 def self.load(value, context = Attributor::DEFAULT_ROOT_CONTEXT, **) # 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.new(context: context, from: value.class, encoding: 'DateTime', value: value) end end |
.native_type ⇒ Object
11 12 13 |
# File 'lib/attributor/types/date_time.rb', line 11 def self.native_type ::DateTime end |