Sha256: 5db807e000f50908d16939939116b621a1f88c9ee2c06ca55ec5080741096579

Contents?: true

Size: 1.68 KB

Versions: 90

Compression:

Stored size: 1.68 KB

Contents

# frozen-string-literal: true

#
class Roda
  module RodaPlugins
    # The path_matchers plugin adds hash matchers that operate
    # on the request's path.
    #
    # It adds a :prefix matcher for matching on the path's prefix,
    # yielding the rest of the matched segment:
    #
    #   r.on prefix: 'foo' do |suffix|
    #     # Matches '/foo-bar', yielding '-bar'
    #     # Does not match bar-foo
    #   end
    #
    # It adds a :suffix matcher for matching on the path's suffix,
    # yielding the part of the segment before the suffix:
    #
    #   r.on suffix: 'bar' do |prefix|
    #     # Matches '/foo-bar', yielding 'foo-'
    #     # Does not match bar-foo
    #   end
    #
    # It adds an :extension matcher for matching on the given file extension,
    # yielding the part of the segment before the extension:
    #
    #   r.on extension: 'bar' do |reset|
    #     # Matches '/foo.bar', yielding 'foo'
    #     # Does not match bar.foo
    #   end
    module PathMatchers
      module RequestMethods
        # Match when the current segment ends with the given extension.
        # request path end with the extension.
        def match_extension(ext)
          match_suffix(".#{ext}")
        end

        # Match when the current path segment starts with the given prefix.
        def match_prefix(prefix)
          consume(self.class.cached_matcher([:prefix, prefix]){/#{prefix}([^\\\/]+)/})
        end

        # Match when the current path segment ends with the given suffix.
        def match_suffix(suffix)
          consume(self.class.cached_matcher([:suffix, suffix]){/([^\\\/]+)#{suffix}/})
        end
      end
    end

    register_plugin(:path_matchers, PathMatchers)
  end
end

Version data entries

90 entries across 90 versions & 1 rubygems

Version Path
roda-3.67.0 lib/roda/plugins/path_matchers.rb
roda-3.66.0 lib/roda/plugins/path_matchers.rb
roda-3.65.0 lib/roda/plugins/path_matchers.rb
roda-3.64.0 lib/roda/plugins/path_matchers.rb
roda-3.63.0 lib/roda/plugins/path_matchers.rb
roda-3.62.0 lib/roda/plugins/path_matchers.rb
roda-3.61.0 lib/roda/plugins/path_matchers.rb
roda-3.60.0 lib/roda/plugins/path_matchers.rb
roda-3.59.0 lib/roda/plugins/path_matchers.rb
roda-3.58.0 lib/roda/plugins/path_matchers.rb
roda-3.57.0 lib/roda/plugins/path_matchers.rb
roda-3.56.0 lib/roda/plugins/path_matchers.rb
roda-3.55.0 lib/roda/plugins/path_matchers.rb
roda-3.54.0 lib/roda/plugins/path_matchers.rb
roda-3.53.0 lib/roda/plugins/path_matchers.rb
roda-3.52.0 lib/roda/plugins/path_matchers.rb
roda-3.51.0 lib/roda/plugins/path_matchers.rb
roda-3.50.0 lib/roda/plugins/path_matchers.rb
roda-3.49.0 lib/roda/plugins/path_matchers.rb
roda-3.48.0 lib/roda/plugins/path_matchers.rb