proxy_machine 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ nbproject
data/README.rdoc CHANGED
@@ -20,7 +20,7 @@ A cool proxy implementation pattern in ruby
20
20
  Callbefores:
21
21
 
22
22
  p = proxy_for [1, 2, 3], :before => {
23
- :reverse => lambda {|obj| puts 'before reverse'}
23
+ :reverse => lambda {|obj, args| puts 'before reverse'}
24
24
  }
25
25
 
26
26
  p.reverse => before reverse
@@ -29,16 +29,18 @@ Callbefores:
29
29
  Callafters:
30
30
 
31
31
  p = proxy_for [1, 2, 3], :after => {
32
- :reverse => lambda {|obj, result| result.sort}
32
+ :reverse => lambda {|obj, result, args| result.sort}
33
33
  }
34
34
 
35
35
  p.reverse => [1, 2, 3] # We reordered the list
36
+
37
+ You will always receive the arguments passed to the original method.
36
38
 
37
- === Defining callbefores and callafters in all method calls
39
+ === Defining callbefores and callafters for all method calls
38
40
 
39
41
  Callbefores:
40
42
  This callback will receive a reference of the object, the symbol of the called method and the
41
- arguments passed.
43
+ original arguments passed.
42
44
 
43
45
  p = proxy_for [1, 2, 3], :before_all => lambda {|obj, method, args| puts 'before all'}
44
46
  p.reverse => before all
@@ -81,7 +83,7 @@ every time it need to use it. You could use this feature with the before_all and
81
83
 
82
84
  === Controlling the method execution with regexp
83
85
 
84
- For before_all and after_all you could use regexp to configure whicth methods will be affected.
86
+ For before_all and after_all you could use regexp to configure which methods will be affected.
85
87
 
86
88
  # Example of class
87
89
  class MyRegexMethods
@@ -92,21 +94,21 @@ For before_all and after_all you could use regexp to configure whicth methods wi
92
94
  def crazy_one; @value ? @value : 'crazy' end
93
95
  end
94
96
 
95
- p = proxy_for MyRegexMethods.new, :before_all => {
96
- :call1 => [/^get_/, lambda {|obj, method, args| obj.value = 'gotcha!' }],
97
- }
97
+ p = proxy_for MyRegexMethods.new, :before_all => [
98
+ [/^get_/, lambda {|obj, method, args| obj.value = 'gotcha!' }]
99
+ ]
98
100
 
99
101
  p.get_value1 => gotcha!
100
102
  p.get_value2 => gotcha!
101
103
  p.another_method => 'another
102
104
  proxy.crazy_one => 'crazy'
103
105
 
104
- You could use many definitions if you want, the calls will happen in alphabetical order of the keys.
106
+ You could use many definitions if you want, the calls will happen in the declared order.
105
107
 
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
- }
108
+ p = proxy_for MyRegexMethods.new, :before_all => [
109
+ [/get_/, lambda {|obj, method, args| obj.value = "it_"}]
110
+ [/value/, lambda {|obj, method, args| obj.value = "#{obj.value}works"}]
111
+ ]
110
112
 
111
113
  p.get_value1 => it_works
112
114
  p.get_value2 => it_works
@@ -124,16 +126,43 @@ It is also possible to use classes instead of procs.
124
126
  def call; @object.value = "#{@object.value}works" end
125
127
  end
126
128
 
127
- p = proxy_for MyRegexMethods.new, :before_all => {
128
- :call2 => [/value/, Change2Performer],
129
- :call1 => [/get_/, lambda {|obj, method, args| obj.value = "it_"}]
130
- }
129
+ p = proxy_for MyRegexMethods.new, :before_all => [
130
+ [/get_/, lambda {|obj, method, args| obj.value = "it_"}],
131
+ [/value/, Change2Performer]
132
+ ]
131
133
 
132
134
  p.get_value1 => it_works
133
135
  p.get_value2 => it_works
