Class: Grape::Validations::AttributesIterator
- Inherits:
-
Object
- Object
- Grape::Validations::AttributesIterator
- Defined in:
- lib/grape/validations/attributes_iterator.rb
Direct Known Subclasses
Instance Method Summary collapse
- #each(params) ⇒ Object
-
#initialize(attrs, scope) ⇒ AttributesIterator
constructor
attrsandscopeare static per validator; onlyparamsvaries per request, so an instance can be built once and reused (it keeps no request-derived state).
Constructor Details
#initialize(attrs, scope) ⇒ AttributesIterator
attrs and scope are static per validator; only params varies
per request, so an instance can be built once and reused (it keeps
no request-derived state). Reused instances are shared across
threads, so each must stay free of mutable instance state.
10 11 12 13 14 15 16 17 18 |
# File 'lib/grape/validations/attributes_iterator.rb', line 10 def initialize(attrs, scope) @attrs = attrs @scope = scope # How many times #do_each may descend into a nested array. The # declaration allows one level per Array-typed scope on the chain, less # the one +Array.wrap+ already consumes in #each. Anything deeper was # put there by the request, not by the declaration. @max_nesting = [scope.array_depth - 1, 0].max end |
Instance Method Details
#each(params) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/grape/validations/attributes_iterator.rb', line 20 def each(params, &) original_params = @scope.params(params) # A scope resolves to a Hash unless the declaration nests arrays, and # then #do_each has nothing to do but hand it straight back: Array.wrap # boxes it, the loop unboxes it on its only iteration, and with no Array # anywhere neither the nesting descent nor the index bookkeeping # applies. Every validator on a flat +params+ block comes through here. return yield_attributes(original_params, &) if original_params.is_a?(Hash) && !@scope.iterates_elements? # because we need recursion for nested arrays do_each(Array.wrap(original_params), original_params, &) end |