Class: Compel::Coercion::Time
- Defined in:
- lib/compel/coercion/types/time.rb
Instance Attribute Summary collapse
-
#format ⇒ Object
readonly
Returns the value of attribute format.
Attributes inherited from Type
Instance Method Summary collapse
Methods inherited from Type
Constructor Details
This class inherits a constructor from Compel::Coercion::Type
Instance Attribute Details
#format ⇒ Object (readonly)
Returns the value of attribute format.
6 7 8 |
# File 'lib/compel/coercion/types/time.rb', line 6 def format @format end |
Instance Method Details
#build_error_result ⇒ Object
27 28 29 30 31 |
# File 'lib/compel/coercion/types/time.rb', line 27 def build_error_result custom_error = "'#{value}' is not a parsable time with format: #{format}" Result.new(nil, value, self.class, custom_error) end |
#coerce_value ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/compel/coercion/types/time.rb', line 8 def coerce_value @format = [:format] || '%FT%T' if value.is_a?(::Time) @value = value.strftime(format) end coerced = ::Time.strptime(value, format) if coerced.strftime(format) == value return coerced end build_error_result rescue build_error_result end |