134
136
  p.another_method => another
135
137
  p.crazy_one => crazy
138
+
139
+ === Building an execution stack
140
+
141
+ # Example of class
142
+ class StackClass
143
+ attr_accessor :name, :company_name
144
+ end
145
+
146
+ # Performers
147
+ make_upper = lambda {|obj, args| obj.name = obj.name.upcase }
148
+ make_without_space = lambda {|obj, args| obj.name = obj.name.gsub /\s+/, '-'}
149
+ make_round_brackets = lambda {|obj, args| obj.name = "(#{obj.name})" }
150
+
151
+ make_lower = lambda {|obj, args| obj.company_name = obj.company_name.downcase }
152
+ make_round_brackets2 = lambda {|obj, args| obj.company_name = "[#{obj.company_name}]" }
136
153
 
154
+ obj = StackClass.new
155
+ obj.name = "important name"
156
+ obj.company_name = "COMPANY NAME"
157
+
158
+ p = proxy_for obj, :before => {
159
+ :name => [make_upper, make_without_space, make_round_brackets],
160
+ :company_name => [make_lower, make_round_brackets2]
161
+ }
162
+
163
+ p.name => (IMPORTANT-NAME)
164
+ p.company_name => [company name]
165
+
137
166
  === How to detect that the object is a proxy?
138
167
 
139
168
  The beautiful way:
data/Rakefile CHANGED
@@ -8,7 +8,8 @@ begin
8
8
  gemspec.homepage = "http://github.com/tulios/proxy_machine"
9
9
  gemspec.authors = ["Túlio Ornelas"]
10
10
  gemspec.test_files = Dir.glob('spec/*_spec.rb')
11
- gemspec.add_development_dependency "rspec", ">= 1.2.9"
11
+ gemspec.add_development_dependency "rspec", ">= 2.0.1"
12
+ gemspec.add_development_dependency "rspec-core", ">= 2.0.1"
12
13
  end
13
14
  Jeweler::GemcutterTasks.new
14
15
  rescue LoadError
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.4
1
+ 0.0.5
@@ -19,18 +19,18 @@ module ProxyMachine
19
19
  end
20
20
 
21
21
  def method_missing(symbol, *args)
22
- @symbol = symbol
23
- @method = symbol.to_s
24
-
22
+ @method_symbol = symbol
23
+ @method = @method_symbol.to_s
24
+
25
25
  raise NoMethodError.new(@method) unless @allow_dinamic or @object.methods.member?(@method)
26
26
 
27
- execute_call(@before_all, @object, symbol, args)
28
- execute_call(@before, @object)
27
+ execute_call(@before_all, @object, @method_symbol, args)
28
+ execute_call(@before, @object, args)
29
29
 
30
30
  @result = @avoid_original_execution ? nil : @object.send(@method, *args)
31
31
 
32
- result_after = execute_call(@after, @object, @result)
33
- result_after_all = execute_call(@after_all, @object, @result, symbol, args)
32
+ result_after = execute_call(@after, @object, @result, args)
33
+ result_after_all = execute_call(@after_all, @object, @result, @method_symbol, args)
34
34
 
35
35
  return result_after_all if result_after_all
36
36
  return result_after if result_after
@@ -43,7 +43,6 @@ module ProxyMachine
43
43
  private
44
44
  def execute_call container, *args
45
45
  executor = get_executor(container)
46
-
47
46
  result = nil
48
47
  if executor.class == Array
49
48
  executor.each do |e|
@@ -59,22 +58,21 @@ module ProxyMachine
59
58
 
60
59
  def get_executor container
61
60
  return nil unless container
62
-
61
+
63
62
  # The content is a proc or a class
64
63
  return container if proc?(container) or class?(container)
65
64
 
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)
65
+ # The content is an array with an array filled with a regex and a proc or a class
66
+ if array?(container) and regexp?(container)
68
67
  matched = regexp_elements(container).select {|array| get_regexp(array) =~ @method}
