module Roda::RodaPlugins::IndifferentParams

  1. lib/roda/plugins/indifferent_params.rb

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. Example:

plugin :indifferent_params

route do |r|

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.


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