Class: StrongCSV::Types::Float

Inherits:
Base
  • Object
show all
Defined in:
lib/strong_csv/types/float.rb

Overview

Float type

Instance Method Summary collapse

Constructor Details

#initialize(constraint: DEFAULT_CONSTRAINT) ⇒ Float

Returns a new instance of Float.

Parameters:

  • constraint (Proc) (defaults to: DEFAULT_CONSTRAINT)


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

Parameters:

  • value (Object)

    Value to be casted to Float

Returns:



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