Skip to content

Conversation

ericproulx
Copy link
Contributor

Refactor versioner middleware: simplify base class and improve consistency

Summary

This PR refactors the Grape versioner middleware to improve code maintainability, reduce complexity, and enhance consistency across the versioning components.

Changes Made

🔧 Base Class Simplification

  • Replaced individual method definitions with define_method for options access
  • Consolidated version options handling in a more maintainable way
  • Streamlined initialization logic with better error headers setup

🎯 Method Consistency

  • Replaced strict? method calls with direct strict access for consistency
  • Updated parameter_key references to use the parameter method directly
  • Improved version matching logic for better performance

📊 Code Reduction

  • Net reduction: 27 insertions, 45 deletions (-18 lines)
  • Simplified Base class from 67 lines to ~49 lines
  • Removed unnecessary method indirection

Files Modified

  • lib/grape/middleware/versioner/base.rb - Major refactoring of base class
  • lib/grape/middleware/versioner/accept_version_header.rb - Method call consistency
  • lib/grape/middleware/versioner/header.rb - Method call consistency
  • lib/grape/middleware/versioner/param.rb - Parameter method consistency

Benefits

Improved maintainability - Less code to maintain with same functionality
Better consistency - Unified approach to accessing options and methods
Enhanced readability - Cleaner, more straightforward code structure
Performance improvement - Direct method access instead of method indirection

Testing

  • All existing functionality preserved
  • No breaking changes to public API
  • Maintains backward compatibility with existing versioning behavior

Branch

small_refactor_versioner_middleware


This refactoring maintains the same external behavior while significantly improving the internal code structure and maintainability of the versioner middleware components.

- Remove unset, unset_namespace_stackable, and namespace_inheritable_to_nil methods
- Simplify reset_validations! to call delete directly instead of through wrappers
- Enhance BaseInheritable#delete to accept multiple keys
- Improve without_root_prefix_and_versioning with proper ensure block cleanup
- Update tests to verify actual behavior instead of method calls

These methods were internal APIs not intended for public use. Removing them
reduces the public API surface and simplifies the codebase.
- Replace dynamic method generation with explicit method definitions
- Simplify get_or_set to work directly with setting objects instead of type symbols
- Update logger to directly access global settings for better performance
- Improve test coverage to focus on actual behavior rather than delegation
- Remove unnecessary complexity in settings DSL implementation
…tency

- Remove method indirection in Base class by using define_method for options
- Consolidate version options handling and improve initialization
- Replace strict? method calls with direct strict access
- Fix parameter_key references to use parameter method directly
- Improve version matching logic and error handling consistency
@ericproulx ericproulx force-pushed the small_refactor_versioner_middleware branch from f1874f1 to 1b369a7 Compare September 20, 2025 12:16
@ericproulx ericproulx requested a review from dblock September 20, 2025 12:40
@ericproulx ericproulx marked this pull request as ready for review September 20, 2025 12:40
@dblock dblock merged commit dd4fa3a into master Sep 21, 2025
93 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants