Class: SlashPort::Check

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

Overview

class SlashPort::Check

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, cmp, value) ⇒ Check

Returns a new instance of Check.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/slashport.rb', line 10

def initialize(name, cmp, value)
  @name = name
  @value = value
  @cmpstr = cmp
  case cmp
  when ">="
    @cmp = Proc.new { |v| v >= coerce(v, @value) }
  when "<="
    @cmp = Proc.new { |v| v <= coerce(v, @value) }
  when "=="
    @cmp = Proc.new { |v| v == coerce(v, @value) }
  when "!="
    @cmp = Proc.new { |v| v != coerce(v, @value) }
  when "<"
    @cmp = Proc.new { |v| v < coerce(v, @value) }
  when ">"
    @cmp = Proc.new { |v| v > coerce(v, @value) }
  else
    raise "Unknown comparison '#{cmp}'"
  end
end

Class Method Details

.new_from_string(value) ⇒ Object

Turn a string “name cmp value” into a Check. Valid cmp are <, >, <=, >=, and ==



45
46
47
48
# File 'lib/slashport.rb', line 45

def self.new_from_string(value)
  return nil unless value =~ /^([A-z0-9_-]+)\s*((?:[><=!]=)|[<>])\s*(.*)$/
  return SlashPort::Check.new($1, $2, $3)
end

Instance Method Details

#coerce(a, b) ⇒ Object

if ‘a’ is an int or float, try to convert b to the same thing



37
38
39
40
41
# File 'lib/slashport.rb', line 37

def coerce(a, b)
  return b.to_i if a.is_a?(Integer)
  return b.to_f if a.is_a?(Float)
  return b
end

#match?(attribute) ⇒ Boolean

Given an attribute, does this check match?

Returns:

  • (Boolean)


51
52
53
54
55
56
57
58
59
60
# File 'lib/slashport.rb', line 51

def match?(attribute)
  match = false
  ["data", "labels"].each do |type|
    if (attribute[type].has_key?(@name) and @cmp.call(attribute[type][@name]))
      match = true
      return match
    end
  end
  return match
end

#to_sObject

def initialize



32
33
34
# File 'lib/slashport.rb', line 32

def to_s
  return "#{@name} #{@cmpstr} #{@value}"
end