Class: StrongCSV::Types::Integer

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

Overview

Integer type

Instance Method Summary collapse

Constructor Details

#initialize(constraint: DEFAULT_CONSTRAINT) ⇒ Integer

Returns a new instance of Integer.

Parameters:

  • constraint (Proc) (defaults to: DEFAULT_CONSTRAINT)
[View source]

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

#cast(value) ⇒ ValueResult

TODO:

Use :exception for Integer after we drop the support of Ruby 2.5

Parameters:

  • value (Object)

    Value to be casted to Integer

Returns:

[View source]

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