proxy_machine 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/README.rdoc +47 -0
- data/Rakefile +15 -0
- data/VERSION +1 -0
- data/init.rb +4 -0
- data/lib/kernel.rb +8 -0
- data/lib/proxy_machine/proxy.rb +127 -0
- data/lib/proxy_machine.rb +12 -0
- data/proxy_machine.gemspec +50 -0
- data/spec/proxy_spec.rb +144 -0
- metadata +77 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Túlio Ornelas
|
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.
|
data/README.rdoc
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
== Example usage
|
2
|
+
|
3
|
+
=== The awesome way:
|
4
|
+
|
5
|
+
p = proxy_for [1, 2, 3]
|
6
|
+
p.reverse => [3, 2, 1]
|
7
|
+
|
8
|
+
p = proxy_for [1, 2, 3], :before => {
|
9
|
+
:reverse => lambda {|obj| puts 'before reverse'}
|
10
|
+
}
|
11
|
+
|
12
|
+
p.reverse => before reverse
|
13
|
+
[3, 2, 1]
|
14
|
+
|
15
|
+
=== Other ways:
|
16
|
+
|
17
|
+
1º - Creates a proxy for the informed object
|
18
|
+
|
19
|
+
p = Proxy.new [1,2,3]
|
20
|
+
p.size => 3
|
21
|
+
|
22
|
+
2º - A proxy with a before callback.
|
23
|
+
|
24
|
+
p = Proxy.new [1,2,3], :before => {
|
25
|
+
:size => lambda {|obj| puts "before: #{obj.inspect}"}
|
26
|
+
}
|
27
|
+
|
28
|
+
p.size => before: [1, 2, 3]
|
29
|
+
3
|
30
|
+
|
31
|
+
3º - A proxy with a after callback
|
32
|
+
|
33
|
+
p = Proxy.new [1,2,3], :after => {
|
34
|
+
:size => lambda {|obj, result| puts "after: #{obj.inspect}: result => #{result}"}
|
35
|
+
}
|
36
|
+
|
37
|
+
p.size => after: [1, 2, 3]: result => 3
|
38
|
+
3
|
39
|
+
|
40
|
+
4º - Both
|
41
|
+
|
42
|
+
p = Proxy.new [1,2,3],
|
43
|
+
:before => {
|
44
|
+
:size => lambda {|obj| puts "before: #{obj.inspect}"}
|
45
|
+
}, :after => {
|
46
|
+
:size => lambda {|obj, result| puts "after: #{obj.inspect}: result => #{result}"}
|
47
|
+
}
|
data/Rakefile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
begin
|
2
|
+
require 'jeweler'
|
3
|
+
Jeweler::Tasks.new do |gemspec|
|
4
|
+
gemspec.name = "proxy_machine"
|
5
|
+
gemspec.summary = "A cool proxy implementation pattern in ruby"
|
6
|
+
gemspec.description = "A cool proxy implementation pattern in ruby"
|
7
|
+
gemspec.email = "ornelas.tulio@gmail.com"
|
8
|
+
gemspec.homepage = "http://github.com/tulios/proxy_machine"
|
9
|
+
gemspec.authors = ["Túlio Ornelas"]
|
10
|
+
gemspec.test_files = Dir.glob('spec/*_spec.rb')
|
11
|
+
end
|
12
|
+
Jeweler::GemcutterTasks.new
|
13
|
+
rescue LoadError
|
14
|
+
puts "Jeweler not available. Install it with: gem install jeweler"
|
15
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
data/init.rb
ADDED
data/lib/kernel.rb
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
module ProxyMachine
|
2
|
+
|
3
|
+
class Proxy < BasicObject
|
4
|
+
|
5
|
+
# Examples:
|
6
|
+
# 1º - Creates a proxy for the informed object
|
7
|
+
#
|
8
|
+
# p = Proxy.new [1,2,3]
|
9
|
+
# p.size => 3
|
10
|
+
#
|
11
|
+
# 2º - A proxy with a before callback.
|
12
|
+
#
|
13
|
+
# p = Proxy.new [1,2,3], :before => {
|
14
|
+
# :size => lambda {|obj| puts "before: #{obj.inspect}"}
|
15
|
+
# }
|
16
|
+
# p.size => before: [1, 2, 3]
|
17
|
+
# 3
|
18
|
+
#
|
19
|
+
# 3º - A proxy with a after callback
|
20
|
+
#
|
21
|
+
# p = Proxy.new [1,2,3], :after => {
|
22
|
+
# :size => lambda {|obj, result| puts "after: #{obj.inspect}: result => #{result}"}
|
23
|
+
# }
|
24
|
+
# p.size => after: [1, 2, 3]: result => 3
|
25
|
+
# 3
|
26
|
+
#
|
27
|
+
# 4º - Both
|
28
|
+
#
|
29
|
+
# p = Proxy.new [1,2,3],
|
30
|
+
# :before => {
|
31
|
+
# :size => lambda {|obj| puts "before: #{obj.inspect}"}
|
32
|
+
# }, :after => {
|
33
|
+
# :size => lambda {|obj, result| puts "after: #{obj.inspect}: result => #{result}"}
|
34
|
+
# }
|
35
|
+
#
|
36
|
+
def initialize object, calls = nil
|
37
|
+
@object = object
|
38
|
+
@before = @before_all = @after = @after_all = nil
|
39
|
+
|
40
|
+
if calls
|
41
|
+
@before = calls[:before]
|
42
|
+
@before_all = calls[:before_all]
|
43
|
+
@after = calls[:after]
|
44
|
+
@after_all = calls[:after_all]
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def method_missing(symbol, *args)
|
49
|
+
method = symbol.to_s
|
50
|
+
raise NoMethodError.new(method) unless @object.methods.member?(method)
|
51
|
+
|
52
|
+
execute_call(@before_all, symbol)
|
53
|
+
execute_call(@before, symbol)
|
54
|
+
|
55
|
+
result = @object.send(method, *args)
|
56
|
+
|
57
|
+
result_after = execute_call_with_result(@after, symbol, result)
|
58
|
+
result_after_all = execute_call_with_result(@after_all, symbol, result)
|
59
|
+
|
60
|
+
return result_after_all if result_after_all
|
61
|
+
return result_after if result_after
|
62
|
+
result
|
63
|
+
end
|
64
|
+
|
65
|
+
def proxied_class?; true end
|
66
|
+
|
67
|
+
private
|
68
|
+
def execute_call container, method_symbol
|
69
|
+
if block = get_block(container, method_symbol) and proc?(block)
|
70
|
+
block.call(@object)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
def execute_call_with_result container, method_symbol, result
|
75
|
+
if block = get_block(container, method_symbol) and proc?(block)
|
76
|
+
block.call(@object, result)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def get_block container, method_symbol
|
81
|
+
if container
|
82
|
+
return container if container.class == Proc
|
83
|
+
return container[method_symbol]
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def proc? block
|
88
|
+
block and block.class == Proc
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
92
|
+
|
93
|
+
end
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{proxy_machine}
|
8
|
+
s.version = "0.0.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["T\303\272lio Ornelas"]
|
12
|
+
s.date = %q{2010-10-03}
|
13
|
+
s.description = %q{A cool proxy implementation pattern in ruby}
|
14
|
+
s.email = %q{ornelas.tulio@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
"LICENSE",
|
21
|
+
"README.rdoc",
|
22
|
+
"Rakefile",
|
23
|
+
"VERSION",
|
24
|
+
"init.rb",
|
25
|
+
"lib/kernel.rb",
|
26
|
+
"lib/proxy_machine.rb",
|
27
|
+
"lib/proxy_machine/proxy.rb",
|
28
|
+
"proxy_machine.gemspec",
|
29
|
+
"spec/proxy_spec.rb"
|
30
|
+
]
|
31
|
+
s.homepage = %q{http://github.com/tulios/proxy_machine}
|
32
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
33
|
+
s.require_paths = ["lib"]
|
34
|
+
s.rubygems_version = %q{1.3.7}
|
35
|
+
s.summary = %q{A cool proxy implementation pattern in ruby}
|
36
|
+
s.test_files = [
|
37
|
+
"spec/proxy_spec.rb"
|
38
|
+
]
|
39
|
+
|
40
|
+
if s.respond_to? :specification_version then
|
41
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
42
|
+
s.specification_version = 3
|
43
|
+
|
44
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
45
|
+
else
|
46
|
+
end
|
47
|
+
else
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
data/spec/proxy_spec.rb
ADDED
@@ -0,0 +1,144 @@
|
|
1
|
+
require 'proxy_machine'
|
2
|
+
|
3
|
+
describe Proxy do
|
4
|
+
|
5
|
+
it 'should create a proxy object from the given one' do
|
6
|
+
array = [1, 2, 3]
|
7
|
+
proxy = Proxy.new array
|
8
|
+
proxy.should_not be_nil
|
9
|
+
proxy.class.should == array.class
|
10
|
+
|
11
|
+
proxy.reverse.should == array.reverse
|
12
|
+
proxy.proxied_class?.should be_true
|
13
|
+
end
|
14
|
+
|
15
|
+
context 'for a certain method' do
|
16
|
+
|
17
|
+
it 'should add a callbefore' do
|
18
|
+
array = [1, 2, 3]
|
19
|
+
proxy = Proxy.new array, :before => {
|
20
|
+
:reverse => lambda {|obj| obj << 50 }
|
21
|
+
}
|
22
|
+
proxy.should_not be_nil
|
23
|
+
proxy.reverse.should == [50, 3, 2, 1]
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'should add a callafter' do
|
27
|
+
array = [1, 2, 3]
|
28
|
+
proxy = Proxy.new array, :after => {
|
29
|
+
:reverse => lambda {|obj, result| result.collect {|e| e*4} }
|
30
|
+
}
|
31
|
+
proxy.should_not be_nil
|
32
|
+
proxy.reverse.should == [12, 8, 4]
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'should add both, callbefore and callafter' do
|
36
|
+
array = [1, 2, 3]
|
37
|
+
proxy = Proxy.new array,
|
38
|
+
:before => {
|
39
|
+
:reverse => lambda {|obj| obj.map! {|e| e*2} }
|
40
|
+
},
|
41
|
+
:after => {
|
42
|
+
:reverse => lambda {|obj, result| result.collect {|e| e/2} }
|
43
|
+
}
|
44
|
+
proxy.should_not be_nil
|
45
|
+
proxy.reverse.should == [3, 2, 1]
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
context 'for all methods' do
|
51
|
+
|
52
|
+
it 'should add a callbefore' do
|
53
|
+
array = [3, 2, 1]
|
54
|
+
proxy = Proxy.new array, :before_all => lambda {|obj| obj.sort!}
|
55
|
+
proxy.should_not be_nil
|
56
|
+
proxy.reverse.should == [3, 2, 1]
|
57
|
+
proxy.max.should == 3
|
58
|
+
proxy.first.should == 1
|
59
|
+
proxy.to_s.should == "123"
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'should add a callafter' do
|
63
|
+
array = [1, 2, 3]
|
64
|
+
|
65
|
+
proxy = Proxy.new array, :after_all => lambda {|obj, result|
|
66
|
+
return result * 10 if result.class == Fixnum
|
67
|
+
result
|
68
|
+
}
|
69
|
+
|
70
|
+
proxy.should_not be_nil
|
71
|
+
proxy.reverse.should == [3, 2, 1]
|
72
|
+
proxy.max.should == 30 # 3 * 10
|
73
|
+
proxy.first.should == 10 # 1 * 10
|
74
|
+
proxy.to_s.should == "123"
|
75
|
+
end
|
76
|
+
|
77
|
+
it 'should add both, callbefore and callafter' do
|
78
|
+
array = [3, 2, 1]
|
79
|
+
proxy = Proxy.new array,
|
80
|
+
:before_all => lambda {|obj| obj.sort!},
|
81
|
+
:after_all => lambda {|obj, result|
|
82
|
+
return result * 10 if result.class == Fixnum
|
83
|
+
result
|
84
|
+
}
|
85
|
+
proxy.should_not be_nil
|
86
|
+
proxy.reverse.should == [3, 2, 1]
|
87
|
+
proxy.max.should == 30 # 3 * 10
|
88
|
+
proxy.first.should == 10 # 1 * 10
|
89
|
+
proxy.to_s.should == "123"
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|
93
|
+
|
94
|
+
context 'for kernel method' do
|
95
|
+
|
96
|
+
it 'should call proxy' do
|
97
|
+
array = [1, 2, 3]
|
98
|
+
proxy = proxy_for array
|
99
|
+
proxy.should_not be_nil
|
100
|
+
proxy.class.should == array.class
|
101
|
+
|
102
|
+
proxy.reverse.should == array.reverse
|
103
|
+
proxy.proxied_class?.should be_true
|
104
|
+
end
|
105
|
+
|
106
|
+
it 'should call proxy passing arguments' do
|
107
|
+
array = [1, 2, 3]
|
108
|
+
proxy = proxy_for array, :before => {
|
109
|
+
:reverse => lambda {|obj| obj << 50 }
|
110
|
+
}
|
111
|
+
proxy.should_not be_nil
|
112
|
+
proxy.reverse.should == [50, 3, 2, 1]
|
113
|
+
end
|
114
|
+
|
115
|
+
end
|
116
|
+
|
117
|
+
end
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
|
144
|
+
|
metadata
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: proxy_machine
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- "T\xC3\xBAlio Ornelas"
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-10-03 00:00:00 -03:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: A cool proxy implementation pattern in ruby
|
23
|
+
email: ornelas.tulio@gmail.com
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files:
|
29
|
+
- LICENSE
|
30
|
+
- README.rdoc
|
31
|
+
files:
|
32
|
+
- LICENSE
|
33
|
+
- README.rdoc
|
34
|
+
- Rakefile
|
35
|
+
- VERSION
|
36
|
+
- init.rb
|
37
|
+
- lib/kernel.rb
|
38
|
+
- lib/proxy_machine.rb
|
39
|
+
- lib/proxy_machine/proxy.rb
|
40
|
+
- proxy_machine.gemspec
|
41
|
+
- spec/proxy_spec.rb
|
42
|
+
has_rdoc: true
|
43
|
+
homepage: http://github.com/tulios/proxy_machine
|
44
|
+
licenses: []
|
45
|
+
|
46
|
+
post_install_message:
|
47
|
+
rdoc_options:
|
48
|
+
- --charset=UTF-8
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
hash: 3
|
57
|
+
segments:
|
58
|
+
- 0
|
59
|
+
version: "0"
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
hash: 3
|
66
|
+
segments:
|
67
|
+
- 0
|
68
|
+
version: "0"
|
69
|
+
requirements: []
|
70
|
+
|
71
|
+
rubyforge_project:
|
72
|
+
rubygems_version: 1.3.7
|
73
|
+
signing_key:
|
74
|
+
specification_version: 3
|
75
|
+
summary: A cool proxy implementation pattern in ruby
|
76
|
+
test_files:
|
77
|
+
- spec/proxy_spec.rb
|