Class: AutoAlert::Checker

Inherits:
Object
  • Object
show all
Defined in:
lib/auto_alert/checker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(alert_kind, raise_condition, resolve_condition, reraise_condition = false, message_builder) ⇒ Checker

Returns a new instance of Checker.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/auto_alert/checker.rb', line 5

def initialize(alert_kind, raise_condition, resolve_condition, reraise_condition = false, message_builder)
  @kind = alert_kind
  @raise_condition = normalize_check_condition raise_condition
  @resolve_condition =
    resolve_condition ?
      normalize_check_condition(resolve_condition) :
      ->(a) { not @raise_condition.call(a) }
  @reraise_condition = if !reraise_condition # If nil or false
      nil
    elsif reraise_condition == true
      @raise_condition
    else
      normalize_check_condition reraise_condition
    end
  @message_builder = message_builder
end

Instance Attribute Details

#kindObject (readonly)

Returns the value of attribute kind.



3
4
5
# File 'lib/auto_alert/checker.rb', line 3

def kind
  @kind
end

Instance Method Details

#check(alertable) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/auto_alert/checker.rb', line 22

def check(alertable)
  existing_alert = alertable.send("#{@kind}_alert")

  case [existing_alert, @reraise_condition, existing_alert&.resolved]

  # Check for alert for the first time
  in nil, _, _
    if @raise_condition.call(alertable)
      alertable.alerts.create(kind: @kind, message: message(alertable), resolved: false)
    end

  # Check if alert should be resolveed
  in alert, _, false
    alert.update(resolved: true) if @resolve_condition.call(alertable)

  # Resolved alert should not be re-raised
  in alert, nil, true
    return

  # Check if resolved alert should be re-raised
  in alert, reraise_condition, true
    if reraise_condition.call(alertable)
      alert.update(message: message(alertable), resolved: false)
    end

  else
    return
  end
end

#message(alertable) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/auto_alert/checker.rb', line 52

def message(alertable)
  case @message_builder
  in Proc => p
    p.call(alertable)
  in Symbol => s if alertable.respond_to?(s, true)
    alertable.send(@message_builder)
  else
    @message_builder
  end
end