69
68
  return matched.collect {|array| get_proc_or_class(array)} unless matched.empty?
70
69
  end
71
70
 
72
- # Lets assume that the content of the key is a proc
73
- container[@symbol]
71
+ hash?(container) ? container[@method_symbol] : container
74
72
  end
75
73
 
76
- def regexp_elements hash
77
- elements = hash.keys.sort.collect {|key| array_with_regex?(hash[key]) ? hash[key] : nil}
74
+ def regexp_elements array
75
+ elements = array.collect {|sub_array| array_with_regex?(sub_array) ? sub_array : nil}
78
76
  compacted_array = elements.compact
79
77
  compacted_array.nil? ? [] : compacted_array
80
78
  end
@@ -93,8 +91,9 @@ module ProxyMachine
93
91
 
94
92
  def proc? block; block and block.class == Proc end
95
93
  def class? param; param and param.class == Class end
94
+ def array? param; param and param.class == Array end
96
95
  def hash? param; param and param.class == Hash end
97
- def regexp? hash; hash and not regexp_elements(hash).empty? end
96
+ def regexp? array; array and not regexp_elements(array).empty? end
98
97
 
99
98
  end
100
99
 
@@ -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.4"
8
+ s.version = "0.0.5"
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-25}
12
+ s.date = %q{2010-11-02}
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 = [
@@ -17,7 +17,8 @@ Gem::Specification.new do |s|
17
17
  "README.rdoc"
18
18
  ]
19
19
  s.files = [
20
- "LICENSE",
20
+ ".gitignore",
21
+ "LICENSE",
21
22
  "README.rdoc",
22
23
  "Rakefile",
23
24
  "VERSION",
@@ -28,6 +29,7 @@ Gem::Specification.new do |s|
28
29
  "lib/proxy_machine/proxy.rb",
29
30
  "lib/symbol.rb",
30
31
  "proxy_machine.gemspec",
32
+ "script/console",
31
33
  "spec/proxy_spec.rb"
32
34
  ]
