require 'mustermann/sinatra' module Padrino module PathRouter class Matcher # To count group of regexp GROUP_REGEXP = %r{\((?!\?:|\?!|\?<=|\? data.captures) unless data.captures.empty? else if mustermann? new_params = handler.params(pattern, :captures => data) params.merge!(new_params) if new_params elsif data params.merge!(Hash[names.zip(data.captures)]) end params.merge!(others){ |_, old, new| old || new } end params end ## # Returns the handler which is an instance of Mustermann or Regexp. # def handler @handler ||= case @path when String Mustermann.new(@path, :capture => @capture, :uri_decode => false) when Regexp /^(?:#{@path})$/ else @path end end ## # Converts the handler into string. # def to_s handler.to_s end ## # Returns names of the handler. # @see Regexp#names # def names handler.names end ## # Returns captures parameter length. # def capture_length if mustermann? handler.named_captures.inject(0) { |count, (_, capture)| count += capture.length } else handler.inspect.scan(GROUP_REGEXP).length end end private ## # Creates a hash with indifferent access. # def indifferent_hash Hash.new{ |hash, key| hash[key.to_s] if key.instance_of?(Symbol) } end end end end