Class: Rack::Params::ArrayContext

Inherits:
Context
  • Object
show all
Defined in:
lib/rack/params/context.rb

Overview

the DSL for validating an array parameter

Constant Summary

Result =
Array

Instance Method Summary collapse

Constructor Details

This class inherits a constructor from Rack::Params::Context

Instance Method Details

#every(type, options = {}) { ... } ⇒ Object

validate and coerce every element in the array, using the same values. equivalent to HashContext#param over every element.

Parameters:

  • type

    the type to use for coercion

  • options (Hash) (defaults to: {})

Yields:

  • if type is Hash or Array, passing a block will recursively validate the coerced value, assuming the block is a new validation context.

Returns:

  • the coerced value

See Also:



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/rack/params/context.rb', line 195

def every(type, options = {}, &block)
  params.each_with_index do |value, i|
    begin
      value = _coerce(value, type, options)
      # options.each { |v, vopts| _validate(i.to_s, value, v, vopts) }
      if block_given?
        value = _recurse(i.to_s, type, value, &block)
        result.errors.merge! value.errors
      end

      result[i] = value
    rescue ArgumentError => ex
      path = [@path, i].reject(&:nil?).join(".")
      result.errors[path] << ex.message
      nil
    end
  end
end