decors 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/lib/decors.rb +1 -1
- data/lib/decors/decorator_base.rb +5 -3
- data/lib/decors/method_added.rb +9 -5
- data/spec/decors_spec.rb +30 -3
- 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: 2bb31ed84c4aa6d83763802dd3ffed2c76b3fdd8
|
4
|
+
data.tar.gz: 60f07758499467ca63547d13bcff6354a845d3a9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8a2651655043a8bc0742fe60db5385989d7e415ae7a2728a3247b61934d7069af5a8b4b02cebde2523228433b0f278aa5805ab273886b7636dae829f725deb06
|
7
|
+
data.tar.gz: ac7ee42f12a9945822fd48017e33977d7b1a364a2ab20b157d6293cbc0847f73254e4a54d0d2dee8f754c45b6e7147c03530dad9033c10ecd55fce356f6e8502
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,10 @@
|
|
1
1
|
# Decors changelog
|
2
2
|
|
3
|
+
## Version 0.3 (Released August 24, 2017)
|
4
|
+
|
5
|
+
- (feature) expose decorated method before and after decoration (https://github.com/getbannerman/decors/issues/10)
|
6
|
+
:warning: this is a breaking change. `decorated_method` that was referring to the method before decoration now refers to the method after decoration. `undecorated_method` refers to the method before decoration.
|
7
|
+
|
3
8
|
## Version 0.2 (Released May 10, 2017)
|
4
9
|
|
5
10
|
- (bugfix) nested singleton handling (https://github.com/getbannerman/decors/issues/2)
|
data/lib/decors.rb
CHANGED
@@ -1,9 +1,11 @@
|
|
1
1
|
module Decors
|
2
2
|
class DecoratorBase
|
3
|
-
attr_reader :decorated_class, :
|
3
|
+
attr_reader :decorated_class, :undecorated_method, :decorated_method, :decorator_args,
|
4
|
+
:decorator_kwargs, :decorator_block
|
4
5
|
|
5
|
-
def initialize(decorated_class, decorated_method, *args, **kwargs, &block)
|
6
|
+
def initialize(decorated_class, undecorated_method, decorated_method, *args, **kwargs, &block)
|
6
7
|
@decorated_class = decorated_class
|
8
|
+
@undecorated_method = undecorated_method
|
7
9
|
@decorated_method = decorated_method
|
8
10
|
@decorator_args = args
|
9
11
|
@decorator_kwargs = kwargs
|
@@ -15,7 +17,7 @@ module Decors
|
|
15
17
|
end
|
16
18
|
|
17
19
|
def undecorated_call(instance, *args, &block)
|
18
|
-
|
20
|
+
undecorated_method.bind(instance).call(*args, &block)
|
19
21
|
end
|
20
22
|
|
21
23
|
def decorated_method_name
|
data/lib/decors/method_added.rb
CHANGED
@@ -5,6 +5,10 @@ module Decors
|
|
5
5
|
module Handler
|
6
6
|
private
|
7
7
|
|
8
|
+
METHOD_CALLED_TOO_EARLY_HANDLER = ->(*) {
|
9
|
+
raise 'You cannot call a decorated method before its decorator is initialized'
|
10
|
+
}
|
11
|
+
|
8
12
|
def declared_decorators
|
9
13
|
@declared_decorators ||= []
|
10
14
|
end
|
@@ -15,14 +19,14 @@ module Decors
|
|
15
19
|
return if @_ignore_additions || declared_decorators.empty?
|
16
20
|
decorator_class, params, blk = declared_decorators.pop
|
17
21
|
|
22
|
+
undecorated_method = clazz.instance_method(method_name)
|
23
|
+
decorator = METHOD_CALLED_TOO_EARLY_HANDLER
|
24
|
+
clazz.send(:define_method, method_name) { |*args, &block| decorator.call(self, *args, &block) }
|
18
25
|
decorated_method = clazz.instance_method(method_name)
|
19
|
-
|
20
26
|
@_ignore_additions = true
|
21
|
-
decorator = decorator_class.new(clazz, decorated_method, *params, &blk)
|
27
|
+
decorator = decorator_class.new(clazz, undecorated_method, decorated_method, *params, &blk)
|
22
28
|
@_ignore_additions = false
|
23
|
-
|
24
|
-
clazz.send(:define_method, method_name) { |*args, &block| decorator.call(self, *args, &block) }
|
25
|
-
clazz.send(Decors::Utils.method_visibility(decorated_method), method_name)
|
29
|
+
clazz.send(Decors::Utils.method_visibility(undecorated_method), method_name)
|
26
30
|
end
|
27
31
|
end
|
28
32
|
|
data/spec/decors_spec.rb
CHANGED
@@ -12,10 +12,37 @@ describe Decors do
|
|
12
12
|
}
|
13
13
|
}
|
14
14
|
|
15
|
+
context 'Check decorated_method & undecorated_method' do
|
16
|
+
let(:instance) { TestClass.new }
|
17
|
+
|
18
|
+
before {
|
19
|
+
stub_class(:SimpleDecorator, inherits: [::Decors::DecoratorBase])
|
20
|
+
call_method = call_proc
|
21
|
+
SimpleDecorator.send(:define_method, :call){ |*| instance_eval(&call_method) }
|
22
|
+
TestClass.class_eval {
|
23
|
+
define_decorator :SimpleDecorator, SimpleDecorator
|
24
|
+
SimpleDecorator()
|
25
|
+
def test; 42 end
|
26
|
+
}
|
27
|
+
}
|
28
|
+
|
29
|
+
context 'Check decorated_method value' do
|
30
|
+
let(:call_proc) { proc { decorated_method } }
|
31
|
+
it { expect(instance.test).to eq TestClass.instance_method(:test) }
|
32
|
+
end
|
33
|
+
|
34
|
+
context 'Check undecorated_method value' do
|
35
|
+
let(:call_proc) { proc { undecorated_method } }
|
36
|
+
it { expect(instance.test.bind(instance).call).to eq 42 }
|
37
|
+
it { expect(instance.test.bind(instance).call).not_to eq TestClass.instance_method(:test) }
|
38
|
+
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
15
42
|
context 'when simple case decorator' do
|
16
43
|
before {
|
17
44
|
stub_class(:SimpleDecorator, inherits: [::Decors::DecoratorBase]) {
|
18
|
-
def initialize(decorated_class, decorated_method, *deco_args, **deco_kwargs, &deco_block)
|
45
|
+
def initialize(decorated_class, undecorated_method, decorated_method, *deco_args, **deco_kwargs, &deco_block)
|
19
46
|
super
|
20
47
|
Spy.passed_args(*deco_args, **deco_kwargs, &deco_block)
|
21
48
|
end
|
@@ -30,7 +57,7 @@ describe Decors do
|
|
30
57
|
}
|
31
58
|
|
32
59
|
context 'It receive all the parameters at initialization' do
|
33
|
-
it { expect(SimpleDecorator).to receive(:new).with(TestClass, anything, 1, 2, a: 3).and_call_original }
|
60
|
+
it { expect(SimpleDecorator).to receive(:new).with(TestClass, anything, anything, 1, 2, a: 3).and_call_original }
|
34
61
|
it { expect(Spy).to receive(:arguments).with(args: [1, 2], kwargs: { a: 3 }, evald_block: 'ok') }
|
35
62
|
|
36
63
|
after {
|
@@ -88,7 +115,7 @@ describe Decors do
|
|
88
115
|
context 'when decorator is defining a method during initialization' do
|
89
116
|
before {
|
90
117
|
stub_class(:StrangeDecorator, inherits: [::Decors::DecoratorBase]) {
|
91
|
-
def initialize(decorated_class, decorated_method, *deco_args, **deco_kwargs, &deco_block)
|
118
|
+
def initialize(decorated_class, undecorated_method, decorated_method, *deco_args, **deco_kwargs, &deco_block)
|
92
119
|
super
|
93
120
|
decorated_class.send(:define_method, :foo) { 42 }
|
94
121
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: decors
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Vivien Meyet
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-08-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: pry
|