method_decorator 1.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 +7 -0
- data/.gitignore +16 -0
- data/.ruby-version +1 -0
- data/Gemfile +3 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +41 -0
- data/Rakefile +32 -0
- data/lib/method_decorator/version.rb +3 -0
- data/lib/method_decorator.rb +28 -0
- data/method_decorator.gemspec +25 -0
- data/spec/lib/method_decorator_spec.rb +27 -0
- data/spec/spec_helper.rb +6 -0
- data/spec/support/some_class.rb +33 -0
- metadata +116 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a702124860b5185e5110d615d2907ebe60186dcb
|
4
|
+
data.tar.gz: 7233f91bbdb27264c7fbce0581277564f298e399
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 440e73547765205775340efa47577d570af2878d9ac2975c67e03a56cdeb91dacba4b92a67f127590aac02105a2d3007540be3351dd5ffd72564300ccf0b8cf2
|
7
|
+
data.tar.gz: fc2fe1761d13dd1a5e92ffba41f8da7fb04ce0e9e1d306fbf4ccd772033a40c8831bee09851caaf64c30d6e728e2f65f473b07f9f999ccc7a0ec534e4504fe8d
|
data/.gitignore
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby-2.3.0@method_decorator
|
data/Gemfile
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2015 YOURNAME
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
= Method Decorator
|
2
|
+
|
3
|
+
Provides a way to dynamically override methods without losing original behavior.
|
4
|
+
|
5
|
+
== Example
|
6
|
+
|
7
|
+
For a given class (with unnecessary complex logic, just to make a point):
|
8
|
+
|
9
|
+
class SomeClass
|
10
|
+
|
11
|
+
def some_method(some_arg)
|
12
|
+
puts some_arg
|
13
|
+
end
|
14
|
+
|
15
|
+
include MethodDecorator
|
16
|
+
|
17
|
+
method_to_override = :some_method
|
18
|
+
original_method_name = original_method_name_for(method_to_override)
|
19
|
+
decorate_method method_to_override do |some_arg|
|
20
|
+
puts :decorated_some_method
|
21
|
+
send original_method_name, some_arg
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
This call:
|
27
|
+
|
28
|
+
SomeClass.new.some_method 'some arg'
|
29
|
+
|
30
|
+
Produces this output:
|
31
|
+
|
32
|
+
decorated_some_method
|
33
|
+
some arg
|
34
|
+
|
35
|
+
== Next moves
|
36
|
+
|
37
|
+
- Find a better way to call the original method, for now it's too verbose
|
38
|
+
|
39
|
+
original_method_name = original_method_name_for(:some_method)
|
40
|
+
...
|
41
|
+
send original_method_name, some_arg
|
data/Rakefile
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# Rakefile for rake -*- ruby -*-
|
2
|
+
|
3
|
+
# Copyright 2003, 2004, 2005 by Jim Weirich (jim@weirichhouse.org)
|
4
|
+
# All rights reserved.
|
5
|
+
|
6
|
+
# This file may be distributed under an MIT style license. See
|
7
|
+
# MIT-LICENSE for details.
|
8
|
+
|
9
|
+
require 'bundler/gem_tasks'
|
10
|
+
require 'rake/testtask'
|
11
|
+
require 'rdoc/task'
|
12
|
+
|
13
|
+
Rake::TestTask.new(:test) do |t|
|
14
|
+
t.libs << "test"
|
15
|
+
t.verbose = true
|
16
|
+
t.test_files = FileList['test/**/test_*.rb']
|
17
|
+
end
|
18
|
+
|
19
|
+
RDoc::Task.new do |doc|
|
20
|
+
doc.main = 'README.rdoc'
|
21
|
+
doc.title = 'Rake -- Ruby Make'
|
22
|
+
doc.rdoc_files = FileList.new %w[lib MIT-LICENSE doc/**/*.rdoc *.rdoc]
|
23
|
+
doc.rdoc_dir = 'html'
|
24
|
+
end
|
25
|
+
|
26
|
+
require 'rspec/core/rake_task'
|
27
|
+
|
28
|
+
RSpec::Core::RakeTask.new(:spec) do |t|
|
29
|
+
t.rspec_opts = %w(--color)
|
30
|
+
end
|
31
|
+
|
32
|
+
task :default => :spec
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module MethodDecorator extend ActiveSupport::Concern
|
2
|
+
|
3
|
+
included do
|
4
|
+
|
5
|
+
class << self
|
6
|
+
|
7
|
+
def decorate_method(target, &block)
|
8
|
+
original_method_name = original_method_name_for target
|
9
|
+
complex_method_name = decorated_method_name_for target
|
10
|
+
|
11
|
+
alias_method original_method_name, target
|
12
|
+
define_method complex_method_name, &block
|
13
|
+
alias_method target, complex_method_name
|
14
|
+
end
|
15
|
+
|
16
|
+
def original_method_name_for(target)
|
17
|
+
"original_#{target}"
|
18
|
+
end
|
19
|
+
|
20
|
+
def decorated_method_name_for(target)
|
21
|
+
"decorated_#{target}"
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
$:.push File.expand_path('../lib', __FILE__)
|
2
|
+
|
3
|
+
require 'method_decorator/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = 'method_decorator'
|
7
|
+
s.version = MethodDecorator::VERSION
|
8
|
+
s.authors = ['r4z3c']
|
9
|
+
s.email = ['r4z3c.43@gmail.com']
|
10
|
+
s.homepage = 'https://github.com/r4z3c/method_decorator.git'
|
11
|
+
s.summary = 'Override methods preserving the original behavior'
|
12
|
+
s.description = 'Provides a way to dynamically override methods without losing original behavior'
|
13
|
+
s.licenses = %w(MIT)
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split("\n")
|
16
|
+
s.test_files = s.files.grep(%r{^(test|spec|features)/})
|
17
|
+
|
18
|
+
s.require_paths = %w(lib)
|
19
|
+
|
20
|
+
s.add_dependency 'bundler', '~>1'
|
21
|
+
s.add_dependency 'activesupport', '~>4'
|
22
|
+
|
23
|
+
s.add_development_dependency 'rspec', '~>3'
|
24
|
+
s.add_development_dependency 'simplecov', '~>0'
|
25
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'support/some_class'
|
3
|
+
|
4
|
+
describe MethodDecorator do
|
5
|
+
|
6
|
+
let(:some_arg) { :some_arg }
|
7
|
+
|
8
|
+
subject { target.some_method some_arg }
|
9
|
+
|
10
|
+
before do
|
11
|
+
expect(target).to receive(:puts).with(:decorated_some_method).ordered
|
12
|
+
expect(target).to receive(:puts).with(some_arg).ordered
|
13
|
+
end
|
14
|
+
|
15
|
+
context '' do
|
16
|
+
let(:target) { SomeClass }
|
17
|
+
|
18
|
+
it { subject }
|
19
|
+
end
|
20
|
+
|
21
|
+
context '' do
|
22
|
+
let(:target) { SomeClass.new }
|
23
|
+
|
24
|
+
it { subject }
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
class SomeClass
|
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
|
+
original_method_name = original_method_name_for(method_to_override)
|
11
|
+
decorate_method method_to_override do |some_arg|
|
12
|
+
puts :decorated_some_method
|
13
|
+
send original_method_name, some_arg
|
14
|
+
end
|
15
|
+
|
16
|
+
class << self
|
17
|
+
|
18
|
+
def some_method(some_arg)
|
19
|
+
puts some_arg
|
20
|
+
end
|
21
|
+
|
22
|
+
include MethodDecorator
|
23
|
+
|
24
|
+
method_to_override = :some_method
|
25
|
+
original_method_name = original_method_name_for(method_to_override)
|
26
|
+
decorate_method method_to_override do |some_arg|
|
27
|
+
puts :decorated_some_method
|
28
|
+
send original_method_name, some_arg
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
metadata
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: method_decorator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- r4z3c
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-06-02 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '4'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '4'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: simplecov
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: Provides a way to dynamically override methods without losing original
|
70
|
+
behavior
|
71
|
+
email:
|
72
|
+
- r4z3c.43@gmail.com
|
73
|
+
executables: []
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- ".gitignore"
|
78
|
+
- ".ruby-version"
|
79
|
+
- Gemfile
|
80
|
+
- MIT-LICENSE
|
81
|
+
- README.rdoc
|
82
|
+
- Rakefile
|
83
|
+
- lib/method_decorator.rb
|
84
|
+
- lib/method_decorator/version.rb
|
85
|
+
- method_decorator.gemspec
|
86
|
+
- spec/lib/method_decorator_spec.rb
|
87
|
+
- spec/spec_helper.rb
|
88
|
+
- spec/support/some_class.rb
|
89
|
+
homepage: https://github.com/r4z3c/method_decorator.git
|
90
|
+
licenses:
|
91
|
+
- MIT
|
92
|
+
metadata: {}
|
93
|
+
post_install_message:
|
94
|
+
rdoc_options: []
|
95
|
+
require_paths:
|
96
|
+
- lib
|
97
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
|
+
requirements:
|
104
|
+
- - ">="
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0'
|
107
|
+
requirements: []
|
108
|
+
rubyforge_project:
|
109
|
+
rubygems_version: 2.5.1
|
110
|
+
signing_key:
|
111
|
+
specification_version: 4
|
112
|
+
summary: Override methods preserving the original behavior
|
113
|
+
test_files:
|
114
|
+
- spec/lib/method_decorator_spec.rb
|
115
|
+
- spec/spec_helper.rb
|
116
|
+
- spec/support/some_class.rb
|