Class: HashCast::Casters::IntegerCaster

Inherits:
Object
  • Object
show all
Defined in:
lib/hash_cast/casters/integer_caster.rb

Constant Summary collapse

DEFAULT_MIN_VALUE =
2**31 * -1
DEFAULT_MAX_VALUE =
2**31

Class Method Summary collapse

Class Method Details

.cast(value, attr_name, options = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/hash_cast/casters/integer_caster.rb', line 6

def self.cast(value, attr_name, options = {})
  integer_value = get_integer_value(value)

  if integer_value < options.fetch(:min_value, DEFAULT_MIN_VALUE)
    raise HashCast::Errors::CastingError, "should be within allowed range"
  end

  if integer_value > options.fetch(:max_value, DEFAULT_MAX_VALUE)
    raise HashCast::Errors::CastingError, "should be within allowed range"
  end

  integer_value
end