message-recorder 1.0.1 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Manifest.txt CHANGED
@@ -13,6 +13,7 @@ lib/message-recorder/chain.rb
13
13
  lib/message-recorder/chaining_mock.rb
14
14
  lib/message-recorder/collector.rb
15
15
  lib/message-recorder/message.rb
16
+ lib/message-recorder/message_call.rb
16
17
  lib/message-recorder/recorder.rb
17
18
  lib/message-recorder/version.rb
18
19
  log/debug.log
@@ -22,8 +23,6 @@ script/txt2html
22
23
  setup.rb
23
24
  spec/chain_spec.rb
24
25
  spec/collector_spec.rb
25
- spec/fob_spec.rb
26
- spec/message_spec.rb
27
26
  spec/spec.opts
28
27
  spec/spec_helper.rb
29
28
  tasks/deployment.rake
@@ -25,6 +25,7 @@ module Message ; end
25
25
 
26
26
  require File.join(File.dirname(__FILE__), "message-recorder", "recorder")
27
27
  require File.join(File.dirname(__FILE__), "message-recorder", "message")
28
+ require File.join(File.dirname(__FILE__), "message-recorder", "message_call")
28
29
  require File.join(File.dirname(__FILE__), "message-recorder", "chain")
29
30
  require File.join(File.dirname(__FILE__), "message-recorder", "collector")
30
31
  require File.join(File.dirname(__FILE__), "message-recorder", "branching_mock")
@@ -27,15 +27,16 @@ class Message::Recorder::Chain < ::Array # :nodoc:
27
27
  end
28
28
 
29
29
  def record(m, *args, &block)
30
- push Message::Recorder::Message.new(m, *args, &block)
30
+ push Message::Recorder::Message.new(m, args, block)
31
31
  end
32
32
 
33
33
  def send_to(subject_, recorder)
34
34
  inject(subject_) do |subject,message|
35
- return nil unless recorder.filter_before(subject, message.method_name, message.arguments)
36
- new_subject = message.send_to(subject, recorder)
37
- recorder.filter_after(subject, message.method_name, message.arguments, new_subject)
38
- new_subject
35
+ message_call = message.to_message_call(subject)
36
+ return nil unless recorder.filter_before(message_call)
37
+ message_call.execute
38
+ recorder.filter_after(message_call)
39
+ message_call.return_value
39
40
  end
40
41
  end
41
42
 
@@ -23,22 +23,14 @@
23
23
  class Message::Recorder::Message # :nodoc:
24
24
  attr_accessor :method_name, :arguments, :block
25
25
 
26
- def initialize(m, *a, &b)
27
- self.method_name = m
28
- self.arguments = a
29
- self.block = b
26
+ def initialize(method_name, arguments, block)
27
+ self.method_name = method_name
28
+ self.arguments = arguments
29
+ self.block = block
30
30
  end
31
-
32
- def send_to(subject, recorder)
33
- subject.__send__(self.method_name, *self.arguments, &self.block)
34
- end
35
-
36
- def arguments
37
- @arguments ||= []
38
- end
39
-
40
- def block
41
- @block ||= Proc.new { nil }
31
+
32
+ def to_message_call(subject)
33
+ Message::Recorder::MessageCall.new(subject, method_name, arguments, block)
42
34
  end
43
35
 
44
36
  end
@@ -0,0 +1,45 @@
1
+ # Copyright (c) 2008 Simon Menke
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining
4
+ # a copy of this software and associated documentation files (the
5
+ # "Software"), to deal in the Software without restriction, including
6
+ # without limitation the rights to use, copy, modify, merge, publish,
7
+ # distribute, sublicense, and/or sell copies of the Software, and to
8
+ # permit persons to whom the Software is furnished to do so, subject to
9
+ # the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be
12
+ # included in all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+
22
+ # An object on this class is passed to the filter blocks
23
+ class Message::Recorder::MessageCall
24
+ attr_reader :subject, :method_name, :arguments, :block, :return_value
25
+
26
+ def arguments # :nodoc:
27
+ @arguments ||= []
28
+ end
29
+ def block # :nodoc:
30
+ @block ||= Proc.new { || false }
31
+ end
32
+
33
+ def initialize(subject, method_name, arguments, block, return_value=nil) # :nodoc:
34
+ @subject = subject
35
+ @method_name = method_name
36
+ @arguments = arguments
37
+ @block = block
38
+ @return_value = return_value
39
+ end
40
+
41
+ def execute # :nodoc:
42
+ @return_value = @subject.__send__(@method_name, *@arguments, &@block)
43
+ end
44
+
45
+ end
@@ -22,26 +22,28 @@
22
22
  # Example:
