method_decorator 1.1.1 → 2.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 +33 -11
- data/Rakefile +6 -5
- data/lib/method_decorator/decoration.rb +14 -0
- data/lib/method_decorator/repository.rb +27 -0
- data/lib/method_decorator/version.rb +1 -1
- data/lib/method_decorator.rb +19 -27
- data/method_decorator.gemspec +2 -2
- data/spec/lib/method_decorator_spec.rb +3 -4
- data/spec/support/some_class.rb +10 -19
- metadata +6 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 607f30fb2f44469cac0ff14b5b6dd1bfa80164ef
|
4
|
+
data.tar.gz: 09ead13f5c6c41d17b7c9cc129d7ee9a1d324ccc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8a2ce94d7ad57315caaa659efa8c621168943db20b656115469254b10850c4dc73bc038eea00b5394ce499df6d5a95618d757d47e134482a753311e8c53ed120
|
7
|
+
data.tar.gz: 973c2a671be37ebcc7bd3924d3e2d581eeb9cc6740dc84fcdaeff99b800e806691635cc0c45f2c0815668cbd6023bfe71907915e1b001b4bccbc4a609f652d83
|
data/README.rdoc
CHANGED
@@ -1,25 +1,20 @@
|
|
1
1
|
= Method Decorator
|
2
2
|
|
3
|
-
Provides a way to dynamically
|
3
|
+
Provides a way to dynamically overwrite methods without losing original behavior.
|
4
4
|
|
5
5
|
== Example
|
6
6
|
|
7
|
-
For a given class (with unnecessary complex logic, just to make a point):
|
8
|
-
|
9
7
|
class SomeClass
|
10
8
|
|
11
9
|
def some_method(some_arg)
|
12
10
|
puts some_arg
|
13
11
|
end
|
14
12
|
|
15
|
-
|
16
|
-
|
17
|
-
method_to_override = :some_method
|
18
|
-
decorate_method method_to_override do |*args|
|
19
|
-
puts :decorated_some_method
|
20
|
-
call_original_method method_to_override, *args
|
21
|
-
end
|
13
|
+
end
|
22
14
|
|
15
|
+
MethodDecorator.decorate SomeClass, :some_method do |*args|
|
16
|
+
puts :decorated_some_method
|
17
|
+
MethodDecorator.call_original self, :some_method, *args
|
23
18
|
end
|
24
19
|
|
25
20
|
This call:
|
@@ -29,4 +24,31 @@ This call:
|
|
29
24
|
Produces this output:
|
30
25
|
|
31
26
|
decorated_some_method
|
32
|
-
some arg
|
27
|
+
some arg
|
28
|
+
|
29
|
+
== Singleton Classes Support
|
30
|
+
|
31
|
+
It works too:
|
32
|
+
|
33
|
+
class SomeClass
|
34
|
+
|
35
|
+
class << self
|
36
|
+
|
37
|
+
def some_method(some_arg)
|
38
|
+
puts some_arg
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
MethodDecorator.decorate SomeClass.singleton_class, :some_method do |*args|
|
46
|
+
puts :decorated_some_method
|
47
|
+
MethodDecorator.call_original self, :some_method, *args
|
48
|
+
end
|
49
|
+
|
50
|
+
SomeClass.some_method 'some arg'
|
51
|
+
|
52
|
+
===
|
53
|
+
|
54
|
+
{<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/Rakefile
CHANGED
@@ -16,11 +16,12 @@ Rake::TestTask.new(:test) do |t|
|
|
16
16
|
t.test_files = FileList['test/**/test_*.rb']
|
17
17
|
end
|
18
18
|
|
19
|
-
RDoc::Task.new do |
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
19
|
+
RDoc::Task.new do |rdoc|
|
20
|
+
rdoc.rdoc_dir = 'rdoc'
|
21
|
+
rdoc.title = 'MethodDecorator'
|
22
|
+
rdoc.options << '--line-numbers'
|
23
|
+
rdoc.rdoc_files.include('README.rdoc')
|
24
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
24
25
|
end
|
25
26
|
|
26
27
|
require 'rspec/core/rake_task'
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module MethodDecorator
|
2
|
+
class Decoration
|
3
|
+
|
4
|
+
attr_accessor :target_class, :target_name, :target_method, :decoration
|
5
|
+
|
6
|
+
def initialize(target_class, target_method_name, target_method)
|
7
|
+
self.target_class = target_class
|
8
|
+
self.target_name = target_method_name
|
9
|
+
self.target_method = target_method
|
10
|
+
self.decoration = block_given? ? Proc.new : nil
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'method_decorator/decoration'
|
2
|
+
|
3
|
+
module MethodDecorator
|
4
|
+
class Repository
|
5
|
+
class << self
|
6
|
+
|
7
|
+
attr_writer :decorations
|
8
|
+
|
9
|
+
def add_decoration(target_class, target_name, &decoration)
|
10
|
+
exists = original_method target_class, target_name
|
11
|
+
target_method = target_class.instance_method target_name
|
12
|
+
decoration = Decoration.new target_class, target_name, target_method, &decoration
|
13
|
+
self.decorations.push(decoration) unless exists
|
14
|
+
not exists
|
15
|
+
end
|
16
|
+
|
17
|
+
def original_method(target_class, target_name)
|
18
|
+
condition = Proc.new { |d| d.target_class.eql?(target_class) and d.target_name.eql?(target_name) }
|
19
|
+
decoration = self.decorations.select(&condition).first
|
20
|
+
decoration ? decoration.target_method : nil
|
21
|
+
end
|
22
|
+
|
23
|
+
def decorations; @decorations ||= [] end
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/lib/method_decorator.rb
CHANGED
@@ -1,37 +1,29 @@
|
|
1
|
-
|
1
|
+
require 'method_decorator/repository'
|
2
2
|
|
3
|
-
|
3
|
+
module MethodDecorator
|
4
|
+
class << self
|
4
5
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
original_method_name = original_method_name_for target
|
11
|
-
complex_method_name = decorated_method_name_for target
|
12
|
-
|
13
|
-
alias_method original_method_name, target
|
14
|
-
define_method complex_method_name, &block
|
15
|
-
alias_method target, complex_method_name
|
16
|
-
end
|
17
|
-
|
18
|
-
def original_method_name_for(target)
|
19
|
-
"original_#{target}"
|
20
|
-
end
|
21
|
-
|
22
|
-
def decorated_method_name_for(target)
|
23
|
-
"decorated_#{target}"
|
24
|
-
end
|
6
|
+
def decorate(target_class, target_name, &decoration)
|
7
|
+
added = Repository.add_decoration target_class, target_name, &decoration
|
8
|
+
target_class.send :define_method, target_name, &decoration if added
|
9
|
+
added
|
10
|
+
end
|
25
11
|
|
12
|
+
def call_original(target_context, target_name, *original_args, &original_block)
|
13
|
+
target_class = target_class target_context
|
14
|
+
target_method = Repository.original_method target_class, target_name
|
15
|
+
target_method.bind(target_context).call *original_args, &original_block
|
26
16
|
end
|
27
17
|
|
28
|
-
|
18
|
+
def target_class(target_context)
|
19
|
+
target_type(target_context).eql?(:singleton) ?
|
20
|
+
target_context.singleton_class :
|
21
|
+
target_context.class
|
22
|
+
end
|
29
23
|
|
30
|
-
def
|
31
|
-
|
32
|
-
send original_method_name, *args, &block
|
24
|
+
def target_type(target_context)
|
25
|
+
target_context.class.eql?(Class) ? :singleton : :class
|
33
26
|
end
|
34
27
|
|
35
28
|
end
|
36
|
-
|
37
29
|
end
|
data/method_decorator.gemspec
CHANGED
@@ -8,8 +8,8 @@ Gem::Specification.new do |s|
|
|
8
8
|
s.authors = ['r4z3c']
|
9
9
|
s.email = ['r4z3c.43@gmail.com']
|
10
10
|
s.homepage = 'https://github.com/r4z3c/method_decorator.git'
|
11
|
-
s.summary = '
|
12
|
-
s.description = 'Provides a way to dynamically
|
11
|
+
s.summary = 'Overwrite methods preserving the original behavior'
|
12
|
+
s.description = 'Provides a way to dynamically overwrite methods without losing original behavior'
|
13
13
|
s.licenses = %w(MIT)
|
14
14
|
|
15
15
|
s.files = `git ls-files`.split("\n")
|
@@ -12,15 +12,14 @@ describe MethodDecorator do
|
|
12
12
|
expect(target).to receive(:puts).with(some_arg).ordered
|
13
13
|
end
|
14
14
|
|
15
|
-
context '' do
|
15
|
+
context 'when singleton method context' do
|
16
16
|
let(:target) { SomeClass }
|
17
|
-
|
18
17
|
it { subject }
|
19
18
|
end
|
20
19
|
|
21
|
-
context '' do
|
20
|
+
context 'when instance method context' do
|
22
21
|
let(:target) { SomeClass.new }
|
23
|
-
|
22
|
+
let(:methods) { target.class.send :generated_methods }
|
24
23
|
it { subject }
|
25
24
|
end
|
26
25
|
|
data/spec/support/some_class.rb
CHANGED
@@ -1,31 +1,22 @@
|
|
1
1
|
class SomeClass
|
2
2
|
|
3
|
-
def some_method(some_arg)
|
4
|
-
puts some_arg
|
5
|
-
end
|
6
|
-
|
7
|
-
include MethodDecorator
|
8
|
-
|
9
|
-
method_to_override = :some_method
|
10
|
-
decorate_method method_to_override do |*args|
|
11
|
-
puts :decorated_some_method
|
12
|
-
call_original_method method_to_override, *args
|
13
|
-
end
|
14
|
-
|
15
3
|
class << self
|
16
4
|
|
17
5
|
def some_method(some_arg)
|
18
6
|
puts some_arg
|
19
7
|
end
|
20
8
|
|
21
|
-
|
22
|
-
|
23
|
-
method_to_override = :some_method
|
24
|
-
decorate_method method_to_override do |*args|
|
25
|
-
puts :decorated_some_method
|
26
|
-
call_original_method method_to_override, *args
|
27
|
-
end
|
9
|
+
end
|
28
10
|
|
11
|
+
def some_method(some_arg)
|
12
|
+
puts some_arg
|
29
13
|
end
|
30
14
|
|
15
|
+
end
|
16
|
+
|
17
|
+
[SomeClass, SomeClass.singleton_class].each do |target_class|
|
18
|
+
MethodDecorator.decorate target_class, :some_method do |*args, &block|
|
19
|
+
puts :decorated_some_method
|
20
|
+
MethodDecorator.call_original self, :some_method, *args, &block
|
21
|
+
end
|
31
22
|
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: 2.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: 2016-
|
11
|
+
date: 2016-11-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -66,7 +66,7 @@ dependencies:
|
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
|
-
description: Provides a way to dynamically
|
69
|
+
description: Provides a way to dynamically overwrite methods without losing original
|
70
70
|
behavior
|
71
71
|
email:
|
72
72
|
- r4z3c.43@gmail.com
|
@@ -81,6 +81,8 @@ files:
|
|
81
81
|
- README.rdoc
|
82
82
|
- Rakefile
|
83
83
|
- lib/method_decorator.rb
|
84
|
+
- lib/method_decorator/decoration.rb
|
85
|
+
- lib/method_decorator/repository.rb
|
84
86
|
- lib/method_decorator/version.rb
|
85
87
|
- method_decorator.gemspec
|
86
88
|
- spec/lib/method_decorator_spec.rb
|
@@ -109,7 +111,7 @@ rubyforge_project:
|
|
109
111
|
rubygems_version: 2.5.1
|
110
112
|
signing_key:
|
111
113
|
specification_version: 4
|
112
|
-
summary:
|
114
|
+
summary: Overwrite methods preserving the original behavior
|
113
115
|
test_files:
|
114
116
|
- spec/lib/method_decorator_spec.rb
|
115
117
|
- spec/spec_helper.rb
|