message-recorder 1.0.3 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,8 @@
1
+ == 1.0.4 2008-02-22
2
+
3
+ * You can now filter before and after call, chain and branch.
4
+ * the after call filter now can break a callchain.
5
+
1
6
  == 1.0.3 2008-02-15
2
7
 
3
8
  * Fixed a small bug.
@@ -23,21 +23,31 @@ class Message::Recorder::BranchingMock # :nodoc:
23
23
  alias_method :__instance_eval__, :instance_eval
24
24
  instance_methods.each { |m| undef_method m unless m =~ /^__/ }
25
25
 
26
- def initialize(collector)
26
+ def initialize(recorder, collector)
27
+ @recorder = recorder
27
28
  @branched_collectors = []
28
29
  @collector_prototype = collector.deep_copy
29
30
  @collector = collector.clear
30
31
  end
32
+
31
33
  def __branch__(&block)
32
34
  __instance_eval__(&block) unless block.nil?
33
35
  @branched_collectors.each { |branched_collector| @collector.concat branched_collector }
34
- Message::Recorder::ChainingMock.new(@collector)
36
+ Message::Recorder::ChainingMock.new(@recorder, @collector)
35
37
  end
38
+
36
39
  def method_missing(m, *args, &block)
40
+ @recorder.filter_before_branch(@collector_prototype)
37
41
  branched_collector = @collector_prototype.branch
38
42
  @branched_collectors << branched_collector
43
+ @recorder.filter_after_branch(@collector_prototype, branched_collector)
44
+
45
+
46
+ message = ::Message::Recorder::Message.new(m, args, block)
47
+ @recorder.filter_before_chain(message)
48
+ branched_collector.record(message)
49
+ @recorder.filter_after_chain(message)
39
50
 
40
- branched_collector.record(m, *args, &block)
41
- Message::Recorder::ChainingMock.new(branched_collector)
51
+ Message::Recorder::ChainingMock.new(@recorder, branched_collector)
42
52
  end
43
53
  end
@@ -26,16 +26,20 @@ class Message::Recorder::Chain < ::Array # :nodoc:
26
26
  super(array)
27
27
  end
28
28
 
29
- def record(m, *args, &block)
30
- push Message::Recorder::Message.new(m, args, block)
29
+ def record_message(m, *args, &block)
30
+ record ::Message::Recorder::Message.new(m, args, block)
31
31
  end
32
32
 
33
- def send_to(subject_, recorder)
34
- inject(subject_) do |subject,message|
35
- message_call = message.to_message_call(subject)
36
- return nil unless recorder.filter_before(message_call)
33
+ def record(message)
34
+ push message
35
+ end
36
+
37
+ def send_to(chain_subject, recorder)
38
+ inject(chain_subject) do |call_subject,message|
39
+ message_call = message.to_message_call(call_subject)
40
+ return nil unless recorder.filter_before_call(message_call)
37
41
  message_call.execute
38
- recorder.filter_after(message_call)
42
+ return nil unless recorder.filter_after_call(message_call)
39
43
  message_call.return_value
40
44
  end
41
45
  end
@@ -24,12 +24,16 @@
24
24
  class Message::Recorder::ChainingMock
25
25
  instance_methods.each { |m| undef_method m unless m =~ /^__/ }
26
26
 
27
- def initialize(collector) # :nodoc:
27
+ def initialize(recorder, collector) # :nodoc:
28
+ @recorder = recorder
28
29
  @collector = collector
29
30
  end
30
31
 
31
32
  def method_missing(m, *args, &block) # :nodoc:
32
- @collector.record(m, *args, &block)
33
+ message = ::Message::Recorder::Message.new(m, args, block)
34
+ @recorder.filter_before_chain(message)
35
+ @collector.record(message)
36
+ @recorder.filter_after_chain(message)
33
37
  return self
34
38
  end
35
39
 
@@ -46,7 +50,7 @@ class Message::Recorder::ChainingMock
46
50
  # recorder.record.bye
47
51
  # recorder.send_to subject