23
23
  # recorder = Message::Recorder.new
24
24
  # recorder.record.downcase.intern
25
- # recorder.before do |subject, method, arguments|
26
- # !(subject.nil? or subject.empty?)
25
+ # recorder.before do |message_call|
26
+ # p message_call.class # => Message::Recorder::MessageCall
27
+ # !(message_call.subject.nil? or message_callsubject.empty?)
27
28
  # end
28
- # recorder.after do |subject, method, arguments, return_value|
29
- # p return_value
29
+ # recorder.after do |message_call|
30
+ # p message_call.class # => Message::Recorder::MessageCall
31
+ # p message_call.return_value
30
32
  # end
31
33
  # recorder.send_to(nil) # => nil
32
34
  # recorder.send_to("") # => nil
33
35
  # recorder.send_to("Mr_Henry") # => :mr_henry
34
36
  class Message::Recorder
35
37
 
36
- # Takes a block with three arguments
37
- # recorder.before { |subject, method, arguments| ... }
38
+ # Takes a block with one argument of type Message::Recorder::MessageCall
39
+ # recorder.before { |message_call| ... }
38
40
  # if the block returns false the chain will be broken.
39
41
  def before(&block)
40
42
  before_filters << block
41
43
  end
42
44
 
43
- # Takes a block with four arguments
44
- # recorder.after { |subject, method, arguments, return_value| ... }
45
+ # Takes a block with one argument of type Message::Recorder::MessageCall
46
+ # recorder.after { |message_call| ... }
45
47
  def after(&block)
46
48
  after_filters << block
47
49
  end
@@ -69,17 +71,17 @@ class Message::Recorder
69
71
  @before_filters ||= []
70
72
  end
71
73
 
72
- def filter_before(subject, method, arguments) # :nodoc:
74
+ def filter_before(message_call) # :nodoc:
73
75
  before_filters.each do |filter|
74
- should_break = filter.call(subject, method, arguments)
76
+ should_break = filter.call(message_call)
75
77
  return false if should_break === false
76
78
  end
77
79
  return true
78
80
  end
79
81
 
80
- def filter_after(subject, method, arguments, return_value) # :nodoc:
82
+ def filter_after(message_call) # :nodoc:
81
83
  after_filters.each do |filter|
82
- filter.call(subject, method, arguments, return_value)
84
+ filter.call(message_call)
83
85
  end
84
86
  end
85
87
 
@@ -23,7 +23,7 @@ module Message #:nodoc:
23
23
  module VERSION #:nodoc:
24
24
  MAJOR = 1
25
25
  MINOR = 0
26
- TINY = 1
26
+ TINY = 2
27
27
 
28
28
  STRING = [MAJOR, MINOR, TINY].join('.')
29
29
  end
data/website/index.html CHANGED
@@ -33,7 +33,7 @@
33
33
  <h1>message recorder</h1>
34
34
  <div id="version" class="clickable" onclick='document.location = "http://rubyforge.org/projects/message-recorder"; return false'>
35
35
  <p>Get Version</p>
36
- <a href="http://rubyforge.org/projects/message-recorder" class="numbers">1.0.1</a>
36
+ <a href="http://rubyforge.org/projects/message-recorder" class="numbers">1.0.2</a>
37
37
  </div>
38
38
  <h2>&#x2192; &#8216;message-recorder&#8217;</h2>
39
39
 
@@ -81,6 +81,37 @@
81
81
  </pre></p>
82
82
 
83
83
 
