Class: Cane::ThresholdCheck

Inherits:
Struct
  • Object
show all
Defined in:
lib/cane/threshold_check.rb

Overview

Configurable check that allows the contents of a file to be compared against a given value.

Defined Under Namespace

Classes: UnavailableValue

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#optsObject

Returns the value of attribute opts

Returns:

  • (Object)

    the current value of opts



7
8
9
# File 'lib/cane/threshold_check.rb', line 7

def opts
  @opts
end

Class Method Details

.keyObject



9
# File 'lib/cane/threshold_check.rb', line 9

def self.key; :threshold; end

.optionsObject



10
11
12
13
14
15
16
# File 'lib/cane/threshold_check.rb', line 10

def self.options
  {
    gte: ["If FILE contains a number, verify it is >= to THRESHOLD",
            variable: "FILE,THRESHOLD",
            type:     Array]
  }
end

Instance Method Details

#thresholdsObject



41
42
43
44
45
# File 'lib/cane/threshold_check.rb', line 41

def thresholds
  (opts[:gte] || []).map do |x|
    x.unshift(:>=)
  end
end

#value_from_file(file) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/cane/threshold_check.rb', line 33

def value_from_file(file)
  begin
    contents = Cane::File.contents(file).chomp.to_f
  rescue Errno::ENOENT
    UnavailableValue.new
  end
end

#violationsObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/cane/threshold_check.rb', line 18

def violations
  thresholds.map do |operator, file, limit|
    value = value_from_file(file)

    unless value.send(operator, limit.to_f)
      {
        description: 'Quality threshold crossed',
        label:       "%s is %s, should be %s %s" % [
          file, value, operator, limit
        ]
      }
    end
  end.compact
end