proxy_machine 0.0.3 → 0.0.4
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/README.rdoc +118 -15
- data/VERSION +1 -1
- data/init.rb +1 -5
- data/lib/proxy_machine/proxy.rb +61 -22
- data/lib/proxy_machine.rb +5 -5
- data/lib/symbol.rb +7 -0
- data/proxy_machine.gemspec +3 -2
- data/spec/proxy_spec.rb +250 -6
- metadata +5 -4
data/README.rdoc
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
= proxy_machine
|
2
|
+
|
1
3
|
== Description
|
2
4
|
|
3
5
|
A cool proxy implementation pattern in ruby
|
@@ -35,8 +37,10 @@ Callafters:
|
|
35
37
|
=== Defining callbefores and callafters in all method calls
|
36
38
|
|
37
39
|
Callbefores:
|
40
|
+
This callback will receive a reference of the object, the symbol of the called method and the
|
41
|
+
arguments passed.
|
38
42
|
|
39
|
-
p = proxy_for [1, 2, 3], :before_all => lambda {|obj| puts 'before all'}
|
43
|
+
p = proxy_for [1, 2, 3], :before_all => lambda {|obj, method, args| puts 'before all'}
|
40
44
|
p.reverse => before all
|
41
45
|
[3, 2, 1]
|
42
46
|
|
@@ -44,8 +48,10 @@ Callbefores:
|
|
44
48
|
3
|
45
49
|
|
46
50
|
Callafters:
|
51
|
+
This callback will receive a reference of the object, the result of execution (this result could be nil),
|
52
|
+
the symbol of the called method and the arguments passed.
|
47
53
|
|
48
|
-
p = proxy_for [1, 2, 3], :after_all => lambda {|obj, result| puts result}
|
54
|
+
p = proxy_for [1, 2, 3], :after_all => lambda {|obj, result, method, args| puts result}
|
49
55
|
p.reverse => [1, 2, 3]
|
50
56
|
[1, 2, 3] # puts
|
51
57
|
|
@@ -54,14 +60,14 @@ Callafters:
|
|
54
60
|
|
55
61
|
=== Registering a class to perform the callafter or callbefore
|
56
62
|
|
57
|
-
The constructor will receive the object, in case of a
|
63
|
+
The constructor will receive the object, in case of a callafter it will receive the result too.
|
58
64
|
You need to have a 'call' method. The ProxyMachine will create a new instance of the class
|
59
|
-
every time it need to use it.
|
65
|
+
every time it need to use it. You could use this feature with the before_all and after_all too.
|
60
66
|
|
61
67
|
# Example of class
|
62
68
|
class SortPerformer
|
63
|
-
def initialize object, result = nil
|
64
|
-
@object = object; @result = result
|
69
|
+
def initialize object, result = nil, method = nil, args = nil
|
70
|
+
@object = object; @result = result; @method = method, @args = args
|
65
71
|
end
|
66
72
|
|
67
73
|
def call; @object.sort! end
|
@@ -72,22 +78,115 @@ every time it need to use it.
|
|
72
78
|
}
|
73
79
|
|
74
80
|
p.reverse => [1, 2, 3]
|
75
|
-
|
76
|
-
===
|
77
|
-
|
78
|
-
|
81
|
+
|
82
|
+
=== Controlling the method execution with regexp
|
83
|
+
|
84
|
+
For before_all and after_all you could use regexp to configure whicth methods will be affected.
|
85
|
+
|
86
|
+
# Example of class
|
87
|
+
class MyRegexMethods
|
88
|
+
attr_accessor :value
|
89
|
+
def get_value1; @value ? @value : 'get' end
|
90
|
+
def get_value2; @value ? @value : 'get' end
|
91
|
+
def another_method; @value ? @value : 'another' end
|
92
|
+
def crazy_one; @value ? @value : 'crazy' end
|
93
|
+
end
|
94
|
+
|
95
|
+
p = proxy_for MyRegexMethods.new, :before_all => {
|
96
|
+
:call1 => [/^get_/, lambda {|obj, method, args| obj.value = 'gotcha!' }],
|
97
|
+
}
|
98
|
+
|
99
|
+
p.get_value1 => gotcha!
|
100
|
+
p.get_value2 => gotcha!
|
101
|
+
p.another_method => 'another
|
102
|
+
proxy.crazy_one => 'crazy'
|
103
|
+
|
104
|
+
You could use many definitions if you want, the calls will happen in alphabetical order of the keys.
|
105
|
+
|
106
|
+
p = proxy_for MyRegexMethods.new, :before_all => {
|
107
|
+
:call2 => [/value/, lambda {|obj, method, args| obj.value = "#{obj.value}works"}],
|
108
|
+
:call1 => [/get_/, lambda {|obj, method, args| obj.value = "it_"}]
|
109
|
+
}
|
110
|
+
|
111
|
+
p.get_value1 => it_works
|
112
|
+
p.get_value2 => it_works
|
113
|
+
p.another_method => another
|
114
|
+
p.crazy_one => crazy
|
115
|
+
|
116
|
+
It is also possible to use classes instead of procs.
|
117
|
+
|
118
|
+
# Example of class
|
119
|
+
class Change2Performer
|
120
|
+
def initialize object, result = nil, method = nil, args = nil
|
121
|
+
@object = object; @result = result; @method = method, @args = args
|
122
|
+
end
|
123
|
+
|
124
|
+
def call; @object.value = "#{@object.value}works" end
|
125
|
+
end
|
126
|
+
|
127
|
+
p = proxy_for MyRegexMethods.new, :before_all => {
|
128
|
+
:call2 => [/value/, Change2Performer],
|
129
|
+
:call1 => [/get_/, lambda {|obj, method, args| obj.value = "it_"}]
|
130
|
+
}
|
131
|
+
|
132
|
+
p.get_value1 => it_works
|
133
|
+
p.get_value2 => it_works
|
134
|
+
p.another_method => another
|
135
|
+
p.crazy_one => crazy
|
79
136
|
|
137
|
+
=== How to detect that the object is a proxy?
|
138
|
+
|
139
|
+
The beautiful way:
|
140
|
+
|
80
141
|
o1 = [1, 2, 3]
|
81
142
|
o1.proxied? => false
|
82
|
-
|
143
|
+
|
83
144
|
o2 = proxy_for [1, 2, 3]
|
84
145
|
o2.proxied? => true
|
85
|
-
|
86
|
-
Other way
|
87
|
-
|
146
|
+
|
147
|
+
Other way:
|
148
|
+
|
88
149
|
p = proxy_for [1, 2, 3]
|
89
150
|
defined? p.proxied_class?
|
151
|
+
|
152
|
+
=== Getting access to the original object
|
153
|
+
|
154
|
+
Call original_object method in the proxy object.
|
155
|
+
|
156
|
+
proxy = proxy_for [1, 2, 3]
|
157
|
+
proxy.proxied? => true
|
158
|
+
proxy.original_object.proxied? => false
|
159
|
+
|
160
|
+
=== Special options
|
161
|
+
|
162
|
+
1 - allow_dinamic: Allow execute methods based on method missing, that do not exists actually.
|
163
|
+
When allow_dinamic is enabled, proxy_machine will not check if this method really exists.
|
164
|
+
Default is false.
|
90
165
|
|
166
|
+
# Example of class
|
167
|
+
class MyClass
|
168
|
+
attr_accessor :value
|
169
|
+
def method_missing(symbol, *args); 'nice!'; end
|
170
|
+
end
|
171
|
+
|
172
|
+
p = proxy_for MyClass.new, :allow_dinamic => true, :before => {
|
173
|
+
:magic_method => lambda {|obj| obj.value = 'other value' }
|
174
|
+
}
|
175
|
+
|
176
|
+
p.magic_method => 'other value'
|
177
|
+
|
178
|
+
2 - avoid_original_execution: When this option is enabled, proxy_machine will not call the original method.
|
179
|
+
Default is false.
|
180
|
+
|
181
|
+
p = proxy_for [3, 2, 1],
|
182
|
+
:avoid_original_execution => true,
|
183
|
+
:before => {
|
184
|
+
:empty? => lambda {|obj| obj.sort!}
|
185
|
+
}
|
186
|
+
|
187
|
+
p.empty? => nil
|
188
|
+
p.original_object => [1, 2, 3]
|
189
|
+
|
91
190
|
=== Trying it in irb
|
92
191
|
|
93
192
|
irb
|
@@ -126,4 +225,8 @@ Other way
|
|
126
225
|
:size => lambda {|obj| puts "before: #{obj.inspect}"}
|
127
226
|
}, :after => {
|
128
227
|
:size => lambda {|obj, result| puts "after: #{obj.inspect}: result => #{result}"}
|
129
|
-
}
|
228
|
+
}
|
229
|
+
|
230
|
+
== Copyright
|
231
|
+
|
232
|
+
Copyright (c) 2010 Túlio Ornelas. See LICENSE for details.
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.4
|
data/init.rb
CHANGED
data/lib/proxy_machine/proxy.rb
CHANGED
@@ -2,61 +2,100 @@ module ProxyMachine
|
|
2
2
|
|
3
3
|
class Proxy < BasicObject
|
4
4
|
|
5
|
-
def initialize object,
|
5
|
+
def initialize object, params = nil
|
6
6
|
@object = object
|
7
7
|
@before = @before_all = @after = @after_all = nil
|
8
|
+
@allow_dinamic = false
|
9
|
+
@avoid_original_execution = false
|
8
10
|
|
9
|
-
if
|
10
|
-
@
|
11
|
-
@
|
12
|
-
@
|
13
|
-
@
|
11
|
+
if params
|
12
|
+
@allow_dinamic = params[:allow_dinamic]
|
13
|
+
@avoid_original_execution = params[:avoid_original_execution]
|
14
|
+
@before = params[:before]
|
15
|
+
@before_all = params[:before_all]
|
16
|
+
@after = params[:after]
|
17
|
+
@after_all = params[:after_all]
|
14
18
|
end
|
15
19
|
end
|
16
20
|
|
17
21
|
def method_missing(symbol, *args)
|
18
22
|
@symbol = symbol
|
19
|
-
method = symbol.to_s
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
+
@method = symbol.to_s
|
24
|
+
|
25
|
+
raise NoMethodError.new(@method) unless @allow_dinamic or @object.methods.member?(@method)
|
26
|
+
|
27
|
+
execute_call(@before_all, @object, symbol, args)
|
23
28
|
execute_call(@before, @object)
|
24
|
-
|
25
|
-
@result = @object.send(method, *args)
|
29
|
+
|
30
|
+
@result = @avoid_original_execution ? nil : @object.send(@method, *args)
|
26
31
|
|
27
32
|
result_after = execute_call(@after, @object, @result)
|
28
|
-
result_after_all = execute_call(@after_all, @object, @result)
|
29
|
-
|
33
|
+
result_after_all = execute_call(@after_all, @object, @result, symbol, args)
|
34
|
+
|
30
35
|
return result_after_all if result_after_all
|
31
36
|
return result_after if result_after
|
32
37
|
@result
|
33
38
|
end
|
34
39
|
|
35
40
|
def proxied_class?; true end
|
36
|
-
|
41
|
+
def original_object; @object end
|
42
|
+
|
37
43
|
private
|
38
44
|
def execute_call container, *args
|
39
45
|
executor = get_executor(container)
|
40
46
|
|
47
|
+
result = nil
|
48
|
+
if executor.class == Array
|
49
|
+
executor.each do |e|
|
50
|
+
result = e.send :call, *args if proc?(e)
|
51
|
+
result = e.send(:new, *args).send :call if class?(e)
|
52
|
+
end
|
53
|
+
return result
|
54
|
+
end
|
55
|
+
|
41
56
|
return executor.send :call, *args if proc?(executor)
|
42
57
|
return executor.send(:new, *args).send :call if class?(executor)
|
43
58
|
end
|
44
59
|
|
45
60
|
def get_executor container
|
46
|
-
|
47
|
-
|
48
|
-
|
61
|
+
return nil unless container
|
62
|
+
|
63
|
+
# The content is a proc or a class
|
64
|
+
return container if proc?(container) or class?(container)
|
65
|
+
|
66
|
+
# The content is a hash with an array filled with a regex and a proc or a class
|
67
|
+
if hash?(container) and regexp?(container)
|
68
|
+
matched = regexp_elements(container).select {|array| get_regexp(array) =~ @method}
|
69
|
+
return matched.collect {|array| get_proc_or_class(array)} unless matched.empty?
|
49
70
|
end
|
71
|
+
|
72
|
+
# Lets assume that the content of the key is a proc
|
73
|
+
container[@symbol]
|
50
74
|
end
|
51
75
|
|
52
|
-
def
|
53
|
-
|
76
|
+
def regexp_elements hash
|
77
|
+
elements = hash.keys.sort.collect {|key| array_with_regex?(hash[key]) ? hash[key] : nil}
|
78
|
+
compacted_array = elements.compact
|
79
|
+
compacted_array.nil? ? [] : compacted_array
|
80
|
+
end
|
81
|
+
|
82
|
+
def get_regexp array
|
83
|
+
array.detect {|element| element.class == Regexp}
|
54
84
|
end
|
55
85
|
|
56
|
-
def
|
57
|
-
|
86
|
+
def get_proc_or_class array
|
87
|
+
array.detect {|element| proc?(element) or class?(element)}
|
88
|
+
end
|
89
|
+
|
90
|
+
def array_with_regex? array
|
91
|
+
array.class == Array and array.size == 2 and not get_regexp(array).nil?
|
58
92
|
end
|
59
93
|
|
94
|
+
def proc? block; block and block.class == Proc end
|
95
|
+
def class? param; param and param.class == Class end
|
96
|
+
def hash? param; param and param.class == Hash end
|
97
|
+
def regexp? hash; hash and not regexp_elements(hash).empty? end
|
98
|
+
|
60
99
|
end
|
61
100
|
|
62
101
|
end
|
data/lib/proxy_machine.rb
CHANGED
@@ -5,9 +5,9 @@ module ProxyMachine
|
|
5
5
|
end unless defined?(BasicObject)
|
6
6
|
|
7
7
|
end
|
8
|
-
|
9
|
-
require 'proxy_machine/proxy'
|
10
|
-
include ProxyMachine
|
11
|
-
|
8
|
+
|
12
9
|
require 'kernel'
|
13
|
-
require 'object'
|
10
|
+
require 'object'
|
11
|
+
require 'symbol'
|
12
|
+
require 'proxy_machine/proxy'
|
13
|
+
include ProxyMachine
|
data/lib/symbol.rb
ADDED
data/proxy_machine.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{proxy_machine}
|
8
|
-
s.version = "0.0.
|
8
|
+
s.version = "0.0.4"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["T\303\272lio Ornelas"]
|
12
|
-
s.date = %q{2010-10-
|
12
|
+
s.date = %q{2010-10-25}
|
13
13
|
s.description = %q{A cool proxy implementation pattern in ruby}
|
14
14
|
s.email = %q{ornelas.tulio@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -26,6 +26,7 @@ Gem::Specification.new do |s|
|
|
26
26
|
"lib/object.rb",
|
27
27
|
"lib/proxy_machine.rb",
|
28
28
|
"lib/proxy_machine/proxy.rb",
|
29
|
+
"lib/symbol.rb",
|
29
30
|
"proxy_machine.gemspec",
|
30
31
|
"spec/proxy_spec.rb"
|
31
32
|
]
|
data/spec/proxy_spec.rb
CHANGED
@@ -54,21 +54,38 @@ describe Proxy do
|
|
54
54
|
end
|
55
55
|
|
56
56
|
context 'for all methods' do
|
57
|
+
class AllMethodsClass
|
58
|
+
attr_accessor :method, :args
|
59
|
+
def do_something; 50 end
|
60
|
+
end
|
57
61
|
|
58
62
|
it 'should add a callbefore' do
|
59
63
|
array = [3, 2, 1]
|
60
|
-
proxy = Proxy.new array, :before_all => lambda {|obj| obj.sort!}
|
64
|
+
proxy = Proxy.new array, :before_all => lambda {|obj, method, args| obj.sort!}
|
61
65
|
proxy.should_not be_nil
|
62
66
|
proxy.reverse.should == [3, 2, 1]
|
63
67
|
proxy.max.should == 3
|
64
68
|
proxy.first.should == 1
|
65
69
|
proxy.to_s.should == "123"
|
66
70
|
end
|
71
|
+
|
72
|
+
it 'should provide method name and arguments for the block in callbefore' do
|
73
|
+
obj = AllMethodsClass.new
|
74
|
+
proxy = proxy_for obj, :before_all => lambda {|obj, method, args|
|
75
|
+
obj.method = method
|
76
|
+
obj.args = args
|
77
|
+
}
|
78
|
+
|
79
|
+
proxy.should_not be_nil
|
80
|
+
proxy.do_something.should == 50
|
81
|
+
proxy.original_object.method.should == :do_something
|
82
|
+
proxy.original_object.args.should be_empty
|
83
|
+
end
|
67
84
|
|
68
85
|
it 'should add a callafter' do
|
69
86
|
array = [1, 2, 3]
|
70
87
|
|
71
|
-
proxy = Proxy.new array, :after_all => lambda {|obj, result|
|
88
|
+
proxy = Proxy.new array, :after_all => lambda {|obj, result, method, args|
|
72
89
|
return result * 10 if result.class == Fixnum
|
73
90
|
result
|
74
91
|
}
|
@@ -80,11 +97,25 @@ describe Proxy do
|
|
80
97
|
proxy.to_s.should == "123"
|
81
98
|
end
|
82
99
|
|
100
|
+
it 'should provide method name and arguments for the block in callafter' do
|
101
|
+
obj = AllMethodsClass.new
|
102
|
+
proxy = proxy_for obj, :after_all => lambda {|obj, result, method, args|
|
103
|
+
obj.method = method
|
104
|
+
obj.args = args
|
105
|
+
nil
|
106
|
+
}
|
107
|
+
|
108
|
+
proxy.should_not be_nil
|
109
|
+
proxy.do_something.should == 50
|
110
|
+
proxy.original_object.method.should == :do_something
|
111
|
+
proxy.original_object.args.should be_empty
|
112
|
+
end
|
113
|
+
|
83
114
|
it 'should add both, callbefore and callafter' do
|
84
115
|
array = [3, 2, 1]
|
85
116
|
proxy = Proxy.new array,
|
86
|
-
:before_all => lambda {|obj| obj.sort!},
|
87
|
-
:after_all => lambda {|obj, result|
|
117
|
+
:before_all => lambda {|obj, method, args| obj.sort!},
|
118
|
+
:after_all => lambda {|obj, result, method, args|
|
88
119
|
return result * 10 if result.class == Fixnum
|
89
120
|
result
|
90
121
|
}
|
@@ -134,8 +165,8 @@ describe Proxy do
|
|
134
165
|
context 'when registering a class' do
|
135
166
|
# Example of class
|
136
167
|
class SortPerformer
|
137
|
-
def initialize object, result = nil
|
138
|
-
@object = object; @result = result
|
168
|
+
def initialize object, result = nil, method = nil, args = nil
|
169
|
+
@object = object; @result = result; @method = method, @args = args
|
139
170
|
end
|
140
171
|
|
141
172
|
def call; @object.sort! end
|
@@ -159,6 +190,219 @@ describe Proxy do
|
|
159
190
|
proxy.reverse.should == [3, 2, 1]
|
160
191
|
end
|
161
192
|
|
193
|
+
it 'should use a instance of the registered class for callbefore_all' do
|
194
|
+
array = [3, 2, 1]
|
195
|
+
proxy = proxy_for array, :before_all => SortPerformer
|
196
|
+
|
197
|
+
proxy.should_not be_nil
|
198
|
+
proxy.reverse.should == [3, 2, 1]
|
199
|
+
end
|
200
|
+
|
201
|
+
it 'should use a instance of the registered class for callafter_all' do
|
202
|
+
array = [1, 2, 3]
|
203
|
+
proxy = proxy_for array, :after_all => SortPerformer
|
204
|
+
|
205
|
+
proxy.should_not be_nil
|
206
|
+
proxy.reverse.should == [1, 2, 3]
|
207
|
+
end
|
208
|
+
|
209
|
+
end
|
210
|
+
|
211
|
+
context 'with dinamic methods' do
|
212
|
+
class MyClass
|
213
|
+
attr_accessor :value
|
214
|
+
def method_missing(symbol, *args); @value; end
|
215
|
+
end
|
216
|
+
|
217
|
+
it 'should allow dinamic methods' do
|
218
|
+
obj = MyClass.new
|
219
|
+
obj.value = "Crazy Value"
|
220
|
+
|
221
|
+
proxy = proxy_for obj, :allow_dinamic => true, :before => {
|
222
|
+
:crazy_method => lambda {|obj| obj.value = "proxied value!" }
|
223
|
+
}
|
224
|
+
|
225
|
+
proxy.should_not be_nil
|
226
|
+
proxy.crazy_method.should == "proxied value!"
|
227
|
+
end
|
228
|
+
|
229
|
+
end
|
230
|
+
|
231
|
+
context 'avoiding original execution' do
|
232
|
+
class AvoidOriginal
|
233
|
+
attr_accessor :value
|
234
|
+
def do_something; @value = "original" end
|
235
|
+
end
|
236
|
+
|
237
|
+
it 'should happen' do
|
238
|
+
obj = AvoidOriginal.new
|
239
|
+
proxy = proxy_for obj, :avoid_original_execution => true, :after_all => lambda {|obj, result, method, args|
|
240
|
+
"avoided"
|
241
|
+
}
|
242
|
+
|
243
|
+
proxy.should_not be_nil
|
244
|
+
proxy.do_something.should == "avoided"
|
245
|
+
proxy.original_object.value.should be_nil
|
246
|
+
end
|
247
|
+
end
|
248
|
+
|
249
|
+
context 'filtering methods by regex' do
|
250
|
+
class MyRegexMethods
|
251
|
+
attr_accessor :value
|
252
|
+
def get_value1; @value ? @value : 'get' end
|
253
|
+
def get_value2; @value ? @value : 'get' end
|
254
|
+
def another_method; @value ? @value : 'another' end
|
255
|
+
def crazy_one; @value ? @value : 'crazy' end
|
256
|
+
end
|
257
|
+
|
258
|
+
class Change1Performer
|
259
|
+
def initialize object, result = nil, method = nil, args = nil
|
260
|
+
@object = object; @result = result; @method = method, @args = args
|
261
|
+
end
|
262
|
+
|
263
|
+
def call; @object.value = 'gotcha!' end
|
264
|
+
end
|
265
|
+
|
266
|
+
class Change2Performer
|
267
|
+
def initialize object, result = nil, method = nil, args = nil
|
268
|
+
@object = object; @result = result; @method = method, @args = args
|
269
|
+
end
|
270
|
+
|
271
|
+
def call; @object.value = "#{@object.value}works" end
|
272
|
+
end
|
273
|
+
|
274
|
+
it 'should affect just the matched methods on callbefore' do
|
275
|
+
obj = MyRegexMethods.new
|
276
|
+
proxy = proxy_for obj, :before_all => {
|
277
|
+
:call1 => [/^get_/, lambda {|obj, method, args| obj.value = 'gotcha!' }],
|
278
|
+
:call2 => [/method$/, lambda {|obj, method, args| obj.value = 'another gotcha!' }]
|
279
|
+
}
|
280
|
+
|
281
|
+
proxy.should_not be_nil
|
282
|
+
|
283
|
+
proxy.original_object.value = nil
|
284
|
+
proxy.get_value1.should == 'gotcha!'
|
285
|
+
|
286
|
+
proxy.original_object.value = nil
|
287
|
+
proxy.get_value2.should == 'gotcha!'
|
288
|
+
|
289
|
+
proxy.original_object.value = nil
|
290
|
+
proxy.another_method.should == 'another gotcha!'
|
291
|
+
|
292
|
+
proxy.original_object.value = nil
|
293
|
+
proxy.crazy_one.should == 'crazy'
|
294
|
+
end
|
295
|
+
|
296
|
+
it 'should affect just the matched methods on callafter' do
|
297
|
+
obj = MyRegexMethods.new
|
298
|
+
proxy = proxy_for obj, :after_all => {
|
299
|
+
:call1 => [/^get_/, lambda {|obj, result, method, args| obj.value = 'gotcha!' }],
|
300
|
+
:call2 => [/method$/, lambda {|obj, result, method, args| obj.value = 'another gotcha!'}]
|
301
|
+
}
|
302
|
+
|
303
|
+
proxy.should_not be_nil
|
304
|
+
|
305
|
+
proxy.original_object.value = nil
|
306
|
+
proxy.get_value1.should == 'gotcha!'
|
307
|
+
|
308
|
+
proxy.original_object.value = nil
|
309
|
+
proxy.get_value2.should == 'gotcha!'
|
310
|
+
|
311
|
+
proxy.original_object.value = nil
|
312
|
+
proxy.another_method.should == 'another gotcha!'
|
313
|
+
|
314
|
+
proxy.original_object.value = nil
|
315
|
+
proxy.crazy_one.should == 'crazy'
|
316
|
+
end
|
317
|
+
|
318
|
+
it 'should execute the entire matched stack on callbefore sorted by the key name' do
|
319
|
+
obj = MyRegexMethods.new
|
320
|
+
proxy = proxy_for obj, :before_all => {
|
321
|
+
:call2 => [/value/, lambda {|obj, method, args| obj.value = "#{obj.value}works"}],
|
322
|
+
:call1 => [/get_/, lambda {|obj, method, args| obj.value = "it_"}]
|
323
|
+
}
|
324
|
+
|
325
|
+
proxy.should_not be_nil
|
326
|
+
|
327
|
+
proxy.original_object.value = nil
|
328
|
+
proxy.get_value1.should == 'it_works'
|
329
|
+
|
330
|
+
proxy.original_object.value = nil
|
331
|
+
proxy.get_value2.should == 'it_works'
|
332
|
+
|
333
|
+
proxy.original_object.value = nil
|
334
|
+
proxy.another_method.should == 'another'
|
335
|
+
|
336
|
+
proxy.original_object.value = nil
|
337
|
+
proxy.crazy_one.should == 'crazy'
|
338
|
+
end
|
339
|
+
|
340
|
+
it 'should ensure that the last value returned is the value of the last called method' do
|
341
|
+
obj = MyRegexMethods.new
|
342
|
+
proxy = proxy_for obj, :after_all => {
|
343
|
+
:call2 => [/value/, lambda {|obj, result, method, args| 2}],
|
344
|
+
:call1 => [/get_/, lambda {|obj, result, method, args| 1}]
|
345
|
+
}
|
346
|
+
|
347
|
+
proxy.should_not be_nil
|
348
|
+
|
349
|
+
proxy.original_object.value = nil
|
350
|
+
proxy.get_value1.should == 2
|
351
|
+
|
352
|
+
proxy.original_object.value = nil
|
353
|
+
proxy.get_value2.should == 2
|
354
|
+
|
355
|
+
proxy.original_object.value = nil
|
356
|
+
proxy.another_method.should == 'another'
|
357
|
+
|
358
|
+
proxy.original_object.value = nil
|
359
|
+
proxy.crazy_one.should == 'crazy'
|
360
|
+
end
|
361
|
+
|
362
|
+
it 'should affect just the matched methods using a registered class' do
|
363
|
+
obj = MyRegexMethods.new
|
364
|
+
proxy = proxy_for obj, :before_all => {
|
365
|
+
:call1 => [/^get_/, Change1Performer],
|
366
|
+
:call2 => [/method$/, lambda {|obj, method, args| obj.value = 'another gotcha!' }]
|
367
|
+
}
|
368
|
+
|
369
|
+
proxy.should_not be_nil
|
370
|
+
|
371
|
+
proxy.original_object.value = nil
|
372
|
+
proxy.get_value1.should == 'gotcha!'
|
373
|
+
|
374
|
+
proxy.original_object.value = nil
|
375
|
+
proxy.get_value2.should == 'gotcha!'
|
376
|
+
|
377
|
+
proxy.original_object.value = nil
|
378
|
+
proxy.another_method.should == 'another gotcha!'
|
379
|
+
|
380
|
+
proxy.original_object.value = nil
|
381
|
+
proxy.crazy_one.should == 'crazy'
|
382
|
+
end
|
383
|
+
|
384
|
+
it 'should execute the entire matched stack sorted by the key name using a registered class' do
|
385
|
+
obj = MyRegexMethods.new
|
386
|
+
proxy = proxy_for obj, :before_all => {
|
387
|
+
:call2 => [/value/, Change2Performer],
|
388
|
+
:call1 => [/get_/, lambda {|obj, method, args| obj.value = "it_"}]
|
389
|
+
}
|
390
|
+
|
391
|
+
proxy.should_not be_nil
|
392
|
+
|
393
|
+
proxy.original_object.value = nil
|
394
|
+
proxy.get_value1.should == 'it_works'
|
395
|
+
|
396
|
+
proxy.original_object.value = nil
|
397
|
+
proxy.get_value2.should == 'it_works'
|
398
|
+
|
399
|
+
proxy.original_object.value = nil
|
400
|
+
proxy.another_method.should == 'another'
|
401
|
+
|
402
|
+
proxy.original_object.value = nil
|
403
|
+
proxy.crazy_one.should == 'crazy'
|
404
|
+
end
|
405
|
+
|
162
406
|
end
|
163
407
|
|
164
408
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: proxy_machine
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 4
|
10
|
+
version: 0.0.4
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- "T\xC3\xBAlio Ornelas"
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-10-
|
18
|
+
date: 2010-10-25 00:00:00 -02:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -53,6 +53,7 @@ files:
|
|
53
53
|
- lib/object.rb
|
54
54
|
- lib/proxy_machine.rb
|
55
55
|
- lib/proxy_machine/proxy.rb
|
56
|
+
- lib/symbol.rb
|
56
57
|
- proxy_machine.gemspec
|
57
58
|
- spec/proxy_spec.rb
|
58
59
|
has_rdoc: true
|