module RSchema::Coercers::Date

Coerces strings into `Date` objects using `Date.parse`

Public Instance Methods

build(_schema) click to toggle source
# File lib/rschema/coercers/date.rb, line 10
def build(_schema)
  self
end
call(value) click to toggle source
# File lib/rschema/coercers/date.rb, line 14
def call(value)
  case value
  when ::Date then Result.success(value)
  when ::String then coerce_string(value)
  else Result.failure
  end
end
will_affect?(value) click to toggle source
# File lib/rschema/coercers/date.rb, line 22
def will_affect?(value)
  !value.is_a?(::Date)
end

Private Instance Methods

coerce_string(str) click to toggle source
# File lib/rschema/coercers/date.rb, line 28
def coerce_string(str)
  date = begin
           ::Date.parse(str)
         rescue
           nil
         end
  date ? Result.success(date) : Result.failure
end