Refactor API::Instance and reorganize DSL modules #2633
+194
−126
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.
Summary
This PR refactors the
Grape::API::Instanceclass and reorganizes methods across DSL modules to improve code organization and maintainability. The changes move routing-related methods fromInstanceto theDSL::Routingmodule, simplify instance variable access patterns, and improve test coverage by using actual HTTP calls instead of inspecting internal state.Changes
API::Instance Refactoring
instanceandbaseattr_readers with direct@instanceand@baseinstance variable accesscompileandcall!methods, consolidating logic intocompile!andcallchange!from protected to public section for better API consistencydef_delegators :basetodef_delegators :@baseto work with instance variablesDSL Module Reorganization
given,mounted, andcascademethods fromAPI::InstancetoDSL::Routingmodule where they logically belongparamsandcontractmethods to the top of the module, withreset_validations!moved to private sectionreset_routes!andreset_endpoints!to private section inDSL::Routingevaluate_as_instance_with_configurationto use@baseinstead ofbaseAPI Class Updates
base=,base_instance?,change!,inherit_settings,recognize_path,reset!,top_level_setting=,top_level_setting)change!,recognize_path, androutesto the list of methods delegated tobase_instanceSettings Module
top_level_settingfromattr_writerinDSL::Settings(now handled differently)Test Improvements
.compiletest that checked internal instance state.change!test: Now verifies that compilation happens twice after callingchange!Rack::MockRequest) instead of checking internal settings hashdeclared_paramsinternal stateBenefits
Files Changed
lib/grape/api.rblib/grape/api/instance.rblib/grape/dsl/routing.rblib/grape/dsl/settings.rblib/grape/dsl/validations.rbspec/grape/api_spec.rbspec/grape/dsl/routing_spec.rbspec/grape/dsl/validations_spec.rbspec/grape/validations_spec.rb