Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/joker1007/finalist
Finalist adds `final` method modifier. `final` forbids method override.
https://github.com/joker1007/finalist
Last synced: 2 days ago
JSON representation
Finalist adds `final` method modifier. `final` forbids method override.
- Host: GitHub
- URL: https://github.com/joker1007/finalist
- Owner: joker1007
- License: mit
- Created: 2018-01-24T14:54:56.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2019-04-04T18:00:34.000Z (over 5 years ago)
- Last Synced: 2024-04-15T22:38:37.904Z (7 months ago)
- Language: Ruby
- Homepage:
- Size: 17.6 KB
- Stars: 39
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Finalist
[![Build Status](https://travis-ci.org/joker1007/finalist.svg?branch=master)](https://travis-ci.org/joker1007/finalist)
[![Gem Version](https://badge.fury.io/rb/finalist.svg)](https://badge.fury.io/rb/finalist)Finalist adds `final` method modifier.
`final` forbids method override.This gem is pseudo static code analyzer by `method_added` and `singleton_method_added` and `included` and `extended`.
it detect final violation when class(module) is defined, not runtime.
Simple case is following.
```ruby
class A1
extend Finalistfinal def foo
end
endclass A2 < A1
def foo # => raise
end
end
```This case raises `Finalist::OverrideFinalMethodError` at `def foo in A2 class`.
This gem supports other cases.
(see [finalist_spec.rb](https://github.com/joker1007/finalist/blob/master/spec/finalist_spec.rb))### My similar gems
- [overrider](https://github.com/joker1007/overrider) (`override` implementation)
- [abstriker](https://github.com/joker1007/abstriker) (`abstract` implementation)## Requirements
- ruby-2.5.0 or later (depend on `UnbountMethod#super_method` behavior)## Installation
Add this line to your application's Gemfile:
```ruby
gem 'finalist'
```And then execute:
$ bundle
Or install it yourself as:
$ gem install finalist
## Usage
A class or module extends `Finalist` module
And add `final` modifier to target method.
(`final` can accept symbol as method name.)### for Production
If you want to disable Finalist, write `Finalist.disable = true` at first line.### Examples
#### include module
```ruby
module E1
extend Finalistfinal def foo
end
endmodule E2
include E1
endmodule E3
include E2def foo # => raise
end
end
```#### include module after override
```ruby
module F1
extend Finalistfinal def foo
end
endclass F2
def foo
endinclude F1 # => raise
end
```#### class method
```ruby
class J1
extend Finalistclass << self
final def foo
end
end
endclass J2 < J1
class << self
def foo # => raise
end
end
end
```#### extend object
```ruby
module H1
extend Finalistfinal def foo
end
enda = "str"
a.extend(H1)
def a.foo # => raise
end
```#### extend object after override
```ruby
module I1
extend Finalistfinal def foo
end
enda = "str"
def a.foo
end
a.extend(I1) # => raise
```#### class method by extend module
```ruby
module K1
extend Finalistfinal def foo
end
endclass K2
extend K1class << self
def foo # => raise
end
end
end
```#### class method by extend module after override
```ruby
module L1
extend Finalistfinal def foo
end
endclass L2
class << self
def foo
end
endextend L1 # => raise
end
```#### overrided by module prepend
This case is a intended loophole.
```ruby
module M1
extend Finalistfinal def foo
end
endmodule M3
def foo
"foo"
end
endclass M2
include M1
prepend M3
endM2.new.foo # => "foo"
```## How is this implemented?
Use so many ruby hooks. `method_added` and `singleton_method_added` and `included` and `extended`.
## Development
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.
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).
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/joker1007/finalist.
## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).