Sha256: d3e11a0adf90ea990ea02fa2ccbb2359585414649878d2cc0c80db6521511c47

Contents?: true

Size: 1.77 KB

Versions: 42

Compression:

Stored size: 1.77 KB

Contents

# frozen-string-literal: true

#
class Roda
  module RodaPlugins
    # The optimized_segment_matchers plugin adds two optimized matcher methods,
    # +r.on_segment+ and +r.is_segment+.  +r.on_segment+ is an optimized version of
    # +r.on String+ that accepts no arguments and yields the next segment if there
    # is a segment. +r.is_segment+ is an optimized version of +r.is String+ that accepts
    # no arguments and yields the next segment only if it is the last segment.
    #
    #   plugin :optimized_segment_matchers
    #
    #   route do |r|
    #     r.on_segment do |x|
    #       # matches any segment (e.g. /a, /b, but not /)
    #       r.is_segment do |y|
    #         # matches only if final segment (e.g. /a/b, /b/c, but not /c, /c/d/, /c/d/e)
    #       end
    #     end
    #   end
    module OptimizedSegmentMatchers
      module RequestMethods
        # Optimized version of +r.on String+ that yields the next segment if there
        # is a segment.
        def on_segment
          rp = @remaining_path
          if rp.getbyte(0) == 47
            if last = rp.index('/', 1)
              @remaining_path = rp[last, rp.length]
              always{yield rp[1, last-1]}
            elsif (len = rp.length) > 1
              @remaining_path = ""
              always{yield rp[1, len]}
            end
          end
        end

        # Optimized version of +r.is String+ that yields the next segment only if it
        # is the final segment.
        def is_segment
          rp = @remaining_path
          if rp.getbyte(0) == 47 && !rp.index('/', 1) && (len = rp.length) > 1
            @remaining_path = ""
            always{yield rp[1, len]}
          end
        end
      end
    end

    register_plugin(:optimized_segment_matchers, OptimizedSegmentMatchers)
  end
end

Version data entries

42 entries across 42 versions & 1 rubygems

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