Method: PulseMeter::Mixins::Utils#assert_ranged_float!

Defined in:
lib/pulse_meter/mixins/utils.rb

#assert_ranged_float!(options, key, from, to) ⇒ Float

Ensures that hash value specified by key is can be converted to float and it is within given range. In case it can makes in-place conversion and returns the value.

Parameters:

  • options (Hash)

    hash to be looked at

  • key (Object)

    hash key

  • from (Float)

    lower bound

  • to (Float)

    upper bound

Returns:

  • (Float)

Raises:

  • (ArgumentError)

    unless value is float within given range



55
56
57
58
59
60
61
62
# File 'lib/pulse_meter/mixins/utils.rb', line 55

def assert_ranged_float!(options, key, from, to)
  f = options[key]
  raise ArgumentError, "#{key} should be defined" unless f
  raise ArgumentError, "#{key} should be float" unless f.respond_to?(:to_f)
  f = f.to_f
  raise ArgumentError, "#{key} should be between #{from} and #{to}" unless f >= from && f <= to
  options[key] = f
end