method_decorator 2.1.0 → 3.0.0
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.
- checksums.yaml +4 -4
- data/README.rdoc +39 -7
- data/lib/method_decorator.rb +30 -2
- data/lib/method_decorator/decoration.rb +1 -1
- data/lib/method_decorator/version.rb +1 -1
- data/spec/lib/method_decorator_spec.rb +58 -15
- data/spec/support/dummy_class.rb +49 -18
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2122ba1d4785e5f9f7a85e9b2d1d87cebdc80790
|
4
|
+
data.tar.gz: ed85eb225a92395e20a39e5e407f872dd1fa6552
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6473ef9b300a885973c49515ebaa926b9bf961099ce8117440c85b61f92706cd0a4e53cecf648e2937e3efa0bd6f606c53fed671c713929e9cd3c484e621d0a9
|
7
|
+
data.tar.gz: 5d129bad827bb7ac3eb4c5e4df66b566d71d163e0f83e1b29a1a6961e9be746c96324968d69a6b254afb49d14a7cbd51822158181f607f3d7df3da522024e5bc
|
data/README.rdoc
CHANGED
@@ -14,9 +14,9 @@ For a given class:
|
|
14
14
|
|
15
15
|
end
|
16
16
|
|
17
|
-
MethodDecorator.decorate SomeClass, :some_method do
|
18
|
-
puts :
|
19
|
-
|
17
|
+
MethodDecorator.decorate SomeClass, :some_method do
|
18
|
+
puts "decorated_some_method_with: #{call_args}"
|
19
|
+
call_original
|
20
20
|
end
|
21
21
|
|
22
22
|
This call:
|
@@ -25,9 +25,37 @@ This call:
|
|
25
25
|
|
26
26
|
Produces this output:
|
27
27
|
|
28
|
-
|
28
|
+
decorated_some_method_with: ["some arg"]
|
29
29
|
some arg
|
30
30
|
|
31
|
+
== Helper Methods
|
32
|
+
|
33
|
+
Inside the block given to `MethodDecorator.decorate`, you can call four helper methods:
|
34
|
+
|
35
|
+
call_args # returns original arguments given to the call
|
36
|
+
call_block # returns original block given to the call
|
37
|
+
call_original # call the original method, with original args and block given to the call
|
38
|
+
call_original_with # call the original method, with desired args and block
|
39
|
+
|
40
|
+
== Module inclusion
|
41
|
+
|
42
|
+
You can call `decorate` method directly instead of calling `MethodDecorator.decorate`. Just include `MethodDecorator` module:
|
43
|
+
|
44
|
+
class SomeClass
|
45
|
+
|
46
|
+
def some_method(some_arg)
|
47
|
+
puts some_arg
|
48
|
+
end
|
49
|
+
|
50
|
+
include MethodDecorator
|
51
|
+
|
52
|
+
decorate :some_method do
|
53
|
+
puts "decorated_some_method_with: #{call_args}"
|
54
|
+
call_original
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
|
31
59
|
== Singleton Classes Support
|
32
60
|
|
33
61
|
It works too:
|
@@ -44,13 +72,17 @@ It works too:
|
|
44
72
|
|
45
73
|
end
|
46
74
|
|
47
|
-
MethodDecorator.decorate SomeClass.singleton_class, :some_method do
|
48
|
-
puts :
|
49
|
-
|
75
|
+
MethodDecorator.decorate SomeClass.singleton_class, :some_method do
|
76
|
+
puts "decorated_some_method_with: #{call_args}"
|
77
|
+
call_original
|
50
78
|
end
|
51
79
|
|
52
80
|
SomeClass.some_method 'some arg'
|
53
81
|
|
82
|
+
== More
|
83
|
+
|
84
|
+
To see more of `MethodDecorator` usages, please take a look at DummyClass[https://github.com/r4z3c/method_decorator/blob/master/spec/support/dummy_class.rb].
|
85
|
+
|
54
86
|
===
|
55
87
|
|
56
88
|
{<img src="https://www.paypalobjects.com/en_US/i/btn/btn_donate_LG.gif" alt="Donate" />}[https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=AMPXM3PW6CTBE]
|
data/lib/method_decorator.rb
CHANGED
@@ -1,6 +1,18 @@
|
|
1
1
|
require 'method_decorator/repository'
|
2
2
|
|
3
|
-
module MethodDecorator
|
3
|
+
module MethodDecorator extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
|
7
|
+
class << self
|
8
|
+
|
9
|
+
def decorate(target_method_name, &decoration)
|
10
|
+
MethodDecorator.decorate self, target_method_name, &decoration
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
4
16
|
|
5
17
|
class << self
|
6
18
|
|
@@ -25,11 +37,27 @@ module MethodDecorator
|
|
25
37
|
def override_method(decoration, target_class, target_method_name)
|
26
38
|
is_protected = target_class.protected_instance_methods.include? target_method_name
|
27
39
|
is_private = target_class.private_instance_methods.include? target_method_name
|
28
|
-
|
40
|
+
define_decorated_method decoration, target_class, target_method_name
|
29
41
|
target_class.instance_eval { protected target_method_name } if is_protected
|
30
42
|
target_class.instance_eval { private target_method_name } if is_private
|
31
43
|
end
|
32
44
|
|
45
|
+
def define_decorated_method(decoration, target_class, target_method_name)
|
46
|
+
target_class.send :define_method, target_method_name do |*args, &block|
|
47
|
+
target_class.send :define_method, :call_args do return *args end
|
48
|
+
target_class.send :define_method, :call_block do return block end
|
49
|
+
target_class.send :define_method, :call_original do
|
50
|
+
MethodDecorator.call_original_method self, target_method_name, *call_args, &call_block
|
51
|
+
end
|
52
|
+
target_class.send :define_method, :call_original_with do |*desired_args, &desired_block|
|
53
|
+
MethodDecorator.call_original_method self, target_method_name, *desired_args, &desired_block
|
54
|
+
end
|
55
|
+
target_class.instance_eval { protected :call_args }
|
56
|
+
target_class.instance_eval { protected :call_block }
|
57
|
+
instance_eval &decoration
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
33
61
|
public
|
34
62
|
|
35
63
|
def call_original_method(target_context, target_method_name, *original_args, &original_block)
|
@@ -3,7 +3,7 @@ module MethodDecorator
|
|
3
3
|
|
4
4
|
attr_accessor :target_class, :target_method_name, :target_method, :decoration
|
5
5
|
|
6
|
-
def initialize(target_class, target_method_name, target_method
|
6
|
+
def initialize(target_class, target_method_name, target_method)
|
7
7
|
self.target_class = target_class
|
8
8
|
self.target_method_name = target_method_name
|
9
9
|
self.target_method = target_method
|
@@ -12,27 +12,57 @@ describe MethodDecorator do
|
|
12
12
|
|
13
13
|
context 'when public method' do
|
14
14
|
|
15
|
-
|
16
|
-
before { expect(target_instance).to receive(:puts).with("a_public_instance_method_arg: #{dummy_arg}").ordered }
|
15
|
+
context 'when through module singleton method' do
|
17
16
|
|
18
|
-
|
17
|
+
before { expect(target_instance).to receive(:puts).with("a_decorated_public_instance_method_with: #{[dummy_arg]}").ordered }
|
18
|
+
before { expect(target_instance).to receive(:puts).with("a_public_instance_method_arg: #{dummy_arg}").ordered }
|
19
|
+
|
20
|
+
it { target_instance.a_public_instance_method dummy_arg }
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
context 'when through module inclusion' do
|
25
|
+
|
26
|
+
before { expect(target_instance).to receive(:puts).with("another_decorated_public_instance_method_with: #{[dummy_arg]}").ordered }
|
27
|
+
before { expect(target_instance).to receive(:puts).with("another_public_instance_method_arg: #{dummy_arg}").ordered }
|
28
|
+
|
29
|
+
it { target_instance.another_public_instance_method dummy_arg }
|
30
|
+
|
31
|
+
end
|
19
32
|
|
20
33
|
end
|
21
34
|
|
22
35
|
context 'when protected method' do
|
23
36
|
|
24
|
-
context 'when calling
|
37
|
+
context 'when calling original method through `call_original`' do
|
38
|
+
|
39
|
+
context 'when calling protected method deliberately' do
|
40
|
+
|
41
|
+
it { expect{target_instance.a_protected_instance_method dummy_arg}.to raise_error NoMethodError }
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
context 'when calling protected method through `send`' do
|
25
46
|
|
26
|
-
|
47
|
+
before { expect(target_instance).to receive(:puts).with("a_decorated_protected_instance_method_with: #{[dummy_arg]}").ordered }
|
48
|
+
before { expect(target_instance).to receive(:puts).with("a_protected_instance_method_arg: #{dummy_arg}").ordered }
|
49
|
+
|
50
|
+
it { target_instance.send :a_protected_instance_method, dummy_arg }
|
51
|
+
|
52
|
+
end
|
27
53
|
|
28
54
|
end
|
29
55
|
|
30
|
-
context 'when calling
|
56
|
+
context 'when calling original method through `call_original_with`' do
|
57
|
+
|
58
|
+
context 'when calling protected method through `send`' do
|
31
59
|
|
32
|
-
|
33
|
-
|
60
|
+
before { expect(target_instance).to receive(:puts).with("another_decorated_protected_instance_method_with: #{[dummy_arg]}").ordered }
|
61
|
+
before { expect(target_instance).to receive(:puts).with("another_protected_instance_method_arg: hue").ordered }
|
34
62
|
|
35
|
-
|
63
|
+
it { target_instance.send :another_protected_instance_method, dummy_arg }
|
64
|
+
|
65
|
+
end
|
36
66
|
|
37
67
|
end
|
38
68
|
|
@@ -48,7 +78,7 @@ describe MethodDecorator do
|
|
48
78
|
|
49
79
|
context 'when calling private method through `send`' do
|
50
80
|
|
51
|
-
before { expect(target_instance).to receive(:puts).with(:
|
81
|
+
before { expect(target_instance).to receive(:puts).with("a_decorated_private_instance_method_with: #{[dummy_arg]}").ordered }
|
52
82
|
before { expect(target_instance).to receive(:puts).with("a_private_instance_method_arg: #{dummy_arg}").ordered }
|
53
83
|
|
54
84
|
it { target_instance.send :a_private_instance_method, dummy_arg }
|
@@ -66,10 +96,23 @@ describe MethodDecorator do
|
|
66
96
|
|
67
97
|
context 'when public method' do
|
68
98
|
|
69
|
-
|
70
|
-
|
99
|
+
context 'when through module singleton method' do
|
100
|
+
|
101
|
+
before { expect(target_instance).to receive(:puts).with("a_decorated_public_singleton_method_with: #{[dummy_arg]}").ordered }
|
102
|
+
before { expect(target_instance).to receive(:puts).with("a_public_singleton_method_arg: #{dummy_arg}").ordered }
|
103
|
+
|
104
|
+
it { target_instance.a_public_singleton_method dummy_arg }
|
71
105
|
|
72
|
-
|
106
|
+
end
|
107
|
+
|
108
|
+
context 'when through module inclusion' do
|
109
|
+
|
110
|
+
before { expect(target_instance).to receive(:puts).with("another_decorated_public_singleton_method_with: #{[dummy_arg]}").ordered }
|
111
|
+
before { expect(target_instance).to receive(:puts).with("another_public_singleton_method_arg: #{dummy_arg}").ordered }
|
112
|
+
|
113
|
+
it { target_instance.another_public_singleton_method dummy_arg }
|
114
|
+
|
115
|
+
end
|
73
116
|
|
74
117
|
end
|
75
118
|
|
@@ -83,7 +126,7 @@ describe MethodDecorator do
|
|
83
126
|
|
84
127
|
context 'when calling protected method through `send`' do
|
85
128
|
|
86
|
-
before { expect(target_instance).to receive(:puts).with(:
|
129
|
+
before { expect(target_instance).to receive(:puts).with("a_decorated_protected_singleton_method_with: #{[dummy_arg]}").ordered }
|
87
130
|
before { expect(target_instance).to receive(:puts).with("a_protected_singleton_method_arg: #{dummy_arg}").ordered }
|
88
131
|
|
89
132
|
it { target_instance.send :a_protected_singleton_method, dummy_arg }
|
@@ -102,7 +145,7 @@ describe MethodDecorator do
|
|
102
145
|
|
103
146
|
context 'when calling private method through `send`' do
|
104
147
|
|
105
|
-
before { expect(target_instance).to receive(:puts).with(:
|
148
|
+
before { expect(target_instance).to receive(:puts).with("a_decorated_private_singleton_method_with: #{[dummy_arg]}").ordered }
|
106
149
|
before { expect(target_instance).to receive(:puts).with("a_private_singleton_method_arg: #{dummy_arg}").ordered }
|
107
150
|
|
108
151
|
it { target_instance.send :a_private_singleton_method, dummy_arg }
|
data/spec/support/dummy_class.rb
CHANGED
@@ -4,12 +4,20 @@ class DummyClass
|
|
4
4
|
puts "a_public_instance_method_arg: #{arg}"
|
5
5
|
end
|
6
6
|
|
7
|
+
def another_public_instance_method(arg)
|
8
|
+
puts "another_public_instance_method_arg: #{arg}"
|
9
|
+
end
|
10
|
+
|
7
11
|
protected
|
8
12
|
|
9
13
|
def a_protected_instance_method(arg)
|
10
14
|
puts "a_protected_instance_method_arg: #{arg}"
|
11
15
|
end
|
12
16
|
|
17
|
+
def another_protected_instance_method(arg)
|
18
|
+
puts "another_protected_instance_method_arg: #{arg}"
|
19
|
+
end
|
20
|
+
|
13
21
|
private
|
14
22
|
|
15
23
|
def a_private_instance_method(arg)
|
@@ -22,6 +30,10 @@ class DummyClass
|
|
22
30
|
puts "a_public_singleton_method_arg: #{arg}"
|
23
31
|
end
|
24
32
|
|
33
|
+
def another_public_singleton_method(arg)
|
34
|
+
puts "another_public_singleton_method_arg: #{arg}"
|
35
|
+
end
|
36
|
+
|
25
37
|
protected
|
26
38
|
|
27
39
|
def a_protected_singleton_method(arg)
|
@@ -34,36 +46,55 @@ class DummyClass
|
|
34
46
|
puts "a_private_singleton_method_arg: #{arg}"
|
35
47
|
end
|
36
48
|
|
49
|
+
include MethodDecorator
|
50
|
+
|
51
|
+
decorate :another_public_singleton_method do
|
52
|
+
puts "another_decorated_public_singleton_method_with: #{call_args}"
|
53
|
+
call_original
|
54
|
+
end
|
55
|
+
|
37
56
|
end
|
38
57
|
|
58
|
+
include MethodDecorator
|
59
|
+
|
60
|
+
decorate :another_public_instance_method do
|
61
|
+
puts "another_decorated_public_instance_method_with: #{call_args}"
|
62
|
+
call_original
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
|
67
|
+
MethodDecorator.decorate DummyClass, :a_public_instance_method do
|
68
|
+
puts "a_decorated_public_instance_method_with: #{call_args}"
|
69
|
+
call_original
|
39
70
|
end
|
40
71
|
|
41
|
-
MethodDecorator.decorate DummyClass, :
|
42
|
-
puts :
|
43
|
-
|
72
|
+
MethodDecorator.decorate DummyClass, :a_protected_instance_method do
|
73
|
+
puts "a_decorated_protected_instance_method_with: #{call_args}"
|
74
|
+
call_original
|
44
75
|
end
|
45
76
|
|
46
|
-
MethodDecorator.decorate DummyClass, :
|
47
|
-
puts :
|
48
|
-
|
77
|
+
MethodDecorator.decorate DummyClass, :another_protected_instance_method do
|
78
|
+
puts "another_decorated_protected_instance_method_with: #{call_args}"
|
79
|
+
call_original_with :hue
|
49
80
|
end
|
50
81
|
|
51
|
-
MethodDecorator.decorate DummyClass, :a_private_instance_method do
|
52
|
-
puts :
|
53
|
-
|
82
|
+
MethodDecorator.decorate DummyClass, :a_private_instance_method do
|
83
|
+
puts "a_decorated_private_instance_method_with: #{call_args}"
|
84
|
+
call_original
|
54
85
|
end
|
55
86
|
|
56
|
-
MethodDecorator.decorate DummyClass.singleton_class, :a_public_singleton_method do
|
57
|
-
puts :
|
58
|
-
|
87
|
+
MethodDecorator.decorate DummyClass.singleton_class, :a_public_singleton_method do
|
88
|
+
puts "a_decorated_public_singleton_method_with: #{call_args}"
|
89
|
+
call_original
|
59
90
|
end
|
60
91
|
|
61
|
-
MethodDecorator.decorate DummyClass.singleton_class, :a_protected_singleton_method do
|
62
|
-
puts :
|
63
|
-
|
92
|
+
MethodDecorator.decorate DummyClass.singleton_class, :a_protected_singleton_method do
|
93
|
+
puts "a_decorated_protected_singleton_method_with: #{call_args}"
|
94
|
+
call_original
|
64
95
|
end
|
65
96
|
|
66
|
-
MethodDecorator.decorate DummyClass.singleton_class, :a_private_singleton_method do
|
67
|
-
puts :
|
68
|
-
|
97
|
+
MethodDecorator.decorate DummyClass.singleton_class, :a_private_singleton_method do
|
98
|
+
puts "a_decorated_private_singleton_method_with: #{call_args}"
|
99
|
+
call_original
|
69
100
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: method_decorator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 3.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- r4z3c
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-02-
|
11
|
+
date: 2017-02-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|