Class: Attributor::Date
- Inherits:
-
Object
- Object
- Attributor::Date
- Includes:
- Type
- Defined in:
- lib/attributor/types/date.rb
Class Method Summary collapse
- .dump(value, **opts) ⇒ Object
- .example(context = nil, options: {}) ⇒ Object
- .load(value, context = Attributor::DEFAULT_ROOT_CONTEXT, **options) ⇒ Object
- .native_type ⇒ Object
Methods included from Type
Class Method Details
.dump(value, **opts) ⇒ Object
34 35 36 |
# File 'lib/attributor/types/date.rb', line 34 def self.dump(value,**opts) value && value.iso8601 end |
.example(context = nil, options: {}) ⇒ Object
12 13 14 |
# File 'lib/attributor/types/date.rb', line 12 def self.example(context=nil, options: {}) return self.load(/[:date:]/.gen, context) end |
.load(value, context = Attributor::DEFAULT_ROOT_CONTEXT, **options) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/attributor/types/date.rb', line 16 def self.load(value,context=Attributor::DEFAULT_ROOT_CONTEXT, **) return value if value.is_a?(self.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 => e 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_type ⇒ Object
8 9 10 |
# File 'lib/attributor/types/date.rb', line 8 def self.native_type return ::Date end |