Class: MonkeyLens::Policy

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

Defined Under Namespace

Classes: Decision, Waiver

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(waivers: []) ⇒ Policy

Returns a new instance of Policy.



102
103
104
105
106
# File 'lib/monkey_lens/policy.rb', line 102

def initialize(waivers: [])
  @waivers = waivers.map do |waiver|
    waiver.is_a?(Waiver) ? waiver : Waiver.new(**symbolize(waiver))
  end.freeze
end

Instance Attribute Details

#waiversObject (readonly)

Returns the value of attribute waivers.



88
89
90
# File 'lib/monkey_lens/policy.rb', line 88

def waivers
  @waivers
end

Class Method Details

.from_hash(data) ⇒ Object



97
98
99
100
# File 'lib/monkey_lens/policy.rb', line 97

def self.from_hash(data)
  values = data["waivers"] || data[:waivers] || []
  new(waivers: values)
end

.load(path) ⇒ Object



90
91
92
93
94
95
# File 'lib/monkey_lens/policy.rb', line 90

def self.load(path)
  data = YAML.safe_load_file(path, permitted_classes: [Date], aliases: false) || {}
  from_hash(data)
rescue Psych::Exception => error
  raise ConfigurationError, "invalid policy file #{path}: #{error.message}"
end

Instance Method Details

#evaluate(result, threshold: "high", today: Date.today) ⇒ Object



108
109
110
111
112
113
114
115
116
117
# File 'lib/monkey_lens/policy.rb', line 108

def evaluate(result, threshold: "high", today: Date.today)
  Change::LEVELS.fetch(threshold.to_s)
  effective = []
  waived = []
  result.changes.each do |change|
    waiver = waivers.find { |candidate| candidate.match?(change, today:) }
    waiver ? waived << {change:, waiver:} : effective << change
  end
  Decision.new(result:, effective_changes: effective, waived_changes: waived, threshold:)
end