Refactor: Extract declared method into separate DSL module #2637
+153
−144
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: Extract
declaredmethod into separate DSL moduleSummary
This PR extracts the
declaredmethod and its related logic fromGrape::DSL::InsideRoute::PostBeforeFilterinto a new dedicatedGrape::DSL::Declaredmodule. This refactoring simplifies the codebase by replacing the dynamic module extension pattern (usingextend) with a static include-based approach.Changes
New Module (
lib/grape/dsl/declared.rb)Grape::DSL::Declaredmodule: Extracts alldeclaredmethod logic, including:declaredpublic methoddeclared_array,declared_hash,declared_hash_attr,handle_passed_param,optioned_param_key,optioned_declared_params)MethodNotYetAvailableexception classSimplified
InsideRoute(lib/grape/dsl/inside_route.rb)PostBeforeFiltermodule: All logic moved toDeclaredmodulepost_filter_methodsclass method: No longer needed with the new patterninclude Declaredinstead of dynamically extending modules viaextend post_extensionafter filter executionEndpoint Changes (
lib/grape/endpoint.rb)@before_filter_passedinstance variable: Tracks whether before filters have completedbefore_filter_passedattr_reader: Allows theDeclaredmodule to check filter state@before_filter_passed = trueis set afterrun_filters befores, :beforeextend post_extensionafter running filtersTest Updates (
spec/grape/dsl/inside_route_spec.rb)Declaredmodule isolationBenefits
declaredmethod logic is now in its own dedicated moduleextend) with static module inclusion (compile-timeinclude)post_filter_methodspattern in favor of a straightforward flag checkTechnical Details
The
declaredmethod now checks thebefore_filter_passedflag directly instead of relying on dynamic module extension timing. Previously, the code usedextend PostBeforeFilterdynamically after filter execution to make methods available. This has been replaced with a staticinclude Declaredthat is always present, but the methods check thebefore_filter_passedflag to enforce the same behavior (preventingdeclaredfrom being called before parameter validation). This provides the same functionality but with a simpler, more explicit, and more performant mechanism.Testing
declaredmethod behavior remains unchangedNotes
This is a pure refactoring with no functional changes. The
declaredmethod's behavior and API remain exactly the same, but the internal implementation is cleaner and more maintainable. The key improvement is replacing the dynamicextendpattern with a staticincludepattern.