Module: CMDx::Coercions::Time
Overview
Converts various input types to Time format
Handles conversion from strings, dates, and other time-like objects to Time using Ruby’s built-in time parsing methods. Supports custom strptime formats and raises CoercionError for values that cannot be converted to Time.
Constant Summary collapse
- ANALOG_TYPES =
Types that are already time-like and don’t need conversion
%w[DateTime Time].freeze
Instance Method Summary collapse
-
#call(value, options = {}) ⇒ Time
Converts a value to a Time object.
Instance Method Details
#call(value, options = {}) ⇒ Time
Converts a value to a Time object
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/cmdx/coercions/time.rb', line 42 def call(value, = {}) return value if ANALOG_TYPES.include?(value.class.name) return value.to_time if value.respond_to?(:to_time) return ::Time.strptime(value, [:strptime]) if [:strptime] ::Time.parse(value) rescue ArgumentError, TypeError type = Locale.t("cmdx.types.time") raise CoercionError, Locale.t("cmdx.coercions.into_a", type:) end |