Sha256: 139c44081494746c58e3b6c6a16c86435862ef820dff78f02cfc3e23481a940d

Contents?: true

Size: 1.89 KB

Versions: 13

Compression:

Stored size: 1.89 KB

Contents

class Roda
  module RodaPlugins
    # The match_affix plugin allows changing the default prefix and suffix used for
    # match patterns.  Roda's default behavior for a match pattern like <tt>"albums"</tt>
    # is to use the pattern <tt>/\A\/(?:albums)(?=\/|\z)/</tt>.  This prefixes the pattern
    # with +/+ and suffixes it with <tt>(?=\/|\z)</tt>.  With the match_affix plugin, you
    # can change the prefix and suffix to use.  So if you want to be explicit and require
    # a leading +/+ in patterns, you can set the prefix to <tt>""</tt>.  If you want to
    # consume a trailing slash instead of leaving it, you can set the suffix to <tt>(\/|\z)</tt>.
    #
    # You set the prefix and suffix to use by passing arguments when loading the plugin:
    #
    #   plugin :match_affix, ""
    #
    # will load the plugin and use an empty prefix.
    #
    #   plugin :match_affix, "", /(\/|\z)/
    #
    # will use an empty prefix and change the suffix to consume a trailing slash.
    #
    #  plugin :match_affix, nil, /(\/|\z)/
    #
    # will not modify the prefix and will change the suffix to consume a trailing slash.
    module MatchAffix
      PREFIX = "/".freeze
      SUFFIX = "(?=\/|\z)".freeze

      # Set the default prefix and suffix to use in match patterns, if a non-nil value
      # is given.
      def self.configure(app, prefix, suffix=nil)
        app.opts[:match_prefix] = prefix if prefix
        app.opts[:match_suffix] = suffix if suffix
      end
      
      module RequestClassMethods
        private

        # Use the match prefix and suffix provided when loading the plugin, or fallback
        # to Roda's default prefix/suffix if one was not provided.
        def consume_pattern(pattern)
          /\A#{roda_class.opts[:match_prefix] || PREFIX}(?:#{pattern})#{roda_class.opts[:match_suffix] || SUFFIX}/
        end
      end
    end

    register_plugin(:match_affix, MatchAffix)
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
roda-2.9.0 lib/roda/plugins/match_affix.rb
roda-2.8.0 lib/roda/plugins/match_affix.rb
roda-2.7.0 lib/roda/plugins/match_affix.rb
roda-2.6.0 lib/roda/plugins/match_affix.rb
roda-2.5.1 lib/roda/plugins/match_affix.rb
roda-2.5.0 lib/roda/plugins/match_affix.rb
roda-2.4.0 lib/roda/plugins/match_affix.rb
roda-2.3.0 lib/roda/plugins/match_affix.rb
roda-2.2.0 lib/roda/plugins/match_affix.rb
roda-2.1.0 lib/roda/plugins/match_affix.rb
roda-2.0.0 lib/roda/plugins/match_affix.rb
roda-1.3.0 lib/roda/plugins/match_affix.rb
roda-1.2.0 lib/roda/plugins/match_affix.rb