Module: Padrino::ParamsProtection::InstanceMethods

### Home Classes Methods

### In Files

- lib/padrino-core/application/params_protection.rb

### Methods

- #filter_params!
- #original_params

### Files

- README.rdoc

### Class/Module Index

Quicksearch

- Padrino
- Padrino::Application
- Padrino::ApplicationLoadError
- Padrino::ApplicationSetup
- Padrino::ApplicationSetup::ClassMethods
- Padrino::AuthenticityToken
- Padrino::Cli
- Padrino::Cli::Adapter
- Padrino::Cli::Base
- Padrino::Cli::Launcher
- Padrino::Filter
- Padrino::Flash
- Padrino::Flash::Helpers
- Padrino::Flash::Storage
- Padrino::Loader
- Padrino::Logger
- Padrino::Logger::Colorize
- Padrino::Logger::Extensions
- Padrino::Logger::Rack
- Padrino::Module
- Padrino::Mounter
- Padrino::Mounter::ApplicationWrapper
- Padrino::Mounter::MounterException
- Padrino::ParamsProtection
- Padrino::ParamsProtection::ClassMethods
- Padrino::ParamsProtection::InstanceMethods
- Padrino::Reloader
- Padrino::Reloader::Rack
- Padrino::Reloader::Storage
- Padrino::Router
- Padrino::Routing
- Padrino::Routing::ClassMethods
- Padrino::Routing::InstanceMethods
- Padrino::Routing::Parent
- Padrino::Routing::UnrecognizedException
- Padrino::Server
- Padrino::ShowExceptions
- Padrino::Tasks
- HttpRouter
- HttpRouter::Node
- HttpRouter::Node::FreeRegex
- HttpRouter::Node::Glob
- HttpRouter::Node::Path
- HttpRouter::Node::SpanningRegex
- HttpRouter::Request
- HttpRouter::Route
- Sinatra
- Sinatra::Base
- Sinatra::Request
- Kernel
- Object
- PadrinoTasks

No matching classes.

# Padrino::ParamsProtection::InstanceMethods

### Public Instance Methods

filter_params!(params, allowed_params) click to toggle source

Filters a hash of parameters leaving only allowed ones and possibly typecasting and processing the others.

@param [Hash] params

    Parameters to filter.
    Warning: this hash will be changed by deleting or replacing its values.

@param [Hash] allowed_params

    A hash of allowed keys and value classes or processing procs. Supported
    scalar classes are: Integer (empty string is cast to nil).

@example

    filter_params!( { "a" => "1", "b" => "abc", "d" => "drop" },
                    { "a" => Integer, "b" => true } )
    # => { "a" => 1, "b" => "abc" }
    filter_params!( { "id" => "", "child" => { "name" => "manny" } },
                    { "id" => Integer, "child" => { "name" => proc{ |v| v.camelize } } } )
    # => { "id" => nil, "child" => { "name" => "Manny" } }
    filter_params!( { "a" => ["1", "2", "3"] },
                    { "a" => true } )
    # => { "a" => ["1", "2", "3"] }
    filter_params!( { "persons" => {"p-1" => { "name" => "manny", "age" => "50" }, "p-2" => { "name" => "richard", "age" => "50" } } },
                    { "persons" => { "name" => true } } )
    # => { "persons" => {"p-1" => { "name" => "manny" }, "p-2" => { "name" => "richard" } } }

    # File lib/padrino-core/application/params_protection.rb, line 97
    def filter_params!(params, allowed_params)
      params.each do |key,value|
        type = allowed_params[key]
        next if value.kind_of?(Array) && type
        case
        when type.kind_of?(Hash) && value.kind_of?(Hash)
          if key == key.pluralize && value.values.first.kind_of?(Hash)
            value.each do |array_index,array_value|
              value[array_index] = filter_params!(array_value, type)
            end
          else
            params[key] = filter_params!(value, type)
          end
        when type == Integer
          params[key] = value.empty? ? nil : value.to_i
        when type.kind_of?(Proc)
          params[key] = type.call(value)
        when type == true
        else
          params.delete(key)
        end
      end
    end

original_params() click to toggle source

Returns the original unfiltered query parameters hash.

    # File lib/padrino-core/application/params_protection.rb, line 124
    def original_params
      @original_params || params
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.