Sha256: 6d1c12fa2c893057c842f166a7049396da8f6e3b8c36e7b4da7829cd21ca177d

Contents?: true

Size: 1.52 KB

Versions: 16

Compression:

Stored size: 1.52 KB

Contents

# frozen-string-literal: true

#
class Roda
  module RodaPlugins
    # The optimized_string_matchers plugin adds two optimized matcher methods,
    # +r.on_branch+ and +r.is_exactly+.  +r.on_branch+ is an optimized version of
    # +r.on+ that only accepts a single string, and +r.is_exactly+ is an
    # optimized version of +r.is+ that only accepts a single string.
    #
    #   plugin :optimized_string_matchers
    #
    #   route do |r|
    #     r.on_branch "x" do
    #       # matches /x and paths starting with /x/
    #       r.is_exactly "y" do
    #         # matches /x/y
    #       end
    #     end
    #   end
    #
    # Note that both of these methods only work with plain strings, not
    # with strings with embedded colons for capturing.  Matching will work
    # correctly in such cases, but the captures will not be yielded to the
    # match blocks.
    module OptimizedStringMatchers
      EMPTY_STRING = ''.freeze

      module RequestMethods
        # Optimized version of +on+ that only supports a single string.
        def on_branch(s)
          always{yield} if _match_string(s)
        end

        # Optimized version of +is+ that only supports a single string.
        def is_exactly(s)
          rp = @remaining_path
          if _match_string(s)
            if @remaining_path == EMPTY_STRING
              always{yield}
            else
              @remaining_path = rp
            end
          end
        end
      end
    end

    register_plugin(:optimized_string_matchers, OptimizedStringMatchers)
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
roda-2.27.0 lib/roda/plugins/optimized_string_matchers.rb
roda-2.26.0 lib/roda/plugins/optimized_string_matchers.rb
roda-2.25.0 lib/roda/plugins/optimized_string_matchers.rb
roda-2.24.0 lib/roda/plugins/optimized_string_matchers.rb
roda-2.23.0 lib/roda/plugins/optimized_string_matchers.rb
roda-2.22.0 lib/roda/plugins/optimized_string_matchers.rb
roda-2.21.0 lib/roda/plugins/optimized_string_matchers.rb
roda-2.20.0 lib/roda/plugins/optimized_string_matchers.rb
roda-2.19.0 lib/roda/plugins/optimized_string_matchers.rb
roda-2.18.0 lib/roda/plugins/optimized_string_matchers.rb
roda-2.17.0 lib/roda/plugins/optimized_string_matchers.rb
roda-2.16.0 lib/roda/plugins/optimized_string_matchers.rb
roda-2.15.0 lib/roda/plugins/optimized_string_matchers.rb
roda-2.14.0 lib/roda/plugins/optimized_string_matchers.rb
roda-2.13.0 lib/roda/plugins/optimized_string_matchers.rb
roda-2.12.0 lib/roda/plugins/optimized_string_matchers.rb