48
52
  def __branch__(&block)
49
- Message::Recorder::BranchingMock.new(@collector).__branch__(&block)
53
+ Message::Recorder::BranchingMock.new(@recorder, @collector).__branch__(&block)
50
54
  end
51
55
  alias_method :with_results, :__branch__
52
56
  alias_method :with_result, :__branch__
@@ -19,31 +19,41 @@
19
19
  # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
20
  # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
21
 
22
- class Message::Recorder::Collector < ::Array # :nodoc:
22
+
23
+ # The Collector stores all the callchains.
24
+ class Message::Recorder::Collector < ::Array
25
+
26
+ attr_accessor :recorder # :nodoc:
23
27
 
24
- def initialize(array=[], root=nil)
25
- super(array)
28
+ def initialize(options={}) # :nodoc:
29
+ super(options[:array]) unless options[:array].nil?
26
30
  push ::Message::Recorder::Chain.new if self.empty?
27
- root_collector = root
31
+ self.recorder = options[:recorder]
32
+ end
33
+
34
+ def collect_messages # :nodoc:
35
+ ::Message::Recorder::ChainingMock.new(self.recorder, self)
28
36
  end
29
37
 
30
- def collect_messages
31
- ::Message::Recorder::ChainingMock.new(self)
38
+ # record a message on all callchains.
39
+ def record_message(m, *args, &block)
40
+ record ::Message::Recorder::Message.new(m, args, block)
32
41
  end
33
42
 
34
- def record(m, *args, &block)
35
- each { |chain| chain.record(m, *args, &block) }
43
+ # record a message object on all callchains.
44
+ def record(message)
45
+ each { |chain| chain.record(message) }
36
46
  end
37
47
 
38
- def branch
48
+ def branch # :nodoc:
39
49
  self.deep_copy
40
50
  end
41
51
 
42
- def deep_copy
43
- ::Message::Recorder::Collector.new(collect { |chain| chain.dup })
52
+ def deep_copy # :nodoc:
53
+ ::Message::Recorder::Collector.new(:array => collect { |chain| chain.dup }, :recorder => self.recorder)
44
54
  end
45
55
 
46
- def send_to(subject, recorder)
56
+ def send_to(subject, recorder) # :nodoc:
47
57
  results = ::Array.new
48
58
  each { |chain| results << chain.send_to(subject, recorder) }
49
59
  results
@@ -19,18 +19,32 @@
19
19
  # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
20
  # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
21
 
22
+ # The Message class describes a message.
23
+ class Message::Recorder::Message
24
+ # the name of the method being called.
25
+ attr_reader :method_name
26
+
27
+ # the arguments passed to the method.
28
+ attr_reader :arguments
29
+
30
+ # the block passed to the method.
31
+ attr_reader :block
22
32
 
23
- class Message::Recorder::Message # :nodoc:
24
- attr_accessor :method_name, :arguments, :block
25
-
26
- def initialize(method_name, arguments, block)
27
- self.method_name = method_name
28
- self.arguments = arguments
29
- self.block = block
33
+ def initialize(method_name, arguments, block) # :nodoc:
34
+ @method_name = method_name
35
+ @arguments = arguments
36
+ @block = block
30
37
  end
31
38
 
32
- def to_message_call(subject)
33
- Message::Recorder::MessageCall.new(subject, method_name, arguments, block)
39
+ def to_message_call(subject) # :nodoc:
40
+ Message::Recorder::MessageCall.new(subject, self)
34
41
  end
35
42
 
43
+ def arguments # :nodoc:
44
+ @arguments ||= []
45
+ end
46
+
47
+ def block # :nodoc:
48
+ @block ||= Proc.new { || false }
49
+ end
36
50
  end
@@ -21,25 +21,23 @@
21
21
 
22
22
  # An object on this class is passed to the filter blocks
23
23
  class Message::Recorder::MessageCall
