lib/roda/plugins/typecast_params.rb
# Runs conversions similar to convert! for each key specified by the :keys option.  If :keys option is not given
# and the object is an array, runs conversions for all entries in the array.  If the :keys
# option is not given and the object is a Hash with string keys '0', '1', ..., 'N' (with
# no skipped keys), runs conversions for all entries in the hash.  If :keys option is a Proc
# or a Method, calls the proc/method with the current object, which should return an
# array of keys to use.
# Supports options given to #convert!, and this additional option:
#
# :keys :: The keys to extract from the object. If a proc or method,
#          calls the value with the current object, which should return the array of keys
#          to use.
Runs conversions similar to convert. for each key specified by the :keys option.  If :keys option is not given
and the object is an array, runs conversions for all entries in the array.

:keys :: The keys to extract from the object. If a proc or method,
calls the value with the current object, which should return the array of keys
to use.
Runs conversions similar to convert . for each key specified by the : keys option . If : keys option is not given and the object is an array runs conversions for all entries in the array . : keys :: The keys to extract from the object . If a proc or method calls the value with the current object which should return the array of keys to use .
def convert_each!(opts=OPTS, &block)
          np = !@capture
          _capture!(nil, opts) do
            case keys = opts[:keys]
            when nil
              keys = (0...@obj.length)
              valid = if @obj.is_a?(Array)
                true
              else
                keys = keys.map(&:to_s)
                keys.all?{|k| @obj.has_key?(k)}
              end
              unless valid
                handle_error(nil, :invalid_type, "convert_each! called on object not an array or hash with keys '0'..'N'")
                next 
              end
            when Array
            when Proc, Method
              keys = keys.call(@obj)
            else
              raise ProgrammerError, "unsupported convert_each! :keys option: #{keys.inspect}"
            end
            keys.map do |i|
              begin
                if v = subkey([i], opts.fetch(:raise, true))
                  yield v
                  v.nested_params if np 
                end
              rescue => e
                handle_error(i, :invalid_type, e)
              end
            end
          end
        end
def convert_each! ( opts = OPTS , & block ) np = ! @capture _capture! ( nil , opts ) do case keys = opts [ :keys ] when nil keys = ( 0 ... @obj . length ) valid = if @obj . is_a? ( Array ) true else keys = keys . map ( & :to_s ) keys . all? { | k | @obj . has_key? ( k ) } end unless valid handle_error ( nil , :invalid_type , "convert_each! called on object not an array or hash with keys '0'..'N'" ) next end when Array when Proc , Method keys = keys . call ( @obj ) else raise ProgrammerError , "unsupported convert_each! :keys option: #{keys.inspect}" end keys . map do | i | begin if v = subkey ( [ i ] , opts . fetch ( :raise , true ) ) yield v v . nested_params if np end rescue => e handle_error ( i , :invalid_type , e ) end end end end
Runs conversions similar to convert!
Runs conversions similar to convert!
# nothing to do
opts
block