Module: Grape::DSL::Parameters
- Included in:
- Validations::ParamsScope
- Defined in:
- lib/grape/dsl/parameters.rb
Overview
Defines DSL methods, meant to be applied to a ParamsScope, which define and describe the parameters accepted by an endpoint, or all endpoints within a namespace.
Constant Summary collapse
- EmptyOptionalValue =
Object.new.freeze
Instance Method Summary collapse
-
#build_with(build_with) ⇒ Object
Set the module used to build the request.params.
-
#declared_param?(param) ⇒ Boolean
Test for whether a certain parameter has been defined in this params block yet.
-
#given(*attrs) { ... } ⇒ Object
Define a block of validations which should be applied if and only if the given parameter is present.
-
#optional(*attrs, using: nil, except: nil, as: nil, **opts) ⇒ Object
Allow, but don't require, one or more parameters for the current endpoint.
-
#params(params) ⇒ Object
private
Hash of parameters relevant for the current scope.
-
#requires(*attrs, using: nil, except: nil, as: nil, **opts) ⇒ Object
(also: #group)
Require one or more parameters for the current endpoint.
-
#use(*names, **options) ⇒ Object
(also: #use_scope, #includes)
Include reusable params rules among current.
-
#with(**opts) ⇒ Object
Define common settings for one or more parameters.
Instance Method Details
#build_with(build_with) ⇒ Object
Set the module used to build the request.params.
31 32 33 |
# File 'lib/grape/dsl/parameters.rb', line 31 def build_with(build_with) @api.inheritable_setting.build_params_with = build_with end |
#declared_param?(param) ⇒ Boolean
Test for whether a certain parameter has been defined in this params block yet.
175 176 177 178 179 180 181 182 183 184 |
# File 'lib/grape/dsl/parameters.rb', line 175 def declared_param?(param) # Elements of @declared_params of lateral scope are pushed in @parent. So check them in @parent. return @parent.declared_param?(param) if lateral? # @declared_params also includes hashes of options and such, but those # won't be flattened out. @declared_params.flatten.any? do |declared_param_attr| first_hash_key_or_param(declared_param_attr.key) == param end end |
#given(*attrs) { ... } ⇒ Object
Define a block of validations which should be applied if and only if the given parameter is present. The parameters are not nested.
164 165 166 167 168 169 170 |
# File 'lib/grape/dsl/parameters.rb', line 164 def given(*attrs, &) attrs.each do |attr| proxy_attr = first_hash_key_or_param(attr) raise Grape::Exceptions::UnknownParameter.new(proxy_attr) unless declared_param?(proxy_attr) end new_lateral_scope(dependent_on: attrs, &) end |
#optional(*attrs, using: nil, except: nil, as: nil, **opts) ⇒ Object
Allow, but don't require, one or more parameters for the current endpoint.
137 138 139 140 141 |
# File 'lib/grape/dsl/parameters.rb', line 137 def optional(*attrs, using: nil, except: nil, as: nil, **opts, &) return (:optional, attrs, { using:, except:, as: }.compact.merge(opts), &) if (attrs) declare(attrs, opts, required: false, using:, except:, as:, &) end |
#params(params) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns hash of parameters relevant for the current scope.
193 194 195 196 |
# File 'lib/grape/dsl/parameters.rb', line 193 def params(params) scoped = @parent ? (@parent..presence || @parent.params(params)) : params @element ? map_params(scoped, @element) : scoped end |
#requires(*attrs, using: nil, except: nil, as: nil, **opts) ⇒ Object Also known as: group
Require one or more parameters for the current endpoint.
127 128 129 130 131 |
# File 'lib/grape/dsl/parameters.rb', line 127 def requires(*attrs, using: nil, except: nil, as: nil, **opts, &) return (:requires, attrs, { using:, except:, as: }.compact.merge(opts), &) if (attrs) declare(attrs, opts, required: true, using:, except:, as:, &) end |
#use(*names, **options) ⇒ Object Also known as: use_scope, includes
Include reusable params rules among current. You can define reusable params with helpers method.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/grape/dsl/parameters.rb', line 56 def use(*names, **) return (:use, names, ) if (names) named_params = @api.inheritable_setting.named_params || {} names.each do |name| params_block = named_params.fetch(name) do raise "Params :#{name} not found!" end if .empty? instance_exec(, ¶ms_block) else instance_exec(**, ¶ms_block) end end end |
#with(**opts) ⇒ Object
Define common settings for one or more parameters
146 147 148 149 |
# File 'lib/grape/dsl/parameters.rb', line 146 def with(**opts, &) new_group_attrs = @group&.deep_merge(opts) || opts new_group_scope(new_group_attrs, &) end |