module RSchema::Coercers::Time

Coerces `String`s to `Time`s using `Time.parse`

Public Instance Methods

build(_schema) click to toggle source
# File lib/rschema/coercers/time.rb, line 10
def build(_schema)
  self
end
call(value) click to toggle source
# File lib/rschema/coercers/time.rb, line 14
def call(value)
  case value
  when ::Time 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/time.rb, line 22
def will_affect?(value)
  !value.is_a?(Time)
end

Private Instance Methods

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