Module: Roda::RodaPlugins::SymbolMatchers

Defined in:
lib/roda/plugins/symbol_matchers.rb

Overview

The symbol_matchers plugin allows you do define custom regexps to use for specific symbols. For example, if you have a route such as:

r.on :username do
  # ...
end

By default this will match all nonempty segments. However, if your usernames must be 6-20 characters, and can only contain a-z and 0-9, you can do:

plugin :symbol_matchers
symbol_matcher :username, /([a-z0-9]{6,20})/

Then the route will only if the path is /foobar123, but not if it is /foo, /FooBar123, or /foobar_123.

By default, this plugin sets up the following symbol matchers:

:d

/(\d+)/, a decimal segment

:rest

/(.*)/, all remaining characters, if any

:w

/(\w+)/, a alphanumeric segment

If the placeholder_string_matchers plugin is loaded, this feature also applies to placeholders in strings, so the following:

r.on "users/:username" do
  # ...
end

Would match /users/foobar123, but not /users/foo, /users/FooBar123, or /users/foobar_123.

If using this plugin with the params_capturing plugin, this plugin should be loaded first.

Defined Under Namespace

Modules: ClassMethods, RequestMethods

Class Method Summary collapse

Class Method Details

.configure(app) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/roda/plugins/symbol_matchers.rb', line 45

def self.configure(app)
  app.symbol_matcher(:d, /(\d+)/)
  app.symbol_matcher(:w, /(\w+)/)
  app.symbol_matcher(:rest, /(.*)/)

  if !app.opts[:verbatim_string_matcher]
    # RODA3: Remove
    app::RodaRequest.class_eval do
      def match_symbol_format
        Roda::RodaPlugins.warn('Implicit use of the :format symbol matcher is deprecated and will be removed in Roda 3.  If you want to use the :format symbol matcher, add the following code to your Roda class: symbol_matcher(:format, /(?:\.(\w+))?/)')
        /(?:\.(\w+))?/
      end

      def match_symbol_opt
        Roda::RodaPlugins.warn('Implicit use of the :opt symbol matcher is deprecated and will be removed in Roda 3.  If you want to use the :opt symbol matcher, add the following code to your Roda class: symbol_matcher(:opt, /(?:\/([^\/]+))?/)')
        /(?:\/([^\/]+))?/
      end

      def match_symbol_optd
        Roda::RodaPlugins.warn('Implicit use of the :optd symbol matcher is deprecated and will be removed in Roda 3.  If you want to use the :optd symbol matcher, add the following code to your Roda class: symbol_matcher(:optd, /(?:\/(\d+))?/)')
        /(?:\/(\d+))?/
      end
    end
  end
end

.load_dependencies(app) ⇒ Object



41
42
43
# File 'lib/roda/plugins/symbol_matchers.rb', line 41

def self.load_dependencies(app)
  app.plugin :_symbol_regexp_matchers
end