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 = Date.new(y.to_i, m.to_i, d.to_i)
  # ...
end

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|
  [Date.new(y.to_i, m.to_i, d.to_i)]
end

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

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

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.

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