Class: StrongCSV::Types::Integer
- Defined in:
- lib/strong_csv/types/integer.rb
Overview
Integer type
Instance Method Summary collapse
- #cast(value) ⇒ ValueResult
-
#initialize(constraint: DEFAULT_CONSTRAINT) ⇒ Integer
constructor
A new instance of Integer.
Constructor Details
permalink #initialize(constraint: DEFAULT_CONSTRAINT) ⇒ Integer
Returns a new instance of Integer.
11 12 13 14 |
# File 'lib/strong_csv/types/integer.rb', line 11 def initialize(constraint: DEFAULT_CONSTRAINT) super() @constraint = constraint end |
Instance Method Details
permalink #cast(value) ⇒ ValueResult
TODO:
Use :exception for Integer after we drop the support of Ruby 2.5
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/strong_csv/types/integer.rb', line 19 def cast(value) int = Integer(value) if @constraint.call(int) ValueResult.new(value: int, original_value: value) else ValueResult.new(original_value: value, error_messages: ["`#{value.inspect}` does not satisfy the specified constraint"]) end rescue ArgumentError, TypeError ValueResult.new(original_value: value, error_messages: ["`#{value.inspect}` can't be casted to Integer"]) end |