class SugarCane::ThresholdCheck

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

Constants

THRESHOLDS

Public Class Methods

key() click to toggle source
# File lib/sugarcane/threshold_check.rb, line 16
def self.key; :threshold; end
options() click to toggle source
# File lib/sugarcane/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

Public Instance Methods

normalized_limit(limit) click to toggle source
# File lib/sugarcane/threshold_check.rb, line 49
def normalized_limit(limit)
  Float(limit)
rescue ArgumentError
  value_from_file(limit)
end
thresholds() click to toggle source
# File lib/sugarcane/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) click to toggle source
# File lib/sugarcane/threshold_check.rb, line 55
def value_from_file(file)
  begin
    contents = SugarCane::File.contents(file).scan(/\d+\.?\d*/).first.to_f
  rescue Errno::ENOENT
    UnavailableValue.new
  end
end
violations() click to toggle source
# File lib/sugarcane/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