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.params['bar'] #=> '123'
    r.params['baz'] #=> 'abc'
    r.params['quux'] #=> '67'
  end
end

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.params['x'] #=> nil
  r.params['y'] #=> nil
  r.params['captures'] #=> ["foo", "123", "abc", "67"]
end

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.params['captures'] # => ["foo", "123", "abc", "67"]
      end
    end
  end
end

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.

This plugin will also handle string matchers with placeholders if the placeholder_string_matchers plugin is loaded before this plugin.

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.