Skip to content

Conversation

@ericproulx
Copy link
Contributor

Summary

This PR refactors the Grape::API::Instance class and reorganizes methods across DSL modules to improve code organization and maintainability. The changes move routing-related methods from Instance to the DSL::Routing module, simplify instance variable access patterns, and improve test coverage by using actual HTTP calls instead of inspecting internal state.

Changes

API::Instance Refactoring

  • Removed attr_readers: Replaced instance and base attr_readers with direct @instance and @base instance variable access
  • Simplified compilation: Removed separate compile and call! methods, consolidating logic into compile! and call
  • Method visibility: Moved change! from protected to public section for better API consistency
  • Updated delegators: Changed def_delegators :base to def_delegators :@base to work with instance variables

DSL Module Reorganization

  • Moved methods to DSL::Routing: Relocated given, mounted, and cascade methods from API::Instance to DSL::Routing module where they logically belong
  • Method ordering in DSL::Validations: Moved params and contract methods to the top of the module, with reset_validations! moved to private section
  • Private method organization: Moved reset_routes! and reset_endpoints! to private section in DSL::Routing
  • Instance variable access: Updated evaluate_as_instance_with_configuration to use @base instead of base

API Class Updates

  • NON_OVERRIDABLE list: Expanded to include additional methods that should not be overridden (base=, base_instance?, change!, inherit_settings, recognize_path, reset!, top_level_setting=, top_level_setting)
  • Delegators: Added change!, recognize_path, and routes to the list of methods delegated to base_instance

Settings Module

  • Removed attr_writer: Removed top_level_setting from attr_writer in DSL::Settings (now handled differently)

Test Improvements

  • Removed internal state tests: Removed .compile test that checked internal instance state
  • Updated .change! test: Now verifies that compilation happens twice after calling change!
  • Integration-style routing tests: Updated routing tests to use actual HTTP calls (Rack::MockRequest) instead of checking internal settings hash
  • Integration-style validation tests: Updated validation tests to use actual HTTP calls and verify JSON responses instead of directly checking declared_params internal state

Benefits

  1. Better code organization: Methods are now located in their appropriate DSL modules
  2. Improved test coverage: Tests now verify actual behavior through HTTP calls rather than internal state
  3. Cleaner API: Direct instance variable access reduces unnecessary method indirection
  4. Better encapsulation: Private methods are properly scoped

Files Changed

  • lib/grape/api.rb
  • lib/grape/api/instance.rb
  • lib/grape/dsl/routing.rb
  • lib/grape/dsl/settings.rb
  • lib/grape/dsl/validations.rb
  • spec/grape/api_spec.rb
  • spec/grape/dsl/routing_spec.rb
  • spec/grape/dsl/validations_spec.rb
  • spec/grape/validations_spec.rb

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