Class: Message::Recorder

Inherits:
Object show all
Defined in:
lib/message-recorder/recorder.rb

Overview

Example:

recorder = Message::Recorder.new
recorder.record.downcase.intern
recorder.before do |message_call|
  p message_call.class # => Message::Recorder::MessageCall
  !(message_call.subject.nil? or message_callsubject.empty?)
end
recorder.after do |message_call|
  p message_call.class # => Message::Recorder::MessageCall
  p message_call.return_value
end
recorder.send_to(nil)        # => nil
recorder.send_to("")         # => nil
recorder.send_to("Mr_Henry") # => :mr_henry

Defined Under Namespace

Classes: BranchingMock, Chain, ChainingMock, Collector, Message, MessageCall

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#after_branch_filtersObject

:nodoc:



134
135
136
# File 'lib/message-recorder/recorder.rb', line 134

def after_branch_filters
  @after_branch_filters
end

#after_call_filtersObject

:nodoc:



132
133
134
# File 'lib/message-recorder/recorder.rb', line 132

def after_call_filters
  @after_call_filters
end

#after_chain_filtersObject

:nodoc:



136
137
138
# File 'lib/message-recorder/recorder.rb', line 136

def after_chain_filters
  @after_chain_filters
end

#before_branch_filtersObject

:nodoc:



133
134
135
# File 'lib/message-recorder/recorder.rb', line 133

def before_branch_filters
  @before_branch_filters
end

#before_call_filtersObject

:nodoc:



131
132
133
# File 'lib/message-recorder/recorder.rb', line 131

def before_call_filters
  @before_call_filters
end

#before_chain_filtersObject

:nodoc:



135
136
137
# File 'lib/message-recorder/recorder.rb', line 135

def before_chain_filters
  @before_chain_filters
end

Instance Method Details

#after(*options, &block) ⇒ Object

AFTER CALL FILTER:

Takes a block with one argument of type Message::Recorder::MessageCall

recorder.after { |message_call| ... }
recorder.after :call { |message_call| ... }
recorder.after :call, :my_filter_method
def my_filter_method(message_call)
  ...
end

if the block returns false the chain will be broken.

AFTER CHAIN FILTER:

Takes a block with one argument of type Message::Recorder::Message

recorder.after :chain { |message| ... }
recorder.after :chain, :my_filter_method
def my_filter_method(message)
  ...
end

AFTER BRANCH FILTER:

Takes a block with two arguments of type Message::Recorder::Collector

recorder.after :branch { |original_collector, new_collector| ... }
recorder.after :branch, :my_filter_method
def my_filter_method(original_collector, new_collector)
  ...
end


107
108
109
110
111
112
113
114
115
116
117
# File 'lib/message-recorder/recorder.rb', line 107

def after(*options,&block)
  type     = options.shift if [:call, :chain, :branch].include? options.first
  type   ||= :call
  filter   = block 
  filter ||= method(options.shift)
  case type
  when :call   then after_call_filters   << filter
  when :chain  then after_chain_filters  << filter
  when :branch then after_branch_filters << filter
  end
end

#before(*options, &block) ⇒ Object

BEFORE CALL FILTER:

Takes a block with one argument of type Message::Recorder::MessageCall

recorder.before { |message_call| ... }
recorder.before :call { |message_call| ... }
recorder.before :call, :my_filter_method
def my_filter_method(message_call)
  ...
end

if the block returns false the chain will be broken.

BEFORE CHAIN FILTER:

Takes a block with one argument of type Message::Recorder::Message

recorder.before :chain { |message| ... }
recorder.before :chain, :my_filter_method
def my_filter_method(message)
  ...
end

BEFORE BRANCH FILTER:

Takes a block with one argument of type Message::Recorder::Collector

recorder.before :branch { |collector| ... }
recorder.before :branch, :my_filter_method
def my_filter_method(collector)
  ...
end


67
68
69
70
71
72
73
74
75
76
77
# File 'lib/message-recorder/recorder.rb', line 67

def before(*options,&block)
  type     = options.shift if [:call, :chain, :branch].include? options.first
  type   ||= :call
  filter   = block 
  filter ||= method(options.shift)
  case type
  when :call   then before_call_filters   << filter
  when :chain  then before_chain_filters  << filter
  when :branch then before_branch_filters << filter
  end
end

#collectorObject

:nodoc:



202
203
204
# File 'lib/message-recorder/recorder.rb', line 202

def collector # :nodoc:
  @collector ||= ::Message::Recorder::Collector.new(:recorder => self)
end

#filter_after_branch(original, branch) ⇒ Object

:nodoc:



184
185
186
187
188
# File 'lib/message-recorder/recorder.rb', line 184

def filter_after_branch(original, branch) # :nodoc:
  after_branch_filters.each do |filter|
    filter.call(original, branch)
  end
end

#filter_after_call(message_call) ⇒ Object

:nodoc:



170
171
172
173
174
175
176
# File 'lib/message-recorder/recorder.rb', line 170

def filter_after_call(message_call) # :nodoc:
  after_call_filters.each do |filter|
    should_break = filter.call(message_call)
    return false if should_break === false
  end
  return true
end

#filter_after_chain(message) ⇒ Object

:nodoc:



196
197
198
199
200
# File 'lib/message-recorder/recorder.rb', line 196

def filter_after_chain(message) # :nodoc:
  after_chain_filters.each do |filter|
    filter.call(message)
  end
end

#filter_before_branch(collector) ⇒ Object

:nodoc:



178
179
180
181
182
# File 'lib/message-recorder/recorder.rb', line 178

def filter_before_branch(collector) # :nodoc:
  before_branch_filters.each do |filter|
    filter.call(collector)
  end
end

#filter_before_call(message_call) ⇒ Object

:nodoc:



162
163
164
165
166
167
168
# File 'lib/message-recorder/recorder.rb', line 162

def filter_before_call(message_call) # :nodoc:
  before_call_filters.each do |filter|
    should_break = filter.call(message_call)
    return false if should_break === false
  end
  return true
end

#filter_before_chain(message) ⇒ Object

:nodoc:



190
191
192
193
194
# File 'lib/message-recorder/recorder.rb', line 190

def filter_before_chain(message) # :nodoc:
  before_chain_filters.each do |filter|
    filter.call(collector)
  end
end

#recordObject

#record returns a mock object which will store all the messages you send to it.



120
121
122
123
# File 'lib/message-recorder/recorder.rb', line 120

def record
  @collector = nil
  collector.collect_messages
end

#send_to(subject) ⇒ Object

#send_to will send the recorded messages to the #subject.



126
127
128
# File 'lib/message-recorder/recorder.rb', line 126

def send_to(subject)
  collector.send_to(subject, self)
end