Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revisit registration for parsers, formatters, versioners and validators #7

Open
wants to merge 19 commits into
base: master
Choose a base branch
from

Conversation

ericproulx
Copy link
Owner

@ericproulx ericproulx commented Dec 15, 2024

This PR brings a common way to load parsers, formatters, validators and middleware versioners. With this change, it facilitates the customization through inheritance.

Parsers, formatters and error formatters

Parsers, formatters and error formatters had static constant named DEFAULTS that was acting as a lookup to translate a format (:json, :xml, etc ...) to an actual class. Instead of relying on this, it will now use a dynamic registry Grape::Util::Registry that will be warmed up through inheritance like the validators (See base classes) Thus, zeitwerk, eager loads classes, so the registries are filled up when Grape is required.

Versioners

I applied the same pattern for middleware versioners since its translating a symbol to an actual class.

Validators

For the validators, I've just replace the actual registry by Grape::Util::Registry and I removed the const_get part since all validators are eager loaded.

Contract scope

I moved the contract scope validator to the validators folders and rename it to ContractScopeValidator to have a better short name than validator.

Grape::Util::Registry

ruby-grape#2437 introduced a length validator and users that already have one with the same name might not be aware that its been overridden. Giving the situation, the registry will emit a warning if an entry has changed. This is true for all classes that are using the registry.

DryTypeCoercer

I refactored the part where its was calling const_get to use a static array.

@coveralls
Copy link

coveralls commented Dec 15, 2024

Pull Request Test Coverage Report for Build 12481808979

Details

  • 194 of 199 (97.49%) changed or added relevant lines in 27 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.5%) to 98.335%

Changes Missing Coverage Covered Lines Changed/Added Lines %
lib/grape/error_formatter/base.rb 34 35 97.14%
lib/grape/formatter/base.rb 7 8 87.5%
lib/grape/formatter/json.rb 3 4 75.0%
lib/grape/parser/base.rb 7 8 87.5%
lib/grape/validations/types/dry_type_coercer.rb 5 6 83.33%
Totals Coverage Status
Change from base Build 12152126541: 0.5%
Covered Lines: 3544
Relevant Lines: 3604

💛 - Coveralls

@ericproulx ericproulx changed the title Dynamic registration Revisit registration for parsers, formatters, versioners and validators Dec 20, 2024
ericproulx and others added 2 commits December 24, 2024 00:56
Co-authored-by: Daniel (dB.) Doubrovkine <[email protected]>
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