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

Dynamic registration #2516

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

Conversation

ericproulx
Copy link
Contributor

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

#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've refactored the part where its was calling const_get to use a static array.

@ericproulx ericproulx marked this pull request as ready for review December 20, 2024 17:34
@ericproulx ericproulx requested a review from dblock December 20, 2024 17:34
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.

1 participant