Class: StrongCSV::Types::Time
Overview
Time type
Instance Method Summary collapse
- #cast(value) ⇒ ValueResult
-
#initialize(format: "%Y-%m-%d") ⇒ Time
constructor
A new instance of Time.
Constructor Details
#initialize(format: "%Y-%m-%d") ⇒ Time
Returns a new instance of Time.
8 9 10 11 12 13 |
# File 'lib/strong_csv/types/time.rb', line 8 def initialize(format: "%Y-%m-%d") raise ArgumentError, "`format` must be a String" unless format.is_a?(::String) super() @format = format end |
Instance Method Details
#cast(value) ⇒ ValueResult
17 18 19 20 21 22 23 |
# File 'lib/strong_csv/types/time.rb', line 17 def cast(value) return ValueResult.new(original_value: value, error_messages: ["`#{value.inspect}` can't be casted to Time with the format `#{@format.inspect}`"]) if value.nil? ValueResult.new(value: ::Time.strptime(value.to_s, @format), original_value: value) rescue ArgumentError ValueResult.new(original_value: value, error_messages: ["`#{value.inspect}` can't be casted to Time with the format `#{@format.inspect}`"]) end |