h1. message recorder h2. → 'message-recorder' h2. What Message::Recorder is a tool to record messages send to Ruby objects. h2. Installing
sudo gem install message-recorder
h2. The basics The rdocs are "here":http://message-recorde.rubyforge.org/rdoc/ This snippet will first record a downcase message and after that it will replay the recorded messages on some objects.
recorder = Message::Recorder.new
recorder.record.downcase
recorder.send_to("HELLO") # => ["hello"]
recorder.send_to("WORLD") # => ["world"]
This is an example of branching. the with_results method accepts a block which is used to define multiple message chains at once.
recorder = Message::Recorder.new
recorder.record.downcase.with_results do
  intern
  capitalize
end

recorder.send_to("HELLO") # => [:hello, "Hello"]
Here we use two before filters to make sure that we get a non-nil adn non-empty subject. And we use an after filter to print the result of each message call.
recorder = Message::Recorder.new
recorder.record.strip.downcase
recorder.before do |message_call|
  message_call.subject.nil? == false
end
recorder.before do |message_call|
  message_call.subject.empty? == false
end
recorder.after do |message_call|
  puts "#{message_call.method_name}: #{message_call.return_value.inspect}"
end
recorder.send_to(nil)           # => nil
recorder.send_to("")            # => nil
recorder.send_to("Mr. Henry")   # => "mr. henry"
recorder.send_to(" Mr. Simon ") # => "mr. henry"
Print out:
strip: "Mr. Henry"
downcase: "mr. henry"
strip: "Mr. Simon"
downcase: "mr. simon"
h2. Demonstration of usage This is a more complex example with nested branches.
recorder = Message::Recorder.new
recorder.record.compact.with_result do
  collect { |s| s.intern }
  collect { |s| s.capitalize }.with_results do
    collect { |s| s.concat " world" }
    collect { |s| s.concat " simon" }
  end.with_results do
    collect { |s| s.concat "!" }
    collect { |s| s.concat "?" }
  end
end

results = recorder.send_to([nil, "hello", nil, "bye", nil])
results == [ # the result is ...
  [:hello, :bye],
  ["Hello world!", "Bye world!"],
  ["Hello simon!", "Bye simon!"],
  ["Hello world?", "Bye world?"],
  ["Hello simon?", "Bye simon?"]
]
h2. License This code is free to use under the terms of the MIT license. h2. Contact Comments are welcome. Send an email to "Simon Menke":mailto:simon@5xm.org