Refactor: Simplify Setting Methods and Remove Dynamic Method Generation #2599
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Refactor: Simplify Setting Methods and Remove Dynamic Method Generation
Overview
This PR refactors the Grape DSL settings implementation to improve code clarity, maintainability, and performance by replacing dynamic method generation with explicit method definitions and simplifying the
get_or_set
method.Changes Made
1. Simplified
get_or_set
Method__send__
to access setting objects2. Replaced Dynamic Method Generation with Explicit Methods
define_method
to dynamically create setting methods3. Optimized Logger Implementation
global_setting
method which added an extra layer of indirection4. Improved Test Coverage
Benefits
Files Changed
lib/grape/dsl/settings.rb
- Refactored setting methods andget_or_set
implementationlib/grape/dsl/logger.rb
- Optimized logger to directly access global settingsspec/grape/dsl/settings_spec.rb
- Updated tests to focus on behaviorspec/grape/dsl/logger_spec.rb
- Simplified test setupTesting
All existing tests pass, and the refactoring maintains backward compatibility. The changes are purely internal improvements to the DSL implementation.
Breaking Changes
None. This is a backward-compatible refactoring that maintains the same public API.