33
35
  s.homepage = %q{http://github.com/tulios/proxy_machine}
@@ -44,12 +46,15 @@ Gem::Specification.new do |s|
44
46
  s.specification_version = 3
45
47
 
46
48
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
47
- s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
49
+ s.add_development_dependency(%q<rspec>, [">= 2.0.1"])
50
+ s.add_development_dependency(%q<rspec-core>, [">= 2.0.1"])
48
51
  else
49
- s.add_dependency(%q<rspec>, [">= 1.2.9"])
52
+ s.add_dependency(%q<rspec>, [">= 2.0.1"])
53
+ s.add_dependency(%q<rspec-core>, [">= 2.0.1"])
50
54
  end
51
55
  else
52
- s.add_dependency(%q<rspec>, [">= 1.2.9"])
56
+ s.add_dependency(%q<rspec>, [">= 2.0.1"])
57
+ s.add_dependency(%q<rspec-core>, [">= 2.0.1"])
53
58
  end
54
59
  end
55
60
 
data/script/console ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+ # File: script/console
3
+ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
4
+
5
+ libs = " -r irb/completion"
6
+ # Perhaps use a console_lib to store any extra methods I may want available in the cosole
7
+ # libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
8
+ libs << " -r #{File.dirname(__FILE__) + '/../lib/proxy_machine.rb'}"
9
+
10
+ puts "Loading proxy_machine gem"
11
+ exec "#{irb} #{libs} --simple-prompt"
data/spec/proxy_spec.rb CHANGED
@@ -23,7 +23,7 @@ describe Proxy do
23
23
  it 'should add a callbefore' do
24
24
  array = [1, 2, 3]
25
25
  proxy = Proxy.new array, :before => {
26
- :reverse => lambda {|obj| obj << 50 }
26
+ :reverse => lambda {|obj, args| obj << 50}
27
27
  }
28
28
  proxy.should_not be_nil
29
29
  proxy.reverse.should == [50, 3, 2, 1]
@@ -32,7 +32,7 @@ describe Proxy do
32
32
  it 'should add a callafter' do
33
33
  array = [1, 2, 3]
34
34
  proxy = Proxy.new array, :after => {
35
- :reverse => lambda {|obj, result| result.collect {|e| e*4} }
35
+ :reverse => lambda {|obj, result, args| result.collect {|e| e*4} }
36
36
  }
37
37
  proxy.should_not be_nil
38
38
  proxy.reverse.should == [12, 8, 4]
@@ -42,10 +42,10 @@ describe Proxy do
42
42
  array = [1, 2, 3]
43
43
  proxy = Proxy.new array,
44
44
  :before => {
45
- :reverse => lambda {|obj| obj.map! {|e| e*2} }
45
+ :reverse => lambda {|obj, args| obj.map! {|e| e*2} }
46
46
  },
47
47
  :after => {
48
- :reverse => lambda {|obj, result| result.collect {|e| e/2} }
48
+ :reverse => lambda {|obj, result, args| result.collect {|e| e/2} }
49
49
  }
50
50
  proxy.should_not be_nil
51
51
  proxy.reverse.should == [3, 2, 1]
@@ -143,7 +143,7 @@ describe Proxy do
143
143
  it 'should call proxy passing arguments' do
144
144
  array = [1, 2, 3]
145
145
  proxy = proxy_for array, :before => {
146
- :reverse => lambda {|obj| obj << 50 }
146
+ :reverse => lambda {|obj, args| obj << 50 }
147
147
  }
148
148
  proxy.should_not be_nil
149
149
  proxy.reverse.should == [50, 3, 2, 1]
@@ -219,7 +219,7 @@ describe Proxy do
219
219
  obj.value = "Crazy Value"
220
220
 
221
221
  proxy = proxy_for obj, :allow_dinamic => true, :before => {
222
- :crazy_method => lambda {|obj| obj.value = "proxied value!" }
222
+ :crazy_method => lambda {|obj, args| obj.value = "proxied value!" }
223
223
  }
224
224
 
225
225
  proxy.should_not be_nil
@@ -273,10 +273,10 @@ describe Proxy do
273
273
 
274
274
  it 'should affect just the matched methods on callbefore' do
275
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
- }
276
+ proxy = proxy_for obj, :before_all => [
277
+ [/^get_/, lambda {|obj, method, args| obj.value = 'gotcha!' }],
278
+ [/method$/, lambda {|obj, method, args| obj.value = 'another gotcha!' }]
279
+ ]
280
280
 
281
281
  proxy.should_not be_nil
282
282
 
@@ -295,10 +295,10 @@ describe Proxy do
295
295
 
296
296
  it 'should affect just the matched methods on callafter' do
297
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
- }
298
+ proxy = proxy_for obj, :after_all => [
299
+ [/^get_/, lambda {|obj, result, method, args| obj.value = 'gotcha!' }],
300
+ [/method$/, lambda {|obj, result, method, args| obj.value = 'another gotcha!'}]
301
+ ]
302
302
 
303
303
  proxy.should_not be_nil
304
304
 
@@ -315,34 +315,12 @@ describe Proxy do
315
315
  proxy.crazy_one.should == 'crazy'
316
316
  end
317
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
318
  it 'should ensure that the last value returned is the value of the last called method' do
341
319
  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
- }
320
+ proxy = proxy_for obj, :after_all => [
321
+ [/get_/, lambda {|obj, result, method, args| 1}],
322
+ [/value/, lambda {|obj, result, method, args| 2}]
323
+ ]
346
324
 
347
325
  proxy.should_not be_nil
348
326
 
@@ -361,10 +339,10 @@ describe Proxy do
361
339
 
362
340
  it 'should affect just the matched methods using a registered class' do
