proxy_machine 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -12,7 +12,11 @@ A cool proxy implementation pattern in ruby
12
12
 
13
13
  p = proxy_for [1, 2, 3]
14
14
  p.reverse => [3, 2, 1]
15
-
15
+
16
+ === Defining callbefores and callafters in method level
17
+
18
+ Callbefores:
19
+
16
20
  p = proxy_for [1, 2, 3], :before => {
17
21
  :reverse => lambda {|obj| puts 'before reverse'}
18
22
  }
@@ -20,6 +24,55 @@ A cool proxy implementation pattern in ruby
20
24
  p.reverse => before reverse
21
25
  [3, 2, 1]
22
26
 
27
+ Callafters:
28
+
29
+ p = proxy_for [1, 2, 3], :after => {
30
+ :reverse => lambda {|obj, result| result.sort}
31
+ }
32
+
33
+ p.reverse => [1, 2, 3] # We reordered the list
34
+
35
+ === Defining callbefores and callafters in all method calls
36
+
37
+ Callbefores:
38
+
39
+ p = proxy_for [1, 2, 3], :before_all => lambda {|obj| puts 'before all'}
40
+ p.reverse => before all
41
+ [3, 2, 1]
42
+
43
+ p.size => before all
44
+ 3
45
+
46
+ Callafters:
47
+
48
+ p = proxy_for [1, 2, 3], :after_all => lambda {|obj, result| puts result}
49
+ p.reverse => [1, 2, 3]
50
+ [1, 2, 3] # puts
51
+
52
+ p.size => 3
53
+ 3 # puts
54
+
55
+ === Registering a class to perform the callafter or callbefore
56
+
57
+ The constructor will receive the object, in case of a callback it will receive the result too.
58
+ 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.
60
+
61
+ # Example of class
62
+ class SortPerformer
63
+ def initialize object, result = nil
64
+ @object = object; @result = result
65
+ end
66
+
67
+ def call; @object.sort! end
68
+ end
69
+
70
+ p = proxy_for [1, 2, 3], :after => {
71
+ :reverse => SortPerformer
72
+ }
73
+
74
+ p.reverse => [1, 2, 3]
75
+
23
76
  === How to detect that the object is a proxy?
24
77
 
25
78
  The beautiful way
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.0.3
@@ -1,38 +1,7 @@
1
1
  module ProxyMachine
2
2
 
3
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
- #
4
+
36
5
  def initialize object, calls = nil
37
6
  @object = object
38
7
  @before = @before_all = @after = @after_all = nil
@@ -45,42 +14,38 @@ module ProxyMachine
45
14
  end
46
15
  end
47
16
 
48
- def method_missing(symbol, *args)
17
+ def method_missing(symbol, *args)
18
+ @symbol = symbol
49
19
  method = symbol.to_s
50
20
  raise NoMethodError.new(method) unless @object.methods.member?(method)
51
21
 
52
- execute_call(@before_all, symbol)
53
- execute_call(@before, symbol)
22
+ execute_call(@before_all, @object)
23
+ execute_call(@before, @object)
54
24
 
55
- result = @object.send(method, *args)
25
+ @result = @object.send(method, *args)
56
26
 
57
- result_after = execute_call_with_result(@after, symbol, result)
58
- result_after_all = execute_call_with_result(@after_all, symbol, result)
27
+ result_after = execute_call(@after, @object, @result)
28
+ result_after_all = execute_call(@after_all, @object, @result)
59
29
 
60
30
  return result_after_all if result_after_all
61
31
  return result_after if result_after
62
- result
32
+ @result
63
33
  end
64
34
 
65
35
  def proxied_class?; true end
66
36
 
67
37
  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
38
+ def execute_call container, *args
39
+ executor = get_executor(container)
40
+
41
+ return executor.send :call, *args if proc?(executor)
42
+ return executor.send(:new, *args).send :call if class?(executor)
78
43
  end
79
44
 
80
- def get_block container, method_symbol
45
+ def get_executor container
81
46
  if container
82
- return container if container.class == Proc
83
- return container[method_symbol]
47
+ return container if proc?(container) or class?(container)
48
+ return container[@symbol]
84
49
  end
85
50
  end
86
51
 
@@ -88,6 +53,10 @@ module ProxyMachine
88
53
  block and block.class == Proc
89
54
  end
90
55
 
56
+ def class? param
57
+ param and param.class == Class
58
+ end
59
+
91
60
  end
92
61
 
93
62
  end
@@ -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.2"
8
+ s.version = "0.0.3"
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-05}
12
+ s.date = %q{2010-10-19}
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 = [
data/spec/proxy_spec.rb CHANGED
@@ -12,6 +12,12 @@ describe Proxy do
12
12
  proxy.proxied_class?.should be_true
13
13
  end
14
14
 
15
+ it 'should detect a proxy object with proxied_class? method' do
16
+ array = [1, 2, 3]
17
+ proxy = Proxy.new array
18
+ (defined? proxy.proxied_class?).should_not be_nil
19
+ end
20
+
15
21
  context 'for a certain method' do
16
22
 
17
23
  it 'should add a callbefore' do
@@ -113,6 +119,47 @@ describe Proxy do
113
119
  end
114
120
 
115
121
  end
122
+
123
+ context 'for object method' do
124
+
125
+ it 'should detect proxied objects' do
126
+ array1 = [1, 2, 3]
127
+ array1.proxied?.should be_false
128
+ array2 = proxy_for [1, 2, 3]
129
+ array2.proxied?.should be_true
130
+ end
131
+
132
+ end
133
+
134
+ context 'when registering a class' do
135
+ # Example of class
136
+ class SortPerformer
137
+ def initialize object, result = nil
138
+ @object = object; @result = result
139
+ end
140
+
141
+ def call; @object.sort! end
142
+ end
143
+
144
+ it 'should use a instance of the registered class for a callafter' do
145
+ array = [1, 2, 3]
146
+ proxy = proxy_for array, :after => {
147
+ :reverse => SortPerformer
148
+ }
149
+ proxy.should_not be_nil
150
+ proxy.reverse.should == [1, 2, 3]
151
+ end
152
+
153
+ it 'should use a instance of the registered class for a callbefore' do
154
+ array = [3, 2, 1]
155
+ proxy = proxy_for array, :before => {
156
+ :reverse => SortPerformer
157
+ }
158
+ proxy.should_not be_nil
159
+ proxy.reverse.should == [3, 2, 1]
160
+ end
161
+
162
+ end
116
163
 
117
164
  end
118
165
 
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: 27
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
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-05 00:00:00 -03:00
18
+ date: 2010-10-19 00:00:00 -02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency