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
17
# File 'lib/cane/threshold_check.rb', line 10

def self.options
  {
    gte: ["Check the number in FILE is >= to THRESHOLD " +
          "(a number or another file name)",
            variable: "FILE,THRESHOLD",
            type:     Array]
  }
end

Instance Method Details

#normalized_limit(limit) ⇒ Object



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

def normalized_limit(limit)
  Float(limit)
rescue ArgumentError
  value_from_file(limit)
end

#thresholdsObject



56
57
58
59
60
# File 'lib/cane/threshold_check.rb', line 56

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

#value_from_file(file) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/cane/threshold_check.rb', line 48

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

#violationsObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/cane/threshold_check.rb', line 19

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

    if !limit.real?
      {
        description: 'Quality threshold could not be read',
        label:       "%s is not a number or a file" % [
          threshold
        ]
      }
    elsif !value.send(operator, limit)
      {
        description: 'Quality threshold crossed',
        label:       "%s is %s, should be %s %s" % [
          file, value, operator, limit
        ]
      }
    end
  end.compact
end