Module: Roda::RodaPlugins::IndifferentParams

Defined in:
lib/roda/plugins/indifferent_params.rb

Overview

The indifferent_params plugin adds a params instance method which offers indifferent access to the request params, allowing you to use symbols to lookup values in a hash where the keys are strings. Note that while this allows for an easier transition from some other ruby frameworks, it is a bad idea in general as it makes it more difficult to separate external data from internal data, and doesn’t handle any typecasting of the data. Consider using the typecast_params plugin instead of this plugin for accessing parameters.

Example:

plugin :indifferent_params

route do |r|
  params[:foo]
end

The exact behavior depends on the version of Rack in use. If you are using Rack 2, this plugin uses rack’s API to set the query parser for the request to use indifferent access. Rack 1 doesn’t support indifferent access to params, so if you are using Rack 1, this plugin will make a deep copy of the request params hash, where each level uses indifferent access. On Rack 1, The params hash is initialized lazily, so you only pay the penalty of copying the request params if you call the params method.

Note that there is a rack-indifferent gem that monkey patches rack to always use indifferent params. If you are using Rack 1, it is recommended to use rack-indifferent instead of this plugin, as it is faster and has some other minor advantages, though it affects all rack applications instead of just the Roda app that you load the plugin into.

Defined Under Namespace

Modules: InstanceMethods, RequestMethods Classes: QueryParser

Constant Summary collapse

INDIFFERENT_PROC =
lambda{|h,k| h[k.to_s] if k.is_a?(Symbol)}