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

Constant Summary collapse

THRESHOLDS =
{
  lt:  :<,
  lte: :<=,
  eq:  :==,
  gte: :>=,
  gt:  :>
}

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



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

def self.key; :threshold; end

.optionsObject



17
18
19
20
21
22
23
24
# File 'lib/cane/threshold_check.rb', line 17

def self.options
  THRESHOLDS.each_with_object({}) do |(key, value), h|
    h[key] = ["Check the number in FILE is #{value} to THRESHOLD " +
              "(a number or another file name)",
                variable: "FILE,THRESHOLD",
                type:     Array]
  end
end

Instance Method Details

#normalized_limit(limit) ⇒ Object



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

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

#thresholdsObject



63
64
65
66
67
68
69
# File 'lib/cane/threshold_check.rb', line 63

def thresholds
  THRESHOLDS.map do |k, v|
    opts.fetch(k, []).map do |x|
      x.unshift(v)
    end
  end.reduce(:+)
end

#value_from_file(file) ⇒ Object



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

def value_from_file(file)
  begin
    contents = Cane::File.contents(file).scan(/\d+\.?\d*/).first.to_f
  rescue Errno::ENOENT
    UnavailableValue.new
  end
end

#violationsObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/cane/threshold_check.rb', line 26

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