84
+ <p>Here we use two before filters to make sure that we get a non-nil adn non-empty subject.
85
+ And we use an after filter to print the result of each message call.</p>
86
+
87
+
88
+ <p><pre class='syntax'>
89
+ <span class="ident">recorder</span> <span class="punct">=</span> <span class="constant">Message</span><span class="punct">::</span><span class="constant">Recorder</span><span class="punct">.</span><span class="ident">new</span>
90
+ <span class="ident">recorder</span><span class="punct">.</span><span class="ident">record</span><span class="punct">.</span><span class="ident">strip</span><span class="punct">.</span><span class="ident">downcase</span>
91
+ <span class="ident">recorder</span><span class="punct">.</span><span class="ident">before</span> <span class="keyword">do</span> <span class="punct">|</span><span class="ident">message_call</span><span class="punct">|</span>
92
+ <span class="ident">message_call</span><span class="punct">.</span><span class="ident">subject</span><span class="punct">.</span><span class="ident">nil?</span> <span class="punct">==</span> <span class="constant">false</span>
93
+ <span class="keyword">end</span>
94
+ <span class="ident">recorder</span><span class="punct">.</span><span class="ident">before</span> <span class="keyword">do</span> <span class="punct">|</span><span class="ident">message_call</span><span class="punct">|</span>
95
+ <span class="ident">message_call</span><span class="punct">.</span><span class="ident">subject</span><span class="punct">.</span><span class="ident">empty?</span> <span class="punct">==</span> <span class="constant">false</span>
96
+ <span class="keyword">end</span>
97
+ <span class="ident">recorder</span><span class="punct">.</span><span class="ident">after</span> <span class="keyword">do</span> <span class="punct">|</span><span class="ident">message_call</span><span class="punct">|</span>
98
+ <span class="ident">puts</span> <span class="punct">&quot;</span><span class="string"><span class="expr">#{message_call.method_name}</span>: <span class="expr">#{message_call.return_value.inspect}</span></span><span class="punct">&quot;</span>
99
+ <span class="keyword">end</span>
100
+ <span class="ident">recorder</span><span class="punct">.</span><span class="ident">send_to</span><span class="punct">(</span><span class="constant">nil</span><span class="punct">)</span> <span class="comment"># =&gt; nil</span>
101
+ <span class="ident">recorder</span><span class="punct">.</span><span class="ident">send_to</span><span class="punct">(&quot;</span><span class="string"></span><span class="punct">&quot;)</span> <span class="comment"># =&gt; nil</span>
102
+ <span class="ident">recorder</span><span class="punct">.</span><span class="ident">send_to</span><span class="punct">(&quot;</span><span class="string">Mr. Henry</span><span class="punct">&quot;)</span> <span class="comment"># =&gt; &quot;mr. henry&quot;</span>
103
+ <span class="ident">recorder</span><span class="punct">.</span><span class="ident">send_to</span><span class="punct">(&quot;</span><span class="string"> Mr. Simon </span><span class="punct">&quot;)</span> <span class="comment"># =&gt; &quot;mr. henry&quot;</span>
104
+ </pre></p>
105
+
106
+
107
+ Print out:
108
+ <pre>
109
+ strip: "Mr. Henry"
110
+ downcase: "mr. henry"
111
+ strip: "Mr. Simon"
112
+ downcase: "mr. simon"
113
+ </pre>
114
+
84
115
  <h2>Demonstration of usage</h2>
85
116
 
86
117
 
data/website/index.txt CHANGED
@@ -36,6 +36,35 @@ end
36
36
  recorder.send_to("HELLO") # => [:hello, "Hello"]
37
37
  </pre>
38
38
 
39
+ Here we use two before filters to make sure that we get a non-nil adn non-empty subject.
40
+ And we use an after filter to print the result of each message call.
41
+
42
+ <pre syntax="ruby">
43
+ recorder = Message::Recorder.new
44
+ recorder.record.strip.downcase
45
+ recorder.before do |message_call|
46
+ message_call.subject.nil? == false
47
+ end
48
+ recorder.before do |message_call|
49
+ message_call.subject.empty? == false
50
+ end
51
+ recorder.after do |message_call|
52
+ puts "#{message_call.method_name}: #{message_call.return_value.inspect}"
53
+ end
54
+ recorder.send_to(nil) # => nil
55
+ recorder.send_to("") # => nil
56
+ recorder.send_to("Mr. Henry") # => "mr. henry"
57
+ recorder.send_to(" Mr. Simon ") # => "mr. henry"
58
+ </pre>
59
+
60
+ Print out:
61
+ <pre>
62
+ strip: "Mr. Henry"
63
+ downcase: "mr. henry"
64
+ strip: "Mr. Simon"
65
+ downcase: "mr. simon"
66
+ </pre>
67
+
39
68
  h2. Demonstration of usage
40
69
 
41
70
  This is a more complex example with nested branches.
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: message-recorder
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Simon Menke
@@ -42,6 +42,7 @@ files:
42
42
  - lib/message-recorder/chaining_mock.rb
43
43
  - lib/message-recorder/collector.rb
44
44
  - lib/message-recorder/message.rb
45
+ - lib/message-recorder/message_call.rb
45
46
  - lib/message-recorder/recorder.rb
46
47
  - lib/message-recorder/version.rb
47
48
  - log/debug.log
