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

Merged
merged 19 commits into from
Dec 27, 2024
Merged

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
Copy link
Member

@dblock dblock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we mention that Grape::XYZ::DEFAULTS no longer exists in UPGRADING?

CHANGELOG.md Outdated Show resolved Hide resolved
ericproulx and others added 2 commits December 24, 2024 00:56
Co-authored-by: Daniel (dB.) Doubrovkine <[email protected]>
@ericproulx
Copy link
Contributor Author

Should we mention that Grape::XYZ::DEFAULTS no longer exists in UPGRADING?

Not sure its relevant since its been added 5 month ago. PR
I'm bringing back the registrable but on a new form.

@dblock dblock merged commit 7ec3e6d into ruby-grape:master Dec 27, 2024
54 checks passed
@ericproulx ericproulx deleted the dynamic_registration branch February 1, 2025 16:53
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