Class: StrongCSV::Types::Float
- Defined in:
- lib/strong_csv/types/float.rb
Overview
Float type
Instance Method Summary collapse
- #cast(value) ⇒ ValueResult
-
#initialize(constraint: DEFAULT_CONSTRAINT) ⇒ Float
constructor
A new instance of Float.
Constructor Details
#initialize(constraint: DEFAULT_CONSTRAINT) ⇒ Float
Returns a new instance of Float.
11 12 13 14 |
# File 'lib/strong_csv/types/float.rb', line 11 def initialize(constraint: DEFAULT_CONSTRAINT) super() @constraint = constraint end |
Instance Method Details
#cast(value) ⇒ ValueResult
TODO:
Use :exception for Float after we drop the support of Ruby 2.5
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/strong_csv/types/float.rb', line 19 def cast(value) float = Float(value) if @constraint.call(float) ValueResult.new(value: float, 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 Float"]) end |