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