Class: Attributor::Time
- Includes:
- Type
- Defined in:
- lib/attributor/types/time.rb
Class Method Summary collapse
- .example(context = nil, options: {}) ⇒ Object
- .load(value, context = Attributor::DEFAULT_ROOT_CONTEXT, **_options) ⇒ Object
- .native_type ⇒ Object
- .parse(value, context) ⇒ Object
Methods included from Type
Methods inherited from Temporal
Class Method Details
.example(context = nil, options: {}) ⇒ Object
11 12 13 |
# File 'lib/attributor/types/time.rb', line 11 def self.example(context = nil, options: {}) load(Randgen.time, context) end |
.load(value, context = Attributor::DEFAULT_ROOT_CONTEXT, **_options) ⇒ Object
15 16 17 18 19 20 21 22 |
# File 'lib/attributor/types/time.rb', line 15 def self.load(value, context = Attributor::DEFAULT_ROOT_CONTEXT, **) return value if value.is_a?(native_type) return nil if value.nil? return value.to_time if value.respond_to?(:to_time) self.parse(value, context) end |
.native_type ⇒ Object
7 8 9 |
# File 'lib/attributor/types/time.rb', line 7 def self.native_type ::Time end |
.parse(value, context) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/attributor/types/time.rb', line 24 def self.parse(value, context) case value when ::Integer return ::Time.at(value) when ::String begin return ::Time.parse(value) rescue ArgumentError raise Attributor::DeserializationError.new(context: context, from: value.class, encoding: 'Time', value: value) end else raise CoercionError, context: context, from: value.class, to: self, value: value end end |