module Roda::RodaPlugins::ClassMatchers

  1. lib/roda/plugins/class_matchers.rb

The class_matchers plugin allows you do define custom regexps and conversion procs to use for specific classes. For example, if you have multiple routes similar to:

r.on /(\d\d\d\d)-(\d\d)-(\d\d)/ do |y, m, d|
  date =, m.to_i, d.to_i)
  # ...

You can register a Date class matcher for that regexp (note that the block must return an array):

class_matcher(Date, /(\d\d\d\d)-(\d\d)-(\d\d)/) do |y, m, d|
  [, m.to_i, d.to_i)]

And then use the Date class as a matcher, and it will yield a Date object:

r.on Date do |date|
  # ...

This is useful to DRY up code if you are using the same type of pattern and type conversion in multiple places in your application.

If you have a segment match the passed regexp, but decide during block processing that you do not want to treat it as a match, you can have the block return nil or false. This is useful if you want to make sure you are using valid data:

class_matcher(Date, /(\dd\d)-(\d\d)-(\d\d)/) do |y, m, d|
  y = y.to_i
  m = m.to_i
  d = d.to_i
  [, m, d)] if Date.valid_date?(y, m, d)

This plugin does not work with the params_capturing plugin, as it does not offer the ability to associate block arguments with named keys.