lib/compel/coercion/time.rb in compel-0.1.3 vs lib/compel/coercion/time.rb in compel-0.2.0

- old
+ new

@@ -1,11 +1,28 @@ module Compel module Coercion class Time < Type - def coerce - ::Time.parse(value) + def coerce! + format = options[: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 + + fail + + rescue + raise \ + Compel::TypeError, + "'#{value}' is not a parsable time with format: #{format}" end end end