Class: Ronin::Core::Params::Types::Numeric

Inherits:
Type
  • Object
show all
Defined in:
lib/ronin/core/params/types/numeric.rb

Overview

Represents a true/false param.

Direct Known Subclasses

Float, Integer

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(min: nil, max: nil, range: nil) ⇒ Numeric

Initializes the numeric value.

Parameters:

  • min (::Integer, nil) (defaults to: nil)

    Optional minimum value for the integer param.

  • max (::Integer, nil) (defaults to: nil)

    Optional maximum value for the integer param.

  • range (Range) (defaults to: nil)

    Optional range of acceptable integers.



63
64
65
66
67
# File 'lib/ronin/core/params/types/numeric.rb', line 63

def initialize(min: nil, max: nil, range: nil)
  @min   = min
  @max   = max
  @range = range
end

Instance Attribute Details

#max::Integer? (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The optional maximum value.

Returns:

  • (::Integer, nil)


42
43
44
# File 'lib/ronin/core/params/types/numeric.rb', line 42

def max
  @max
end

#min::Integer? (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The optional minimum value.

Returns:

  • (::Integer, nil)


35
36
37
# File 'lib/ronin/core/params/types/numeric.rb', line 35

def min
  @min
end

#rangeRange? (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The optional range of acceptable numbers.

Returns:

  • (Range, nil)


49
50
51
# File 'lib/ronin/core/params/types/numeric.rb', line 49

def range
  @range
end

Instance Method Details

#coerce(value) ⇒ ::Integer, ::Float

This method is abstract.

Validates a numeric value.

Parameters:

  • value (::Integer, ::Float)

    The value to validate.

Returns:

  • (::Integer, ::Float)

    The validated value.

Raises:



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/ronin/core/params/types/numeric.rb', line 84

def coerce(value)
  if @range
    unless @range.include?(value)
      raise(ValidationError,"value is not within the range of acceptable values #{@range.begin}-#{@range.end} (#{value.inspect})")
    end
  else
    if @min && (value < @min)
      raise(ValidationError,"value is below minimum value of #{@min} (#{value.inspect})")
    end

    if @max && (value > @max)
      raise(ValidationError,"value is above maximum value of #{@max} (#{value.inspect})")
    end
  end

  return value
end