24
- attr_reader :subject, :method_name, :arguments, :block, :return_value
24
+ # the receiver of the message
25
+ attr_reader :subject
26
+ # the message sed to the subject when execute is called.
27
+ attr_reader :message
28
+ # the return value of the message call.
29
+ attr_reader :return_value
25
30
 
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:
31
+ # initialize a message call
32
+ def initialize(subject, message, return_value=nil)
34
33
  @subject = subject
35
- @method_name = method_name
36
- @arguments = arguments
37
- @block = block
34
+ @message = message
38
35
  @return_value = return_value
39
36
  end
40
37
 
41
- def execute # :nodoc:
42
- @return_value = @subject.__send__(@method_name, *@arguments, &@block)
38
+ # send <tt>message</tt> to <tt>subject</tt>.
39
+ def execute
40
+ @return_value = @subject.__send__(@message.method_name, *@message.arguments, &@message.block)
43
41
  end
44
42
 
45
43
  end
@@ -35,17 +35,85 @@
35
35
  # recorder.send_to("Mr_Henry") # => :mr_henry
36
36
  class Message::Recorder
37
37
 
38
+
39
+ # <b>BEFORE CALL FILTER:</b>
40
+ #
38
41
  # Takes a block with one argument of type Message::Recorder::MessageCall
39
42
  # recorder.before { |message_call| ... }
43
+ # recorder.before :call { |message_call| ... }
44
+ # recorder.before :call, :my_filter_method
45
+ # def my_filter_method(message_call)
46
+ # ...
47
+ # end
40
48
  # if the block returns false the chain will be broken.
41
- def before(&block)
42
- before_filters << block
49
+ #
50
+ # <b>BEFORE CHAIN FILTER:</b>
51
+ #
52
+ # Takes a block with one argument of type Message::Recorder::Message
53
+ # recorder.before :chain { |message| ... }
54
+ # recorder.before :chain, :my_filter_method
55
+ # def my_filter_method(message)
56
+ # ...
57
+ # end
58
+ #
59
+ # <b>BEFORE BRANCH FILTER:</b>
60
+ #
61
+ # Takes a block with one argument of type Message::Recorder::Collector
62
+ # recorder.before :branch { |collector| ... }
63
+ # recorder.before :branch, :my_filter_method
64
+ # def my_filter_method(collector)
65
+ # ...
66
+ # end
67
+ def before(*options,&block)
68
+ type = options.shift if [:call, :chain, :branch].include? options.first
69
+ type ||= :call
70
+ filter = block
71
+ filter ||= method(options.shift)
72
+ case type
73
+ when :call then before_call_filters << filter
74
+ when :chain then before_chain_filters << filter
75
+ when :branch then before_branch_filters << filter
76
+ end
43
77
  end
44
78
 
79
+ # <b>AFTER CALL FILTER:</b>
80
+ #
45
81
  # Takes a block with one argument of type Message::Recorder::MessageCall
46
82
  # recorder.after { |message_call| ... }
47
- def after(&block)
48
- after_filters << block
83
+ # recorder.after :call { |message_call| ... }
84
+ # recorder.after :call, :my_filter_method
85
+ # def my_filter_method(message_call)
86
+ # ...
87
+ # end
88
+ # if the block returns false the chain will be broken.
89
+ #
90
+ # <b>AFTER CHAIN FILTER:</b>
91
+ #
92
+ # Takes a block with one argument of type Message::Recorder::Message
93
+ # recorder.after :chain { |message| ... }
94
+ # recorder.after :chain, :my_filter_method
95
+ # def my_filter_method(message)
96
+ # ...
97
+ # end
98
+ #
99
+ # <b>AFTER BRANCH FILTER:</b>
100
+ #
101
+ # Takes a block with two arguments of type Message::Recorder::Collector
102
+ # recorder.after :branch { |original_collector, new_collector| ... }
103
+ # recorder.after :branch, :my_filter_method
104
+ # def my_filter_method(original_collector, new_collector)
105
+ # ...
106
+ # end
107
+ def after(*options,&block)
108
+ type = options.shift if [:call, :chain, :branch].include? options.first
109
+ type ||= :call
110
+ filter = block
111
+ filter ||= method(options.shift)
112
+ case type
113
+ when :call then after_call_filters << filter
114
+ when :chain then after_chain_filters << filter
115
+ when :branch then after_branch_filters << filter
116
+ end
49
117
  end
