Class: WhoopsNotifier::Strategy

Inherits:
Object
  • Object
show all
Defined in:
lib/whoops_notifier/strategy.rb

Overview

Strategies are responsible for building Reports and determining whether a a Report should be ignored.

Each strategy contains any number of report builders and ignore criteria.

Each report builder and ignore criteria takes a name. This makes adding a report builder or ignore criteria more like adding a method - the name makes it easier to see the intention of the code. It also makes it easier to get useful info when you inspect the strategy.

Strategies use call to actually apply modifiers and criteria for the same reason that Rack uses call. Conceivably, you could add a strategy to the notifier with WhoopsNotifier.strategies = lambda{ |report, evidence|

report.details = evidence[:detail]

} or something along those lines.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Strategy

Returns a new instance of Strategy.



23
24
25
26
27
28
# File 'lib/whoops_notifier/strategy.rb', line 23

def initialize(name)
  self.name             = name
  self.ignore_criteria  = []
  self.report_builders = []
  WhoopsNotifier.strategies[name] = self
end

Instance Attribute Details

#ignore_criteriaObject

Returns the value of attribute ignore_criteria.



21
22
23
# File 'lib/whoops_notifier/strategy.rb', line 21

def ignore_criteria
  @ignore_criteria
end

#nameObject

Returns the value of attribute name.



21
22
23
# File 'lib/whoops_notifier/strategy.rb', line 21

def name
  @name
end

#report_buildersObject

Returns the value of attribute report_builders.



21
22
23
# File 'lib/whoops_notifier/strategy.rb', line 21

def report_builders
  @report_builders
end

Instance Method Details

#add_ignore_criteria(name, &block) ⇒ Object

block takes one param, the investigator’s report



51
52
53
54
# File 'lib/whoops_notifier/strategy.rb', line 51

def add_ignore_criteria(name, &block)
  give_name(name, block)
  @ignore_criteria << block
end

#add_report_builder(name, &block) ⇒ Object

block should take two params, the report and the evidence use evidence to build the report



45
46
47
48
# File 'lib/whoops_notifier/strategy.rb', line 45

def add_report_builder(name, &block)
  give_name(name, block)
  @report_builders << block
end

#call(report, evidence) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/whoops_notifier/strategy.rb', line 30

def call(report, evidence)
  report_builders.each do |report_modifier|
    report_modifier.call(report, evidence)
  end
  
  ignore_criteria.each do |ignore_criterion|
    if ignore_criterion.call(report, evidence)
      report.ignore = true
      break
    end
  end
end

#give_name(name, block) ⇒ Object



56
57
58
59
60
61
# File 'lib/whoops_notifier/strategy.rb', line 56

def give_name(name, block)
  class << block
    attr_accessor :name
  end
  block.name = name
end

#inspectObject



63
64
65
# File 'lib/whoops_notifier/strategy.rb', line 63

def inspect
  "#{name}\nreport modifiers: #{report_builders.collect{|r| r.name}.join(", ")}\nignore criteria: #{ignore_criteria.collect{|i| i.name}.join(", ")}"
end