-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Add parameter alias #1674
Add parameter alias #1674
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find it rather strange that declared(params)
needs to be used to enable the alias. I feel like when I use as
, params
should just be the target structure and maybe there should be some other way to access the "raw" parameters. That seems more predictable, no?
README.md
Outdated
@@ -1094,6 +1095,23 @@ params do | |||
end | |||
``` | |||
|
|||
### Alias | |||
|
|||
It's possible to set alias for parameters using `as` (it can be useful when refactoring an existing API): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can set an alias for parameters using as
, which can be useful when refactoring existing APIs.
README.md
Outdated
end | ||
``` | ||
|
||
The value passed to `as` will be the key when calling `declared(params)` (calling only `params` will not set the alias). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe ", but params
will not use the alias."
@@ -115,10 +115,15 @@ def required? | |||
|
|||
# Adds a parameter declaration to our list of validations. | |||
# @param attrs [Array] (see Grape::DSL::Parameters#requires) | |||
def push_declared_params(attrs) | |||
def push_declared_params(attrs, param_alias = nil) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of param_alias
I would make it opts
and a Hash
of different options, starting with as
, for future proofing.
I am not sure if would be cool to use alias with |
When we use |
So much better! Merging. |
Nice work @glaucocustodio. |
Closes #1673.
Please, check if it's ok.
The methods
requires
andoptional
have aif opts[:using]
(grape/dsl/parameters.rb
) but I don't know how this condition can be true, I also didn't find any mention tousing
on readme.