active_support_alias_class_method 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +52 -0
- data/Rakefile +11 -0
- data/active_support_alias_class_method.gemspec +22 -0
- data/lib/active_support_alias_class_method.rb +5 -0
- data/lib/active_support_alias_class_method/core_ext/module.rb +1 -0
- data/lib/active_support_alias_class_method/core_ext/module/aliasing.rb +33 -0
- data/lib/active_support_alias_class_method/version.rb +3 -0
- data/test/abstract_unit.rb +6 -0
- data/test/core_ext/module_test.rb +198 -0
- metadata +108 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Kenta Murata
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
# ActiveSupportAliasClassMethod
|
2
|
+
|
3
|
+
A supplementary library of activesupport to provide ```alias_class_method``` and ```alias_class_method_chain```.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'active_support_alias_class_method'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install active_support_alias_class_method
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
require 'active_support_alias_class_method/core_ext/module'
|
23
|
+
|
24
|
+
class Foo
|
25
|
+
def self.foo
|
26
|
+
:foo
|
27
|
+
end
|
28
|
+
|
29
|
+
alias_class_method :bar, :foo
|
30
|
+
end
|
31
|
+
|
32
|
+
Foo.foo #=> :foo
|
33
|
+
Foo.bar #=> :foo
|
34
|
+
|
35
|
+
class Foo
|
36
|
+
def self.foo_with_baz
|
37
|
+
[:baz, foo_without_baz]
|
38
|
+
end
|
39
|
+
|
40
|
+
alias_class_method_chain :foo, :baz
|
41
|
+
end
|
42
|
+
|
43
|
+
Foo.foo #=> [:baz, :foo]
|
44
|
+
```
|
45
|
+
|
46
|
+
## Contributing
|
47
|
+
|
48
|
+
1. Fork it
|
49
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
50
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
51
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
52
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/active_support_alias_class_method/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Kenta Murata"]
|
6
|
+
gem.email = ["mrkn@cookpad.com"]
|
7
|
+
gem.description = %q{A supplementary library of activesupport to provide alias_class_method and alias_class_method_chain}
|
8
|
+
gem.summary = %q{Providing alias_class_method and alias_class_method_chain}
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "active_support_alias_class_method"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = ActiveSupportAliasClassMethod::VERSION
|
17
|
+
|
18
|
+
gem.add_dependency('activesupport', '~> 3.0.0')
|
19
|
+
|
20
|
+
gem.add_development_dependency('rake')
|
21
|
+
gem.add_development_dependency('test-unit')
|
22
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'active_support_alias_class_method/core_ext/module/aliasing'
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'active_support/core_ext/kernel/singleton_class'
|
2
|
+
|
3
|
+
class Module
|
4
|
+
def alias_class_method(new, original)
|
5
|
+
singleton_class.class_eval do
|
6
|
+
alias_method new, original
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
def alias_class_method_chain(target, feature)
|
11
|
+
# Strip out punctuation on predicates or bang methods since
|
12
|
+
# e.g. target?_without_feature is not a valid method name.
|
13
|
+
aliased_target, punctuation = target.to_s.sub(/([?!=])$/, ''), $1
|
14
|
+
yield(aliased_target, punctuation) if block_given?
|
15
|
+
|
16
|
+
with_method, without_method = "#{aliased_target}_with_#{feature}#{punctuation}", "#{aliased_target}_without_#{feature}#{punctuation}"
|
17
|
+
|
18
|
+
self.singleton_class.class_eval do
|
19
|
+
alias_method without_method, target
|
20
|
+
alias_method target, with_method
|
21
|
+
|
22
|
+
case
|
23
|
+
when public_method_defined?(without_method)
|
24
|
+
public target
|
25
|
+
when protected_method_defined?(without_method)
|
26
|
+
protected target
|
27
|
+
when private_method_defined?(without_method)
|
28
|
+
private target
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
@@ -0,0 +1,198 @@
|
|
1
|
+
require 'abstract_unit'
|
2
|
+
require 'active_support_alias_class_method/core_ext/module'
|
3
|
+
|
4
|
+
module BarClassMethodAliaser
|
5
|
+
def self.included(foo_class)
|
6
|
+
foo_class.class_eval do
|
7
|
+
extend BarClassMethods
|
8
|
+
alias_class_method_chain :bar, :baz
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
module BarClassMethods
|
14
|
+
def bar_with_baz
|
15
|
+
bar_without_baz << '_with_baz'
|
16
|
+
end
|
17
|
+
|
18
|
+
def quux_with_baz!
|
19
|
+
quux_without_baz! << '_with_baz'
|
20
|
+
end
|
21
|
+
|
22
|
+
def quux_with_baz?
|
23
|
+
false
|
24
|
+
end
|
25
|
+
|
26
|
+
def quux_with_baz=(v)
|
27
|
+
send(:quux_without_baz=, v) << '_with_baz'
|
28
|
+
end
|
29
|
+
|
30
|
+
def duck_with_orange
|
31
|
+
duck_without_orange << '_with_orange'
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
class ClassMethodAliasingTest < Test::Unit::TestCase
|
36
|
+
def setup
|
37
|
+
Object.const_set :FooClassWithBarClassMethod, Class.new { def self.bar() 'bar' end }
|
38
|
+
@class = Class.new(FooClassWithBarClassMethod)
|
39
|
+
end
|
40
|
+
|
41
|
+
def teardown
|
42
|
+
Object.instance_eval { remove_const :FooClassWithBarClassMethod }
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_alias_class_method
|
46
|
+
assert_respond_to @class, :bar
|
47
|
+
@class.class_eval do
|
48
|
+
alias_class_method :foo, :bar
|
49
|
+
end
|
50
|
+
assert_respond_to @class, :foo
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_alias_class_method_chain
|
54
|
+
assert @class.respond_to?(:bar)
|
55
|
+
feature_aliases = [:bar_with_baz, :bar_without_baz]
|
56
|
+
|
57
|
+
feature_aliases.each do |method|
|
58
|
+
assert !@class.respond_to?(method)
|
59
|
+
end
|
60
|
+
|
61
|
+
assert_equal 'bar', @class.bar
|
62
|
+
|
63
|
+
FooClassWithBarClassMethod.class_eval { include BarClassMethodAliaser }
|
64
|
+
|
65
|
+
feature_aliases.each do |method|
|
66
|
+
assert_respond_to @class, method
|
67
|
+
end
|
68
|
+
|
69
|
+
assert_equal 'bar_with_baz', @class.bar
|
70
|
+
assert_equal 'bar', @class.bar_without_baz
|
71
|
+
end
|
72
|
+
|
73
|
+
def test_alias_class_method_chain_with_punctuation_method
|
74
|
+
FooClassWithBarClassMethod.class_eval do
|
75
|
+
def self.quux!; 'quux' end
|
76
|
+
end
|
77
|
+
|
78
|
+
assert !@class.respond_to?(:quux_with_baz!)
|
79
|
+
FooClassWithBarClassMethod.class_eval do
|
80
|
+
include BarClassMethodAliaser
|
81
|
+
alias_class_method_chain :quux!, :baz
|
82
|
+
end
|
83
|
+
assert_respond_to @class, :quux_with_baz!
|
84
|
+
|
85
|
+
assert_equal 'quux_with_baz', @class.quux!
|
86
|
+
assert_equal 'quux', @class.quux_without_baz!
|
87
|
+
end
|
88
|
+
|
89
|
+
def test_alias_class_method_chain_with_same_names_between_predicates_and_bang_methods
|
90
|
+
FooClassWithBarClassMethod.class_eval do
|
91
|
+
def self.quux!; 'quux!' end
|
92
|
+
def self.quux?; true end
|
93
|
+
def self.quux=(v); 'quux=' end
|
94
|
+
end
|
95
|
+
|
96
|
+
assert !@class.respond_to?(:quux_with_baz!)
|
97
|
+
assert !@class.respond_to?(:quux_with_baz?)
|
98
|
+
assert !@class.respond_to?(:quux_with_baz=)
|
99
|
+
|
100
|
+
FooClassWithBarClassMethod.class_eval { include BarClassMethodAliaser }
|
101
|
+
assert_respond_to @class, :quux_with_baz!
|
102
|
+
assert_respond_to @class, :quux_with_baz?
|
103
|
+
assert_respond_to @class, :quux_with_baz=
|
104
|
+
|
105
|
+
|
106
|
+
FooClassWithBarClassMethod.alias_class_method_chain :quux!, :baz
|
107
|
+
assert_equal 'quux!_with_baz', @class.quux!
|
108
|
+
assert_equal 'quux!', @class.quux_without_baz!
|
109
|
+
|
110
|
+
FooClassWithBarClassMethod.alias_class_method_chain :quux?, :baz
|
111
|
+
assert_equal false, @class.quux?
|
112
|
+
assert_equal true, @class.quux_without_baz?
|
113
|
+
|
114
|
+
FooClassWithBarClassMethod.alias_class_method_chain :quux=, :baz
|
115
|
+
assert_equal 'quux=_with_baz', @class.send(:quux=, 1234)
|
116
|
+
assert_equal 'quux=', @class.send(:quux_without_baz=, 1234)
|
117
|
+
end
|
118
|
+
|
119
|
+
def test_alias_class_method_chain_with_feature_punctuation
|
120
|
+
FooClassWithBarClassMethod.class_eval do
|
121
|
+
def self.quux; 'quux' end
|
122
|
+
def self.quux?; 'quux?' end
|
123
|
+
include BarClassMethodAliaser
|
124
|
+
alias_class_method_chain :quux, :baz!
|
125
|
+
end
|
126
|
+
|
127
|
+
assert_nothing_raised do
|
128
|
+
assert_equal 'quux_with_baz', @class.quux_with_baz!
|
129
|
+
end
|
130
|
+
|
131
|
+
assert_raise(NameError) do
|
132
|
+
FooClassWithBarClassMethod.alias_class_method_chain :quux?, :baz!
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
def test_alias_class_method_chain_yields_target_and_punctuation
|
137
|
+
args = nil
|
138
|
+
|
139
|
+
FooClassWithBarClassMethod.class_eval do
|
140
|
+
def self.quux?; end
|
141
|
+
extend BarClassMethods
|
142
|
+
|
143
|
+
FooClassWithBarClassMethod.alias_class_method_chain :quux?, :baz do |target, punctuation|
|
144
|
+
args = [target, punctuation]
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
assert_not_nil args
|
149
|
+
assert_equal 'quux', args[0]
|
150
|
+
assert_equal '?', args[1]
|
151
|
+
end
|
152
|
+
|
153
|
+
def test_alias_method_chain_preserves_private_method_status
|
154
|
+
FooClassWithBarClassMethod.class_eval do
|
155
|
+
def self.duck; 'duck' end
|
156
|
+
include BarClassMethodAliaser
|
157
|
+
private_class_method :duck
|
158
|
+
alias_class_method_chain :duck, :orange
|
159
|
+
end
|
160
|
+
|
161
|
+
assert_raise NoMethodError do
|
162
|
+
@class.duck
|
163
|
+
end
|
164
|
+
|
165
|
+
assert_equal 'duck_with_orange', @class.instance_eval { duck }
|
166
|
+
assert FooClassWithBarClassMethod.singleton_class.private_method_defined?(:duck)
|
167
|
+
end
|
168
|
+
|
169
|
+
def test_alias_method_chain_preserves_protected_method_status
|
170
|
+
FooClassWithBarClassMethod.class_eval do
|
171
|
+
def self.duck; 'duck' end
|
172
|
+
include BarClassMethodAliaser
|
173
|
+
class << self
|
174
|
+
protected :duck
|
175
|
+
end
|
176
|
+
alias_class_method_chain :duck, :orange
|
177
|
+
end
|
178
|
+
|
179
|
+
assert_raise NoMethodError do
|
180
|
+
@class.duck
|
181
|
+
end
|
182
|
+
|
183
|
+
assert_equal 'duck_with_orange', @class.instance_eval { duck }
|
184
|
+
assert FooClassWithBarClassMethod.singleton_class.protected_method_defined?(:duck)
|
185
|
+
end
|
186
|
+
|
187
|
+
def test_alias_method_chain_preserves_public_method_status
|
188
|
+
FooClassWithBarClassMethod.class_eval do
|
189
|
+
def self.duck; 'duck' end
|
190
|
+
include BarClassMethodAliaser
|
191
|
+
public_class_method :duck
|
192
|
+
alias_class_method_chain :duck, :orange
|
193
|
+
end
|
194
|
+
|
195
|
+
assert_equal 'duck_with_orange', @class.duck
|
196
|
+
assert FooClassWithBarClassMethod.singleton_class.public_method_defined?(:duck)
|
197
|
+
end
|
198
|
+
end
|
metadata
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: active_support_alias_class_method
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Kenta Murata
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-06-04 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activesupport
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.0.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 3.0.0
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: test-unit
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
description: A supplementary library of activesupport to provide alias_class_method
|
63
|
+
and alias_class_method_chain
|
64
|
+
email:
|
65
|
+
- mrkn@cookpad.com
|
66
|
+
executables: []
|
67
|
+
extensions: []
|
68
|
+
extra_rdoc_files: []
|
69
|
+
files:
|
70
|
+
- .gitignore
|
71
|
+
- Gemfile
|
72
|
+
- LICENSE
|
73
|
+
- README.md
|
74
|
+
- Rakefile
|
75
|
+
- active_support_alias_class_method.gemspec
|
76
|
+
- lib/active_support_alias_class_method.rb
|
77
|
+
- lib/active_support_alias_class_method/core_ext/module.rb
|
78
|
+
- lib/active_support_alias_class_method/core_ext/module/aliasing.rb
|
79
|
+
- lib/active_support_alias_class_method/version.rb
|
80
|
+
- test/abstract_unit.rb
|
81
|
+
- test/core_ext/module_test.rb
|
82
|
+
homepage: ''
|
83
|
+
licenses: []
|
84
|
+
post_install_message:
|
85
|
+
rdoc_options: []
|
86
|
+
require_paths:
|
87
|
+
- lib
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
95
|
+
none: false
|
96
|
+
requirements:
|
97
|
+
- - ! '>='
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
requirements: []
|
101
|
+
rubyforge_project:
|
102
|
+
rubygems_version: 1.8.23
|
103
|
+
signing_key:
|
104
|
+
specification_version: 3
|
105
|
+
summary: Providing alias_class_method and alias_class_method_chain
|
106
|
+
test_files:
|
107
|
+
- test/abstract_unit.rb
|
108
|
+
- test/core_ext/module_test.rb
|