# Copyright (c) 2008 Simon Menke # # Permission is hereby granted, free of charge, to any person obtaining # a copy of this software and associated documentation files (the # "Software"), to deal in the Software without restriction, including # without limitation the rights to use, copy, modify, merge, publish, # distribute, sublicense, and/or sell copies of the Software, and to # permit persons to whom the Software is furnished to do so, subject to # the following conditions: # # The above copyright notice and this permission notice shall be # included in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. # 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 class Message::Recorder # Takes a block with one argument of type Message::Recorder::MessageCall # recorder.before { |message_call| ... } # if the block returns false the chain will be broken. def before(&block) before_filters << block end # Takes a block with one argument of type Message::Recorder::MessageCall # recorder.after { |message_call| ... } def after(&block) after_filters << block end # #record returns a mock object which will store all the messages you send to it. def record @collector = nil collector.collect_messages end # #send_to will send the recorded messages to the #subject. def send_to(subject) collector.send_to(subject, self) end attr_accessor :before_filters # :nodoc: attr_accessor :after_filters # :nodoc: def before_filters # :nodoc: @before_filters ||= [] end def after_filters # :nodoc: @before_filters ||= [] end def filter_before(message_call) # :nodoc: before_filters.each do |filter| should_break = filter.call(message_call) return false if should_break === false end return true end def filter_after(message_call) # :nodoc: after_filters.each do |filter| filter.call(message_call) end end def collector # :nodoc: @collector ||= ::Message::Recorder::Collector.new end end