@@ -51,8 +52,6 @@ files:
51
52
  - setup.rb
52
53
  - spec/chain_spec.rb
53
54
  - spec/collector_spec.rb
54
- - spec/fob_spec.rb
55
- - spec/message_spec.rb
56
55
  - spec/spec.opts
57
56
  - spec/spec_helper.rb
58
57
  - tasks/deployment.rake
data/spec/fob_spec.rb DELETED
@@ -1,13 +0,0 @@
1
- require File.dirname(__FILE__) + '/spec_helper'
2
-
3
- describe Message::Recorder do
4
-
5
- it "should dup properly" do
6
- chain = Message::Recorder::Chain.new
7
- chain.record(:downcase)
8
- chain.record(:+, " world")
9
-
10
- chain.dup.should be_eql(chain)
11
- end
12
-
13
- end
data/spec/message_spec.rb DELETED
@@ -1,89 +0,0 @@
1
- require File.dirname(__FILE__) + '/spec_helper'
2
-
3
- describe Message::Recorder::Message do
4
-
5
- before do
6
- @recorder = Message::Recorder.new
7
- end
8
-
9
- it "should init properly (with all arguments)" do
10
- message = Message::Recorder::Message.new(:inject, 0) { |sum, n| sum += n }
11
- message.method_name.should be_eql(:inject)
12
- message.arguments.should be_eql([0])
13
- message.block.should_not be_nil
14
- end
15
-
16
- it "should init properly (with all arguments, except block)" do
17
- message = Message::Recorder::Message.new(:inject, 0)
18
- message.method_name.should be_eql(:inject)
19
- message.arguments.should be_eql([0])
20
- message.block.should_not be_nil
21
- end
22
-
23
- it "should init properly (with all arguments, except m_arguments)" do
24
- message = Message::Recorder::Message.new(:inject) { |sum, n| sum += n }
25
- message.method_name.should be_eql(:inject)
26
- message.arguments.should be_eql([])
27
- message.block.should_not be_nil
28
- end
29
-
30
- it "should init properly (with all arguments, except block m_arguments)" do
31
- message = Message::Recorder::Message.new(:inject)
32
- message.method_name.should be_eql(:inject)
33
- message.arguments.should be_eql([])
34
- message.block.should_not be_nil
35
- end
36
-
37
- it "should send itself properly to a subject (without arguments and block)" do
38
- message = Message::Recorder::Message.new(:downcase)
39
- message.send_to("HELLO", @recorder).should be_eql("hello")
40
- end
41
-
42
- it "should send itself properly to a subject (without block)" do
43
- message = Message::Recorder::Message.new(:+, " world")
44
- message.send_to("hello", @recorder).should be_eql("hello world")
45
- end
46
-
47
- it "should send itself properly to a subject (without arguments)" do
48
- message = Message::Recorder::Message.new(:inject) { |sum, n| sum + n }
49
- message.send_to([1,2,3], @recorder).should be_eql(6)
50
- end
51
-
52
- it "should send itself properly to a subject" do
53
- message = Message::Recorder::Message.new(:inject, 6) { |sum, n| sum + n }
54
- message.send_to([1,2,3], @recorder).should be_eql(12)
55
- end
56
-
57
- it "should dup properly (with all arguments)" do
58
- message = Message::Recorder::Message.new(:inject, 0) { |sum, n| sum += n }
59
- message = message.dup
60
- message.method_name.should be_eql(:inject)
61
- message.arguments.should be_eql([0])
62
- message.block.should_not be_nil
63
- end
64
-
65
- it "should dup properly (with all arguments, except block)" do
66
- message = Message::Recorder::Message.new(:inject, 0)
67
- message = message.dup
68
- message.method_name.should be_eql(:inject)
69
- message.arguments.should be_eql([0])
70
- message.block.should_not be_nil
71
- end
72
-
73
- it "should dup properly (with all arguments, except m_arguments)" do
74
- message = Message::Recorder::Message.new(:inject) { |sum, n| sum += n }
75
- message = message.dup
76
- message.method_name.should be_eql(:inject)
77
- message.arguments.should be_eql([])
78
- message.block.should_not be_nil
79
- end
80
-
81
- it "should dup properly (with all arguments, except block m_arguments)" do
82
- message = Message::Recorder::Message.new(:inject)
83
- message = message.dup
84
- message.method_name.should be_eql(:inject)
85
- message.arguments.should be_eql([])
86
- message.block.should_not be_nil
87
- end
88
-
89
- end