Class: Highway::Steps::Types::Number

Inherits:
Any
  • Object
show all
Defined in:
lib/highway/steps/types/number.rb

Overview

This class represents a numeric parameter type. It can be used in parameters which have an integer or float value.

Instance Method Summary collapse

Methods inherited from Any

#initialize, #typecheck_and_validate, #validate

Constructor Details

This class inherits a constructor from Highway::Steps::Types::Any

Instance Method Details

#typecheck(value) ⇒ Integer, ...

Typecheck and coerce a value if possible.

This method returns a typechecked and coerced value or ‘nil` if value has invalid type and can’t be coerced.

Parameters:

  • value (Object)

    A value.

Returns:

  • (Integer, Float, nil)
[View source]

26
27
28
29
30
31
32
# File 'lib/highway/steps/types/number.rb', line 26

def typecheck(value)
  case value
    when ::Numeric then value
    when ::String && value.to_i.to_s == value then value.to_i
    when ::String && value.to_f.to_s == value then value.to_f
  end
end