363
341
  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
- }
342
+ proxy = proxy_for obj, :before_all => [
343
+ [/^get_/, Change1Performer],
344
+ [/method$/, lambda {|obj, method, args| obj.value = 'another gotcha!' }]
345
+ ]
368
346
 
369
347
  proxy.should_not be_nil
370
348
 
@@ -383,10 +361,10 @@ describe Proxy do
383
361
 
384
362
  it 'should execute the entire matched stack sorted by the key name using a registered class' do
385
363
  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
- }
364
+ proxy = proxy_for obj, :before_all => [
365
+ [/get_/, lambda {|obj, method, args| obj.value = "it_"}],
366
+ [/value/, Change2Performer]
367
+ ]
390
368
 
391
369
  proxy.should_not be_nil
392
370
 
@@ -404,7 +382,40 @@ describe Proxy do
404
382
  end
405
383
 
406
384
  end
407
-
385
+
386
+ context 'creating an execution stack' do
387
+ class StackClass
388
+ attr_accessor :name
389
+ def do_logic var
390
+ @name = "#{@name}_#{var}"
391
+ end
392
+ end
393
+
394
+ it 'should execute the entire stack over a single proxied method' do
395
+ obj = StackClass.new
396
+ obj.name = "important name"
397
+
398
+ make_upper = lambda {|obj, args| obj.name = obj.name.upcase }
399
+ make_without_space = lambda {|obj, args| obj.name = obj.name.gsub /\s+/, '-'}
400
+ make_round_brackets = lambda {|obj, args| obj.name = "(#{obj.name})" }
401
+
402
+ make_lower = lambda {|obj, args| args[0].downcase! }
403
+ make_round_brackets2 = lambda {|obj, args| args[0] = "[#{args[0]}]" }
404
+
405
+ proxy = proxy_for obj, :before => {
406
+ :name => [make_upper, make_without_space, make_round_brackets],
407
+ :do_logic => [make_lower, make_round_brackets2]
408
+ }
409
+
410
+ proxy.should_not be_nil
411
+ proxy.name.should == "(IMPORTANT-NAME)"
412
+
413
+ proxy.do_logic "MY NAME"
414
+ proxy.original_object.name.should == "(IMPORTANT-NAME)_[my name]"
415
+ end
416
+
417
+ end
418
+
408
419
  end
409
420
 
410
421
 
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: 23
4
+ hash: 21
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 4
10
- version: 0.0.4
9
+ - 5
10
+ version: 0.0.5
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-25 00:00:00 -02:00
18
+ date: 2010-11-02 00:00:00 -02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -28,12 +28,28 @@ dependencies:
28
28
  - !ruby/object:Gem::Version
29
29
  hash: 13
30
30
  segments:
31
- - 1
32
31
  - 2
33
- - 9
34
- version: 1.2.9
32
+ - 0
33
+ - 1
34
+ version: 2.0.1
35
35
  type: :development
36
36
  version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: rspec-core
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 13
46
+ segments:
47
+ - 2
48
+ - 0
49
+ - 1
50
+ version: 2.0.1
51
+ type: :development
52
+ version_requirements: *id002
37
53
  description: A cool proxy implementation pattern in ruby
38
54
  email: ornelas.tulio@gmail.com
39
55
  executables: []
@@ -44,6 +60,7 @@ extra_rdoc_files:
44
60
  - LICENSE
45
61
  - README.rdoc
46
62
  files:
63
+ - .gitignore
47
64
  - LICENSE
48
65
  - README.rdoc
49
66
  - Rakefile
@@ -55,6 +72,7 @@ files:
55
72
  - lib/proxy_machine/proxy.rb
56
73
  - lib/symbol.rb
57
74
  - proxy_machine.gemspec
75
+ - script/console
58
76
  - spec/proxy_spec.rb
59
77
  has_rdoc: true
60
78
  homepage: http://github.com/tulios/proxy_machine