50
118
 
51
119
  # #record returns a mock object which will store all the messages you send to it.
@@ -60,33 +128,79 @@ class Message::Recorder
60
128
  end
61
129
 
62
130
 
63
- attr_accessor :before_filters # :nodoc:
64
- attr_accessor :after_filters # :nodoc:
131
+ attr_accessor :before_call_filters # :nodoc:
132
+ attr_accessor :after_call_filters # :nodoc:
133
+ attr_accessor :before_branch_filters # :nodoc:
134
+ attr_accessor :after_branch_filters # :nodoc:
135
+ attr_accessor :before_chain_filters # :nodoc:
136
+ attr_accessor :after_chain_filters # :nodoc:
137
+
138
+ def before_call_filters # :nodoc:
139
+ @before_call_filters ||= []
140
+ end
141
+
142
+ def after_call_filters # :nodoc:
143
+ @after_call_filters ||= []
144
+ end
145
+
146
+ def before_branch_filters # :nodoc:
147
+ @before_branch_filters ||= []
148
+ end
65
149
 
66
- def before_filters # :nodoc:
67
- @before_filters ||= []
150
+ def after_branch_filters # :nodoc:
151
+ @after_branch_filters ||= []
68
152
  end
69
153
 
70
- def after_filters # :nodoc:
71
- @before_filters ||= []
154
+ def before_chain_filters # :nodoc:
155
+ @before_chain_filters ||= []
72
156
  end
73
157
 
74
- def filter_before(message_call) # :nodoc:
75
- before_filters.each do |filter|
158
+ def after_chain_filters # :nodoc:
159
+ @after_chain_filters ||= []
160
+ end
161
+
162
+ def filter_before_call(message_call) # :nodoc:
163
+ before_call_filters.each do |filter|
76
164
  should_break = filter.call(message_call)
77
165
  return false if should_break === false
78
166
  end
79
167
  return true
80
168
  end
81
169
 
82
- def filter_after(message_call) # :nodoc:
83
- after_filters.each do |filter|
84
- filter.call(message_call)
170
+ def filter_after_call(message_call) # :nodoc:
171
+ after_call_filters.each do |filter|
172
+ should_break = filter.call(message_call)
173
+ return false if should_break === false
174
+ end
175
+ return true
176
+ end
177
+
178
+ def filter_before_branch(collector) # :nodoc:
179
+ before_branch_filters.each do |filter|
180
+ filter.call(collector)
181
+ end
182
+ end
183
+
184
+ def filter_after_branch(original, branch) # :nodoc:
185
+ after_branch_filters.each do |filter|
186
+ filter.call(original, branch)
187
+ end
188
+ end
189
+
190
+ def filter_before_chain(message) # :nodoc:
191
+ before_chain_filters.each do |filter|
192
+ filter.call(collector)
193
+ end
194
+ end
195
+
196
+ def filter_after_chain(message) # :nodoc:
197
+ after_chain_filters.each do |filter|
198
+ filter.call(message)
85
199
  end
86
200
  end
87
201
 
88
202
  def collector # :nodoc:
89
- @collector ||= ::Message::Recorder::Collector.new
203
+ @collector ||= ::Message::Recorder::Collector.new(:recorder => self)
90
204
  end
91
205
 
92
206
  end
@@ -23,7 +23,7 @@ module Message #:nodoc:
23
23
  module VERSION #:nodoc:
24
24
  MAJOR = 1
25
25
  MINOR = 0
26
- TINY = 3
26
+ TINY = 4
27
27
 
28
28
  STRING = [MAJOR, MINOR, TINY].join('.')
29
29
  end
@@ -8,8 +8,8 @@ describe Message::Recorder::Chain do
8
8
 
