finalizable 0.1.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/.rspec +3 -0
- data/.rubocop.yml +33 -0
- data/.travis.yml +6 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +63 -0
- data/LICENSE.txt +21 -0
- data/README.md +75 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/finalizable.gemspec +38 -0
- data/lib/finalizable.rb +9 -0
- data/lib/finalizable/finalization.rb +47 -0
- data/lib/finalizable/version.rb +3 -0
- metadata +132 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: cdd7523fe757dc528ba201a7777c37d370d620b5460754a185295df64bb95965
|
4
|
+
data.tar.gz: 2e6d093cb023f73e7c5557bd5c8762de0226396d176c4261ebf308b8fca6dff7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b249ca916b58b19e8d9ac5dbf85262ea78b3b49b39f6fe7d8d4dd76c640643f588fc641d56a2a4d29cfd6cdbb9473d365595a7e9993e13025be470c66f0f2279
|
7
|
+
data.tar.gz: 79a62f09e55d32f0fa6211fdba5bdf1f9abbc34018b8db899a0ad803622fc0587832546a292ba07ffac4115ea1ba617812892b992e81f89ab919fe795338cef0
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
AllCops:
|
2
|
+
Include:
|
3
|
+
- '**/Rakefile'
|
4
|
+
Exclude:
|
5
|
+
- 'spec/**/*'
|
6
|
+
- 'vendor/bundle/**/*'
|
7
|
+
|
8
|
+
Lint/Debugger:
|
9
|
+
Enabled: false
|
10
|
+
|
11
|
+
Metrics/LineLength:
|
12
|
+
Max: 160
|
13
|
+
AllowURI: true
|
14
|
+
Exclude:
|
15
|
+
- config/routes.rb
|
16
|
+
|
17
|
+
Style/FrozenStringLiteralComment:
|
18
|
+
Enabled: false
|
19
|
+
|
20
|
+
Style/ParallelAssignment:
|
21
|
+
Enabled: false
|
22
|
+
|
23
|
+
Style/PercentLiteralDelimiters:
|
24
|
+
Enabled: false
|
25
|
+
|
26
|
+
Style/HashEachMethods:
|
27
|
+
Enabled: true
|
28
|
+
|
29
|
+
Style/HashTransformKeys:
|
30
|
+
Enabled: true
|
31
|
+
|
32
|
+
Style/HashTransformValues:
|
33
|
+
Enabled: true
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
finalizable (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
ast (2.4.0)
|
10
|
+
byebug (11.1.1)
|
11
|
+
coderay (1.1.2)
|
12
|
+
diff-lcs (1.3)
|
13
|
+
jaro_winkler (1.5.4)
|
14
|
+
method_source (0.9.2)
|
15
|
+
parallel (1.19.1)
|
16
|
+
parser (2.7.0.2)
|
17
|
+
ast (~> 2.4.0)
|
18
|
+
pry (0.12.2)
|
19
|
+
coderay (~> 1.1.0)
|
20
|
+
method_source (~> 0.9.0)
|
21
|
+
pry-byebug (3.8.0)
|
22
|
+
byebug (~> 11.0)
|
23
|
+
pry (~> 0.10)
|
24
|
+
rainbow (3.0.0)
|
25
|
+
rake (12.3.3)
|
26
|
+
rexml (3.2.4)
|
27
|
+
rspec (3.9.0)
|
28
|
+
rspec-core (~> 3.9.0)
|
29
|
+
rspec-expectations (~> 3.9.0)
|
30
|
+
rspec-mocks (~> 3.9.0)
|
31
|
+
rspec-core (3.9.1)
|
32
|
+
rspec-support (~> 3.9.1)
|
33
|
+
rspec-expectations (3.9.0)
|
34
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
35
|
+
rspec-support (~> 3.9.0)
|
36
|
+
rspec-mocks (3.9.1)
|
37
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
38
|
+
rspec-support (~> 3.9.0)
|
39
|
+
rspec-support (3.9.2)
|
40
|
+
rubocop (0.80.0)
|
41
|
+
jaro_winkler (~> 1.5.1)
|
42
|
+
parallel (~> 1.10)
|
43
|
+
parser (>= 2.7.0.1)
|
44
|
+
rainbow (>= 2.2.2, < 4.0)
|
45
|
+
rexml
|
46
|
+
ruby-progressbar (~> 1.7)
|
47
|
+
unicode-display_width (>= 1.4.0, < 1.7)
|
48
|
+
ruby-progressbar (1.10.1)
|
49
|
+
unicode-display_width (1.6.1)
|
50
|
+
|
51
|
+
PLATFORMS
|
52
|
+
ruby
|
53
|
+
|
54
|
+
DEPENDENCIES
|
55
|
+
bundler (~> 2.0)
|
56
|
+
finalizable!
|
57
|
+
pry-byebug (~> 3.8.0)
|
58
|
+
rake (~> 12.0)
|
59
|
+
rspec (~> 3.0)
|
60
|
+
rubocop (~> 0.80.0)
|
61
|
+
|
62
|
+
BUNDLED WITH
|
63
|
+
2.1.2
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 Shigeru Hagiwara
|
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
|
13
|
+
all 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
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
[](https://travis-ci.com/hgwr/finalizable)
|
2
|
+
[](https://badge.fury.io/rb/finalizable)
|
3
|
+
|
4
|
+
# Finalizable
|
5
|
+
|
6
|
+
This gem makes the specified method non-overridable in Ruby.
|
7
|
+
|
8
|
+
- I found [the awesome question in stack overflow](https://stackoverflow.com/questions/708642/how-to-make-a-base-class-method-non-overridable-in-ruby).
|
9
|
+
- In the answer section, [Original page](http://www.thesorensens.org/2006/10/06/final-methods-in-ruby-prevent-method-override/) is intrudoced.
|
10
|
+
- [Original page](http://www.thesorensens.org/2006/10/06/final-methods-in-ruby-prevent-method-override/) is not accessible now (2020-02-23).
|
11
|
+
- I refered [the page in WayBack Machine](https://web.archive.org/web/20080226085648/http://www.thesorensens.org/2006/10/06/final-methods-in-ruby-prevent-method-override/).
|
12
|
+
|
13
|
+
## Installation
|
14
|
+
|
15
|
+
Add this line to your application's Gemfile:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
gem 'finalizable'
|
19
|
+
```
|
20
|
+
|
21
|
+
And then execute:
|
22
|
+
|
23
|
+
$ bundle install
|
24
|
+
|
25
|
+
Or install it yourself as:
|
26
|
+
|
27
|
+
$ gem install finalizable
|
28
|
+
|
29
|
+
## Usage
|
30
|
+
|
31
|
+
see `lib/finalizable/example/`
|
32
|
+
|
33
|
+
Example of parent class which is using `final` and `somewhat_final`.
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
class ParentClass
|
37
|
+
include ::Finalizable
|
38
|
+
final :final_method_defined_in_parent_class
|
39
|
+
somewhat_final :somewhat_final_method_defined_in_parent_class
|
40
|
+
|
41
|
+
def final_method_defined_in_parent_class
|
42
|
+
:return_value_from_parent_class
|
43
|
+
end
|
44
|
+
|
45
|
+
def somewhat_final_method_defined_in_parent_class
|
46
|
+
:return_value_from_parent_class
|
47
|
+
end
|
48
|
+
|
49
|
+
def overridable_method_defined_in_parent_class
|
50
|
+
:return_value_from_parent_class
|
51
|
+
end
|
52
|
+
end
|
53
|
+
```
|
54
|
+
|
55
|
+
- If subclass overrides the method `final_method_defined_in_parent_class`, you will get `Finalizable::OverrideError` on loading time not runtime.
|
56
|
+
- If subclass overrides the method `somewhat_final_method_defined_in_parent_class`, you will get a `Kernel#.warn` message.
|
57
|
+
|
58
|
+
```
|
59
|
+
WARNING: Child class 'Finalizable::Example::AClassSomeMethodsAreNotAllowedOverriding' should not override parent class method 'Finalizable::Example::ParentClass.somewhat_final_method_defined_in_parent_class'.
|
60
|
+
```
|
61
|
+
|
62
|
+
## Development
|
63
|
+
|
64
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
65
|
+
|
66
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
67
|
+
|
68
|
+
## Contributing
|
69
|
+
|
70
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/finalizable.
|
71
|
+
|
72
|
+
|
73
|
+
## License
|
74
|
+
|
75
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'finalizable'
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require 'irb'
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/finalizable.gemspec
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
lib = File.expand_path('lib', __dir__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'finalizable/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'finalizable'
|
7
|
+
spec.version = Finalizable::VERSION
|
8
|
+
spec.authors = ['Shigeru Hagiwara']
|
9
|
+
spec.email = ['hgwrsgr@gmail.com']
|
10
|
+
|
11
|
+
spec.summary = %q{Preventing overriding specific methods.}
|
12
|
+
spec.description = %q{This gem makes the specified method non-overridable in Ruby.}
|
13
|
+
spec.homepage = 'https://rubygems.org/gems/finalizable'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
spec.required_ruby_version = Gem::Requirement.new('>= 2.4.0')
|
16
|
+
|
17
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
18
|
+
spec.metadata['source_code_uri'] = 'https://github.com/hgwr/finalizable/'
|
19
|
+
spec.metadata['changelog_uri'] = 'https://github.com/hgwr/finalizable/'
|
20
|
+
|
21
|
+
# Specify which files should be added to the gem when it is released.
|
22
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
23
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
24
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
25
|
+
end
|
26
|
+
spec.bindir = 'exe'
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ['lib']
|
29
|
+
|
30
|
+
spec.required_ruby_version = '>= 2.4'
|
31
|
+
|
32
|
+
spec.add_development_dependency 'bundler', '~> 2.0'
|
33
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
34
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
35
|
+
|
36
|
+
spec.add_development_dependency 'pry-byebug', '~> 3.8.0'
|
37
|
+
spec.add_development_dependency 'rubocop', '~> 0.80.0'
|
38
|
+
end
|
data/lib/finalizable.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
module Finalizable
|
2
|
+
# Finalizable Error class
|
3
|
+
class OverrideError < StandardError; end
|
4
|
+
|
5
|
+
# Class methods
|
6
|
+
module Finalization
|
7
|
+
@@final_methods = {}
|
8
|
+
@@somewhat_final_methods = {}
|
9
|
+
|
10
|
+
def final(*names)
|
11
|
+
@@final_methods[self] = names
|
12
|
+
end
|
13
|
+
|
14
|
+
def somewhat_final(*names)
|
15
|
+
@@somewhat_final_methods[self] = names
|
16
|
+
end
|
17
|
+
|
18
|
+
def method_added(method_name)
|
19
|
+
prevent_override?(method_name)
|
20
|
+
end
|
21
|
+
|
22
|
+
def prevent_override?(method_name)
|
23
|
+
raise_error_if_prohibited_methods_are_overridden(method_name)
|
24
|
+
warn_if_prohibited_methods_are_overridden(method_name)
|
25
|
+
end
|
26
|
+
|
27
|
+
def raise_error_if_prohibited_methods_are_overridden(method_name)
|
28
|
+
@@final_methods.each do |class_name, final_methods|
|
29
|
+
ancestors = self.ancestors.dup
|
30
|
+
ancestors.shift # remove myself from the list
|
31
|
+
if ancestors.include?(class_name) && final_methods.include?(method_name)
|
32
|
+
raise Finalizable::OverrideError, "Child class '#{self}' should not override parent class method '#{class_name}.#{method_name}'."
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def warn_if_prohibited_methods_are_overridden(method_name)
|
38
|
+
@@somewhat_final_methods.each do |class_name, final_methods|
|
39
|
+
ancestors = self.ancestors.dup
|
40
|
+
ancestors.shift # remove myself from the list
|
41
|
+
if ancestors.include?(class_name) && final_methods.include?(method_name)
|
42
|
+
warn "WARNING: Child class '#{self}' should not override parent class method '#{class_name}.#{method_name}'."
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
metadata
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: finalizable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Shigeru Hagiwara
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-02-23 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: '2.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
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.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry-byebug
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 3.8.0
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 3.8.0
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rubocop
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 0.80.0
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 0.80.0
|
83
|
+
description: This gem makes the specified method non-overridable in Ruby.
|
84
|
+
email:
|
85
|
+
- hgwrsgr@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".rspec"
|
92
|
+
- ".rubocop.yml"
|
93
|
+
- ".travis.yml"
|
94
|
+
- Gemfile
|
95
|
+
- Gemfile.lock
|
96
|
+
- LICENSE.txt
|
97
|
+
- README.md
|
98
|
+
- Rakefile
|
99
|
+
- bin/console
|
100
|
+
- bin/setup
|
101
|
+
- finalizable.gemspec
|
102
|
+
- lib/finalizable.rb
|
103
|
+
- lib/finalizable/finalization.rb
|
104
|
+
- lib/finalizable/version.rb
|
105
|
+
- log/.keep
|
106
|
+
homepage: https://rubygems.org/gems/finalizable
|
107
|
+
licenses:
|
108
|
+
- MIT
|
109
|
+
metadata:
|
110
|
+
homepage_uri: https://rubygems.org/gems/finalizable
|
111
|
+
source_code_uri: https://github.com/hgwr/finalizable/
|
112
|
+
changelog_uri: https://github.com/hgwr/finalizable/
|
113
|
+
post_install_message:
|
114
|
+
rdoc_options: []
|
115
|
+
require_paths:
|
116
|
+
- lib
|
117
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
118
|
+
requirements:
|
119
|
+
- - ">="
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: '2.4'
|
122
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
123
|
+
requirements:
|
124
|
+
- - ">="
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
version: '0'
|
127
|
+
requirements: []
|
128
|
+
rubygems_version: 3.1.2
|
129
|
+
signing_key:
|
130
|
+
specification_version: 4
|
131
|
+
summary: Preventing overriding specific methods.
|
132
|
+
test_files: []
|