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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7759f5e7d47d9e741ae03ea06cb02acaff1f984e
4
- data.tar.gz: 2b41184cffff4ba470f4730fe68c3fe52e779518
3
+ metadata.gz: 2bb31ed84c4aa6d83763802dd3ffed2c76b3fdd8
4
+ data.tar.gz: 60f07758499467ca63547d13bcff6354a845d3a9
5
5
  SHA512:
6
- metadata.gz: 7abe88ecad1024a83fefd0629af2120689063b0335d0fcb8f5711a071213c5919177804ddd9d377f38faffc7e0bf0aaa7cd9772b42e4df15376326850328c219
7
- data.tar.gz: 66d71eea9cd047431f76e349f4a95e15cdf06d60bcaaaa7103831f4eb12539fe3622ead623755a9e68ba820b77c6c10fc09708ab1e09fbe7a131f1644f46c7ec
6
+ metadata.gz: 8a2651655043a8bc0742fe60db5385989d7e415ae7a2728a3247b61934d7069af5a8b4b02cebde2523228433b0f278aa5805ab273886b7636dae829f725deb06
7
+ data.tar.gz: ac7ee42f12a9945822fd48017e33977d7b1a364a2ab20b157d6293cbc0847f73254e4a54d0d2dee8f754c45b6e7147c03530dad9033c10ecd55fce356f6e8502
@@ -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)
@@ -3,5 +3,5 @@ require 'decors/decorator_definition'
3
3
  require 'decors/method_added'
4
4
 
5
5
  module Decors
6
- VERSION = '0.2.0'
6
+ VERSION = '0.3.0'
7
7
  end
@@ -1,9 +1,11 @@
1
1
  module Decors
2
2
  class DecoratorBase
3
- attr_reader :decorated_class, :decorated_method, :decorator_args, :decorator_kwargs, :decorator_block
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
- decorated_method.bind(instance).call(*args, &block)
20
+ undecorated_method.bind(instance).call(*args, &block)
19
21
  end
20
22
 
21
23
  def decorated_method_name
@@ -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
 
@@ -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.2.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-05-10 00:00:00.000000000 Z
11
+ date: 2017-08-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pry