Class: NagiosCheck::Range

Inherits:
Object
  • Object
show all
Defined in:
lib/nagios_check/range.rb

Instance Method Summary collapse

Constructor Details

#initialize(string_range) ⇒ Range

Returns a new instance of Range.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/nagios_check/range.rb', line 3

def initialize(string_range)
  if string_range.nil? || string_range.empty?
    raise RuntimeError, "Pattern should not be nil" 
  end
  @string_range = string_range
  tokens = string_range.scan(/^(@)?(([-.0-9]+|~)?:)?([-.0-9]+)?$/).first
  unless tokens
    raise RuntimeError, "Pattern should be of form [@][~][min]:max" 
  end
  @inverse = true if tokens.include? "@"
  case tokens[2]
  when nil, "" then @min = 0
  when '~' then @min = nil
  else @min = tokens[2].to_f
  end
  @max = tokens[3].nil? || tokens[3] == "" ? nil : tokens[3].to_f
end

Instance Method Details

#===(value) ⇒ Object



26
27
28
# File 'lib/nagios_check/range.rb', line 26

def ===(value)
  include?(value)
end

#include?(value) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
# File 'lib/nagios_check/range.rb', line 21

def include?(value)
  result = (@min.nil? || value >= @min) && (@max.nil? || value <= @max)
  @inverse ? not(result) : result
end

#to_sObject



30
31
32
# File 'lib/nagios_check/range.rb', line 30

def to_s
  "Range[#{@reversed ? "~" : ""}#{@inclusive ? "@" : ""}#{@min}:#{@max}]"
end