class Padrino::PathRouter::Matcher
Constants
- GROUP_REGEXP
To count group of regexp
Public Class Methods
new(path, options = {})
click to toggle source
Constructs an instance of PathRouter::Matcher
.
# File lib/padrino-core/path_router/matcher.rb, line 12 def initialize(path, options = {}) @path = path.is_a?(String) && path.empty? ? "/" : path @capture = options[:capture] @default_values = options[:default_values] end
Public Instance Methods
capture_length()
click to toggle source
Returns captures parameter length.
# File lib/padrino-core/path_router/matcher.rb, line 109 def capture_length if mustermann? handler.named_captures.inject(0) { |count, (_, capture)| count += capture.length } else handler.inspect.scan(GROUP_REGEXP).length end end
expand(params)
click to toggle source
Expands the path by using parameters.
# File lib/padrino-core/path_router/matcher.rb, line 39 def expand(params) params = @default_values.merge(params) if @default_values.is_a?(Hash) params, query = params.each_with_object([{}, {}]) do |(key, val), parts| parts[handler.names.include?(key.to_s) ? 0 : 1][key] = val end expanded_path = handler.expand(:append, params) expanded_path += ?? + Padrino::Utils.build_uri_query(query) unless query.empty? expanded_path end
handler()
click to toggle source
Returns the handler which is an instance of Mustermann or Regexp.
# File lib/padrino-core/path_router/matcher.rb, line 79 def handler @handler ||= case @path when String Mustermann.new(@path, :capture => @capture, :uri_decode => false) when Regexp /^(?:#{@path})$/ else @path end end
match(pattern)
click to toggle source
Matches a pattern with the route matcher.
# File lib/padrino-core/path_router/matcher.rb, line 21 def match(pattern) if match_data = handler.match(pattern) match_data elsif pattern != "/" && pattern.end_with?("/") handler.match(pattern[0..-2]) end end
mustermann?()
click to toggle source
Returns true if handler is an instance of Mustermann.
# File lib/padrino-core/path_router/matcher.rb, line 52 def mustermann? handler.instance_of?(Mustermann::Sinatra) end
names()
click to toggle source
Returns names of the handler. @see Regexp#names
# File lib/padrino-core/path_router/matcher.rb, line 102 def names handler.names end
params_for(pattern, others)
click to toggle source
Builds a parameters, and returns them.
# File lib/padrino-core/path_router/matcher.rb, line 59 def params_for(pattern, others) data = match(pattern) params = indifferent_hash if data.names.empty? params.merge!(:captures => 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
to_regexp()
click to toggle source
Returns a regexp from handler.
# File lib/padrino-core/path_router/matcher.rb, line 32 def to_regexp mustermann? ? handler.to_regexp : handler end
to_s()
click to toggle source
Converts the handler into string.
# File lib/padrino-core/path_router/matcher.rb, line 94 def to_s handler.to_s end
Private Instance Methods
indifferent_hash()
click to toggle source
Creates a hash with indifferent access.
# File lib/padrino-core/path_router/matcher.rb, line 122 def indifferent_hash Hash.new{ |hash, key| hash[key.to_s] if key.instance_of?(Symbol) } end