override-parens 0.0.1
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.
- data/.gitignore +35 -0
- data/LICENSE +22 -0
- data/README.md +57 -0
- data/lib/override_parens.rb +30 -0
- data/override-parens.gemspec +18 -0
- data/test.rb +125 -0
- metadata +53 -0
data/.gitignore
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
*.gem
|
|
2
|
+
*.rbc
|
|
3
|
+
/.config
|
|
4
|
+
/coverage/
|
|
5
|
+
/InstalledFiles
|
|
6
|
+
/pkg/
|
|
7
|
+
/spec/reports/
|
|
8
|
+
/test/tmp/
|
|
9
|
+
/test/version_tmp/
|
|
10
|
+
/tmp/
|
|
11
|
+
|
|
12
|
+
## Specific to RubyMotion:
|
|
13
|
+
.dat*
|
|
14
|
+
.repl_history
|
|
15
|
+
build/
|
|
16
|
+
|
|
17
|
+
## Documentation cache and generated files:
|
|
18
|
+
/.yardoc/
|
|
19
|
+
/_yardoc/
|
|
20
|
+
/doc/
|
|
21
|
+
/rdoc/
|
|
22
|
+
|
|
23
|
+
## Environment normalisation:
|
|
24
|
+
/.bundle/
|
|
25
|
+
/vendor/bundle
|
|
26
|
+
/lib/bundler/man/
|
|
27
|
+
|
|
28
|
+
# for a library or gem, you might want to ignore these files since the code is
|
|
29
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
30
|
+
# Gemfile.lock
|
|
31
|
+
# .ruby-version
|
|
32
|
+
# .ruby-gemset
|
|
33
|
+
|
|
34
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
|
35
|
+
.rvmrc
|
data/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2015 Ben Christel
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
data/README.md
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# override-parens
|
|
2
|
+
|
|
3
|
+
A few gems and Ruby libraries - notably the `JSON` library that comes with Ruby - give you shorthands like the following:
|
|
4
|
+
|
|
5
|
+
```ruby
|
|
6
|
+
JSON('{}') == JSON.parse('{}')
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
Ruby doesn't actually let you override the method call operator - what's really going on is that the JSON library defines a method `JSON` on Kernel, and Ruby tolerates methods that have the same name as a class or module.
|
|
10
|
+
|
|
11
|
+
This gem lets you hide that piece of trickery and pretend that it's possible to override parentheses in Ruby.
|
|
12
|
+
|
|
13
|
+
```ruby
|
|
14
|
+
require 'override-parens'
|
|
15
|
+
|
|
16
|
+
class MyClass
|
|
17
|
+
include OverrideParens
|
|
18
|
+
|
|
19
|
+
def self.parens(foo)
|
|
20
|
+
new(foo)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def initialize(foo)
|
|
24
|
+
@foo = foo
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
MyClass(1)
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Installation
|
|
32
|
+
|
|
33
|
+
Add this line to your application's Gemfile:
|
|
34
|
+
|
|
35
|
+
```ruby
|
|
36
|
+
gem 'override-parens'
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
And then execute:
|
|
40
|
+
|
|
41
|
+
$ bundle
|
|
42
|
+
|
|
43
|
+
Or install it yourself as:
|
|
44
|
+
|
|
45
|
+
$ gem install override-parens
|
|
46
|
+
|
|
47
|
+
## Dependencies
|
|
48
|
+
|
|
49
|
+
`override-parens` only works on **Ruby 1.9.3** and above.
|
|
50
|
+
|
|
51
|
+
## Contributing
|
|
52
|
+
|
|
53
|
+
1. Fork it ( https://github.com/benchristel/override-parens/fork )
|
|
54
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
|
55
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
|
56
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
|
57
|
+
5. Create a new Pull Request
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
module OverrideParens
|
|
2
|
+
def self.included(base)
|
|
3
|
+
namespace = base.name.split('::')[0...-1]
|
|
4
|
+
unqualified_name = base.name.split('::')[-1]
|
|
5
|
+
immediate_namespace = namespace.inject(Kernel) { |mod, const_name| mod.const_get const_name }
|
|
6
|
+
|
|
7
|
+
immediate_namespace.class_eval do
|
|
8
|
+
define_method(unqualified_name) do |*args, &block|
|
|
9
|
+
base.parens(*args, &block)
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
define_singleton_method(unqualified_name) do |*args, &block|
|
|
13
|
+
base.parens(*args, &block)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
private unqualified_name
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def base.parens(*args)
|
|
20
|
+
raise NotImplementedError.new "To use OverrideParens, you must implement #{self.name}.parens, e.g.:" +
|
|
21
|
+
%Q{
|
|
22
|
+
class #{self.name}
|
|
23
|
+
def self.parens(*args)
|
|
24
|
+
# ...
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
}
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
lib = File.expand_path('../lib', __FILE__)
|
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
3
|
+
|
|
4
|
+
Gem::Specification.new do |s|
|
|
5
|
+
version = "0.0.1"
|
|
6
|
+
|
|
7
|
+
s.name = "override-parens"
|
|
8
|
+
s.version = version
|
|
9
|
+
s.platform = Gem::Platform::RUBY
|
|
10
|
+
s.license = "MIT"
|
|
11
|
+
s.authors = ["Ben Christel"]
|
|
12
|
+
s.homepage = "http://github.com/benchristel/override-parens"
|
|
13
|
+
s.summary = "override-parens-#{version}"
|
|
14
|
+
s.description = "Add the method call operator (parentheses) to classes and modules, e.g. MyClass(foo)"
|
|
15
|
+
s.files = `git ls-files`.split("\n")
|
|
16
|
+
s.test_files = s.files.grep(/^test/)
|
|
17
|
+
s.require_paths = ["lib"]
|
|
18
|
+
end
|
data/test.rb
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
gem 'minitest'
|
|
2
|
+
require 'minitest/autorun'
|
|
3
|
+
require_relative 'lib/override_parens'
|
|
4
|
+
|
|
5
|
+
class OuterClass
|
|
6
|
+
include OverrideParens
|
|
7
|
+
|
|
8
|
+
def self.parens(arg)
|
|
9
|
+
"outer #{arg}"
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
class MiddleClass
|
|
13
|
+
include OverrideParens
|
|
14
|
+
|
|
15
|
+
def self.parens(arg)
|
|
16
|
+
"middle class #{arg}"
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
class InnerClass
|
|
20
|
+
include OverrideParens
|
|
21
|
+
|
|
22
|
+
def self.parens(arg)
|
|
23
|
+
"inner class #{arg}"
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
module NotImplemented
|
|
29
|
+
include OverrideParens
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
module OuterModule
|
|
34
|
+
include OverrideParens
|
|
35
|
+
|
|
36
|
+
def self.parens(arg)
|
|
37
|
+
"outer module #{arg}"
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
module InnerModule
|
|
41
|
+
include OverrideParens
|
|
42
|
+
|
|
43
|
+
def self.parens(arg)
|
|
44
|
+
"inner module #{arg}"
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
module ClassInModule
|
|
49
|
+
include OverrideParens
|
|
50
|
+
|
|
51
|
+
def self.parens(arg)
|
|
52
|
+
"class in module #{arg}"
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
class Shadowed
|
|
58
|
+
include OverrideParens
|
|
59
|
+
|
|
60
|
+
def self.parens(arg)
|
|
61
|
+
"hidden by inner class"
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
class TestParens < MiniTest::Test
|
|
66
|
+
def test_overriding_parens_on_a_class
|
|
67
|
+
assert_equal 'outer 1', OuterClass(1)
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def test_overriding_parens_on_a_module
|
|
71
|
+
assert_equal 'outer module 1', OuterModule(1)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def test_overriding_parens_on_an_inner_class
|
|
75
|
+
assert_equal 'middle class 1', OuterClass::MiddleClass(1)
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def test_overriding_parens_on_a_deeply_nested_class
|
|
79
|
+
assert_equal 'inner class 1', OuterClass::MiddleClass::InnerClass(1)
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def test_overriding_parens_on_a_nested_module
|
|
83
|
+
assert_equal 'inner module 1', OuterModule::InnerModule(1)
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def test_overriding_parens_on_a_class_in_a_module
|
|
87
|
+
assert_equal 'class in module 1', OuterModule::ClassInModule(1)
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
class Shadowed
|
|
91
|
+
include OverrideParens
|
|
92
|
+
|
|
93
|
+
def self.parens(arg)
|
|
94
|
+
"shadowed #{arg}"
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def test_namespace_local_classes_shadow_top_level_classes
|
|
99
|
+
assert_equal 'shadowed 1', Shadowed(1)
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def test_shadowed_classes_are_still_accessible_from_kernel
|
|
103
|
+
assert_equal "hidden by inner class", Kernel::Shadowed(1)
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def test_no_funny_business
|
|
107
|
+
result = begin
|
|
108
|
+
OuterClass.new.MiddleClass(1)
|
|
109
|
+
rescue NoMethodError
|
|
110
|
+
"raised"
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
assert_equal "raised", result
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def test_exception_when_parens_is_not_implemented
|
|
117
|
+
result = begin
|
|
118
|
+
OuterClass::NotImplemented(1)
|
|
119
|
+
rescue NotImplementedError => e
|
|
120
|
+
e.message
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
assert result.include? 'OuterClass::NotImplemented'
|
|
124
|
+
end
|
|
125
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: override-parens
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
prerelease:
|
|
6
|
+
platform: ruby
|
|
7
|
+
authors:
|
|
8
|
+
- Ben Christel
|
|
9
|
+
autorequire:
|
|
10
|
+
bindir: bin
|
|
11
|
+
cert_chain: []
|
|
12
|
+
date: 2015-04-19 00:00:00.000000000 Z
|
|
13
|
+
dependencies: []
|
|
14
|
+
description: Add the method call operator (parentheses) to classes and modules, e.g.
|
|
15
|
+
MyClass(foo)
|
|
16
|
+
email:
|
|
17
|
+
executables: []
|
|
18
|
+
extensions: []
|
|
19
|
+
extra_rdoc_files: []
|
|
20
|
+
files:
|
|
21
|
+
- .gitignore
|
|
22
|
+
- LICENSE
|
|
23
|
+
- README.md
|
|
24
|
+
- lib/override_parens.rb
|
|
25
|
+
- override-parens.gemspec
|
|
26
|
+
- test.rb
|
|
27
|
+
homepage: http://github.com/benchristel/override-parens
|
|
28
|
+
licenses:
|
|
29
|
+
- MIT
|
|
30
|
+
post_install_message:
|
|
31
|
+
rdoc_options: []
|
|
32
|
+
require_paths:
|
|
33
|
+
- lib
|
|
34
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
35
|
+
none: false
|
|
36
|
+
requirements:
|
|
37
|
+
- - ! '>='
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '0'
|
|
40
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
41
|
+
none: false
|
|
42
|
+
requirements:
|
|
43
|
+
- - ! '>='
|
|
44
|
+
- !ruby/object:Gem::Version
|
|
45
|
+
version: '0'
|
|
46
|
+
requirements: []
|
|
47
|
+
rubyforge_project:
|
|
48
|
+
rubygems_version: 1.8.23
|
|
49
|
+
signing_key:
|
|
50
|
+
specification_version: 3
|
|
51
|
+
summary: override-parens-0.0.1
|
|
52
|
+
test_files:
|
|
53
|
+
- test.rb
|