Sha256: 4a28d84c14ba9878164f6b4f6d0e755505e414ad1563cf2f2c63baf2d8e71abd

Contents?: true

Size: 1.54 KB

Versions: 42

Compression:

Stored size: 1.54 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
    #
    # If you are using the placeholder_string_matchers plugin, 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
      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?
              always{yield}
            else
              @remaining_path = rp
            end
          end
        end
      end
    end

    register_plugin(:optimized_string_matchers, OptimizedStringMatchers)
  end
end

Version data entries

42 entries across 42 versions & 1 rubygems

Version Path
roda-3.86.0 lib/roda/plugins/optimized_string_matchers.rb
roda-3.85.0 lib/roda/plugins/optimized_string_matchers.rb
roda-3.84.0 lib/roda/plugins/optimized_string_matchers.rb
roda-3.83.0 lib/roda/plugins/optimized_string_matchers.rb
roda-3.82.0 lib/roda/plugins/optimized_string_matchers.rb
roda-3.81.0 lib/roda/plugins/optimized_string_matchers.rb
roda-3.79.0 lib/roda/plugins/optimized_string_matchers.rb
roda-3.78.0 lib/roda/plugins/optimized_string_matchers.rb
roda-3.77.0 lib/roda/plugins/optimized_string_matchers.rb
roda-3.76.0 lib/roda/plugins/optimized_string_matchers.rb
roda-3.75.0 lib/roda/plugins/optimized_string_matchers.rb
roda-3.74.0 lib/roda/plugins/optimized_string_matchers.rb
roda-3.73.0 lib/roda/plugins/optimized_string_matchers.rb
roda-3.72.0 lib/roda/plugins/optimized_string_matchers.rb
roda-3.71.0 lib/roda/plugins/optimized_string_matchers.rb
roda-3.70.0 lib/roda/plugins/optimized_string_matchers.rb
roda-3.69.0 lib/roda/plugins/optimized_string_matchers.rb
roda-3.68.0 lib/roda/plugins/optimized_string_matchers.rb
roda-3.67.0 lib/roda/plugins/optimized_string_matchers.rb
roda-3.66.0 lib/roda/plugins/optimized_string_matchers.rb