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.
- data/History.txt +5 -0
- data/lib/message-recorder/branching_mock.rb +14 -4
- data/lib/message-recorder/chain.rb +11 -7
- data/lib/message-recorder/chaining_mock.rb +7 -3
- data/lib/message-recorder/collector.rb +22 -12
- data/lib/message-recorder/message.rb +23 -9
- data/lib/message-recorder/message_call.rb +12 -14
- data/lib/message-recorder/recorder.rb +130 -16
- data/lib/message-recorder/version.rb +1 -1
- data/spec/chain_spec.rb +4 -4
- data/spec/collector_spec.rb +10 -10
- data/website/index.html +1 -1
- metadata +2 -2
data/History.txt
CHANGED
@@ -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
|
-
|
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
|
30
|
-
|
29
|
+
def record_message(m, *args, &block)
|
30
|
+
record ::Message::Recorder::Message.new(m, args, block)
|
31
31
|
end
|
32
32
|
|
33
|
-
def
|
34
|
-
|
35
|
-
|
36
|
-
|
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.
|
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
|
-
|
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
|
-
|
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(
|
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
|
-
|
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
|
-
|
31
|
-
|
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
|
-
|
35
|
-
|
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
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
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,
|
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
|
-
|
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
|
-
|
27
|
-
|
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
|
-
@
|
36
|
-
@arguments = arguments
|
37
|
-
@block = block
|
34
|
+
@message = message
|
38
35
|
@return_value = return_value
|
39
36
|
end
|
40
37
|
|
41
|
-
|
42
|
-
|
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
|
-
|
42
|
-
|
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
|
-
|
48
|
-
|
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 :
|
64
|
-
attr_accessor :
|
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
|
67
|
-
@
|
150
|
+
def after_branch_filters # :nodoc:
|
151
|
+
@after_branch_filters ||= []
|
68
152
|
end
|
69
153
|
|
70
|
-
def
|
71
|
-
@
|
154
|
+
def before_chain_filters # :nodoc:
|
155
|
+
@before_chain_filters ||= []
|
72
156
|
end
|
73
157
|
|
74
|
-
def
|
75
|
-
|
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
|
83
|
-
|
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
|
data/spec/chain_spec.rb
CHANGED
@@ -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.
|
12
|
-
chain.
|
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.
|
21
|
-
chain.
|
20
|
+
chain.record_message(:downcase)
|
21
|
+
chain.record_message(:+, " world")
|
22
22
|
|
23
23
|
chain.dup.should be_eql(chain)
|
24
24
|
end
|
data/spec/collector_spec.rb
CHANGED
@@ -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.
|
14
|
-
chain.
|
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.
|
24
|
-
chain.
|
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.
|
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
|
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.
|
36
|
+
<a href="http://rubyforge.org/projects/message-recorder" class="numbers">1.0.4</a>
|
37
37
|
</div>
|
38
38
|
<h2>→ ‘message-recorder’</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.
|
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-
|
12
|
+
date: 2008-02-22 00:00:00 +01:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|