Class: WhoopsLogger::Strategy

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

Overview

Strategies are responsible for building messages and determining whether a a message should be ignored.

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

Each message builder and ignore criteria takes a name. This makes adding a message 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 WhoopsLogger.strategies = lambda{ |message, raw_data|

message.details = raw_data[: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_logger/strategy.rb', line 23

def initialize(name)
  self.name             = name
  self.ignore_criteria  = []
  self.message_builders = []
  WhoopsLogger.strategies[name] = self
end

Instance Attribute Details

#ignore_criteriaObject

Returns the value of attribute ignore_criteria.



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

def ignore_criteria
  @ignore_criteria
end

#message_buildersObject

Returns the value of attribute message_builders.



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

def message_builders
  @message_builders
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

#add_ignore_criteria(name, &block) ⇒ Object

block takes one param, the message_creator’s message



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

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

#add_message_builder(name, &block) ⇒ Object

block should take two params, the message and the raw_data use raw_data to build the message



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

def add_message_builder(name, &block)
  give_name(name, block)
  @message_builders << block
end

#call(message, raw_data) ⇒ Object



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

def call(message, raw_data)
  message_builders.each do |message_modifier|
    message_modifier.call(message, raw_data)
  end
  
  ignore_criteria.each do |ignore_criterion|
    if ignore_criterion.call(message, raw_data)
      message.ignore = true
      break
    end
  end
end

#give_name(name, block) ⇒ Object



56
57
58
59
60
61
# File 'lib/whoops_logger/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_logger/strategy.rb', line 63

def inspect
  "#{name}\nmessage builders: #{message_builders.collect{|r| r.name}.join(", ")}\nignore criteria: #{ignore_criteria.collect{|i| i.name}.join(", ")}"
end