9
9
  it "should record" do
10
10
  chain = Message::Recorder::Chain.new
11
- chain.record(:downcase)
12
- chain.record(:+, " world")
11
+ chain.record_message(:downcase)
12
+ chain.record_message(:+, " world")
13
13
 
14
14
  chain.size.should be_eql(2)
15
15
  chain.send_to("HELLO",@recorder).should be_eql("hello world")
@@ -17,8 +17,8 @@ describe Message::Recorder::Chain do
17
17
 
18
18
  it "should dup properly" do
19
19
  chain = Message::Recorder::Chain.new
20
- chain.record(:downcase)
21
- chain.record(:+, " world")
20
+ chain.record_message(:downcase)
21
+ chain.record_message(:+, " world")
22
22
 
23
23
  chain.dup.should be_eql(chain)
24
24
  end
@@ -9,9 +9,9 @@ describe Message::Recorder::Collector do
9
9
  end
10
10
 
11
11
  it "should record" do
12
- chain = Message::Recorder::Collector.new
13
- chain.record(:downcase)
14
- chain.record(:+, " world")
12
+ chain = Message::Recorder::Collector.new(:recorder => @recorder)
13
+ chain.record_message(:downcase)
14
+ chain.record_message(:+, " world")
15
15
 
16
16
  chain.size.should be_eql(1)
17
17
  chain.first.size.should be_eql(2)
@@ -19,16 +19,16 @@ describe Message::Recorder::Collector do
19
19
  end
20
20
 
21
21
  it "should branch" do
22
- chain = Message::Recorder::Collector.new
23
- chain.record(:downcase)
24
- chain.record(:+, " world")
22
+ chain = Message::Recorder::Collector.new(:recorder => @recorder)
23
+ chain.record_message(:downcase)
24
+ chain.record_message(:+, " world")
25
25
 
26
26
  chain.size.should be_eql(1)
27
27
  chain.first.size.should be_eql(2)
28
28
  chain.send_to("HELLO", @recorder).should be_eql(["hello world"])
29
29
 
30
30
  branch = chain.branch
31
- branch.record(:+, "!")
31
+ branch.record_message(:+, "!")
32
32
  branch.size.should be_eql(1)
33
33
  branch.first.size.should be_eql(3)
34
34
  branch.send_to("HELLO", @recorder).should be_eql(["hello world!"])
@@ -37,7 +37,7 @@ describe Message::Recorder::Collector do
37
37
  end
38
38
 
39
39
  it "should collect a single chain" do
40
- collector = Message::Recorder::Collector.new
40
+ collector = Message::Recorder::Collector.new(:recorder => @recorder)
41
41
  collector.collect_messages.downcase + " world"
42
42
 
43
43
  collector.size.should be_eql(1)
@@ -45,7 +45,7 @@ describe Message::Recorder::Collector do
45
45
  end
46
46
 
47
47
  it "should collect a branched chain" do
48
- collector = Message::Recorder::Collector.new
48
+ collector = Message::Recorder::Collector.new(:recorder => @recorder)
49
49
  collector.collect_messages.downcase.with_results do
50
50
  intern
51
51
  capitalize
@@ -58,7 +58,7 @@ describe Message::Recorder::Collector do
58
58
  end
59
59
 
60
60
  it "should collect a branched chain" do
61
- collector = Message::Recorder::Collector.new
61
+ collector = Message::Recorder::Collector.new(:recorder => @recorder)
62
62
  collector.collect_messages.compact.with_results do
63
63
  collect { |s| s.intern }
64
64
  collect { |s| s.capitalize }.with_results do
@@ -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.3</a>
36
+ <a href="http://rubyforge.org/projects/message-recorder" class="numbers">1.0.4</a>
37
37
  </div>
38
38
  <h2>&#x2192; &#8216;message-recorder&#8217;</h2>
39
39
 
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.3
4
+ version: 1.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Simon Menke
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-02-15 00:00:00 +01:00
12
+ date: 2008-02-22 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies: []
15
15