# 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. # class Message::Recorder # a Proc with one argument # recorder.break_if = Proc.new { |subject| subject.nil? } # if the Proc returns false the chain will be broken. attr_writer :break_if # a Proc with three arguments # recorder.before_call = Proc.new { |subject, method, arguments| ... } attr_writer :before_call # a Proc with four arguments # recorder.after_call = Proc.new { |subject, method, arguments, return_value| ... } attr_writer :after_call def initialize # :nodoc: @collector = nil before_call = nil after_call = nil break_if = nil end # #record returns a mock object which will store all the messages you send to it. def record @collector = ::Message::Recorder::Collector.new @collector.collect_messages end # #send_to will send the recorded messages to the #subject. def send_to(subject) @collector.send_to(subject, self) end def break_if # :nodoc: @break_if ||= Proc.new { |f| false } end def before_call # :nodoc: @before_call ||= Proc.new { |f, m, args| nil } end def after_call # :nodoc: @after_call ||= Proc.new { |f, m, args, result| nil } end end