RuboCop::SortedMethodsByCall
Enforces "waterfall" method ordering: define methods after any method that calls them within the same scope.
- RuboCop::SortedMethodsByCall
- Features
- Installation
- Configuration
- Basic Setup
- Configuration Options
- Usage Examples
- Good Code (waterfall order)
- Bad Code (violates waterfall order)
- Autocorrection
- Testing
- Development
- Available Commands
- Release Process
- Requirements
- Contributing
- Documentation
- License
- Code of Conduct
Features
- Waterfall ordering enforcement: Caller methods must be defined before their callees;
-
Smart visibility handling: Respects
private/protected/publicsections; - Safe mutual recursion: Handles recursive method calls gracefully;
-
Autocorrection support: Automatically reorders methods (opt-in with
-A); - Full RuboCop integration: Works seamlessly with modern RuboCop plugin system;
- Comprehensive scope support: Classes, modules, singleton classes, and top-level;
Installation
Add this line to your application's Gemfile:
gem 'rubocop-sorted_methods_by_call'And then execute:
bundle installOr install it yourself:
gem install rubocop-sorted_methods_by_callConfiguration
Basic Setup
Add to your .rubocop.yml:
plugins:
- rubocop-sorted_methods_by_call
SortedMethodsByCall/Waterfall:
Enabled: trueConfiguration Options
SortedMethodsByCall/Waterfall:
Enabled: true
SafeAutoCorrect: false # Autocorrection requires -A flag
AllowedRecursion: true # Allow mutual recursion (default: true)Usage Examples
Good Code (waterfall order)
In waterfall ordering, callers come before callees. This creates a top-down reading flow where main logic appears before implementation details.
class Service
def call
foo
bar
end
private
def bar
method123
end
def method123
foo
end
def foo
123
end
endBad Code (violates waterfall order)
class Service
def call
foo
bar
end
private
def foo # ❌ Offense: Define #foo after its caller #method123
123
end
def bar
method123
end
def method123
foo
end
endAutocorrection
Run with unsafe autocorrection to automatically fix violations:
bundle exec rubocop -AThis will reorder the methods while preserving comments and visibility modifiers.
Testing
Run the test suite:
bundle exec rspecRun RuboCop on the gem itself:
bundle exec rubocop
bundle exec rubocop --config test_project/.rubocop.test.yml lib/ -ADevelopment
After checking out the repo, run:
bin/setupThis will install dependencies and start an interactive console.
Available Commands
-
bin/console- Interactive development console -
bin/setup- Install dependencies and build gem -
bundle exec rake- Run tests and linting
Release Process
- Update version in
lib/rubocop/sorted_methods_by_call/version.rb - Create and push a git tag:
git tag v0.1.0 && git push origin v0.1.0 - GitHub Actions will automatically:
- Build the gem
- Publish to RubyGems.org
- Create a GitHub release
Requirements
- Ruby: >= 2.7
- RuboCop: >= 1.72.0 (required for plugin system)
Contributing
Bug reports and pull requests are welcome! Please follow these guidelines:
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -am 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a pull request
Please ensure your code passes all tests and follows the existing style.
Documentation
Code is covered with YARD docs, you can access online docs at https://unurgunite.github.io/rubocop-sorted_methods_by_call_docs/
License
The gem is available as open source under the terms of MIT License.
Code of Conduct
Everyone interacting with this project is expected to follow the Code of Conduct.
Note: This gem implements true waterfall ordering that considers the complete call graph across all methods in a scope. Methods are ordered so that every callee appears after all of its callers, creating a natural top-down reading flow.