module Roda::RodaPlugins::ParamsCapturing

  1. lib/roda/plugins/params_capturing.rb

The params_capturing plugin makes symbol matchers update the request params with the value of the captured segments, using the matcher as the key:

plugin :params_capturing

route do |r|
  # GET /foo/123/abc/67
  r.on("foo", :bar, :baz, :quux) do
    r[:bar] #=> '123'
    r[:baz] #=> 'abc'
    r[:quux] #=> '67'

Note that this updating of the request params using the matcher as the key is only done if all arguments to the matcher are symbols or strings.

All matchers will update the request params by adding all captured segments to the captures key:

r.on(:x, /(\d+)\/(\w+)/, :y) do
  r[:x] #=> nil
  r[:y] #=> nil
  r[:captures] #=> ["foo", "123", "abc", "67"]

Note that the request params captures entry will be appended to with each nested match:

r.on(:w) do
  r.on(:x) do
    r.on(:y) do
      r.on(:z) do
        r[:captures] # => ["foo", "123", "abc", "67"]

Note that any existing params captures entry will be overwritten by this plugin. You can use r.GET or r.POST to get the underlying entry, depending on how it was submitted.

Also note that the param keys are actually stored in r.params as strings and not symbols (r[] converts the argument to a string before looking it up in r.params).

This plugin will also handle string matchers if placeholders in string matchers are supported.

Also note that this plugin will not work correctly if you are using the symbol_matchers plugin with custom symbol matching and are using symbols that capture multiple values or no values.