Class: Padrino::PathRouter::Matcher

Inherits:
Object
  • Object
show all
Defined in:
lib/padrino-core/path_router/matcher.rb

Constant Summary collapse

GROUP_REGEXP =

To count group of regexp

%r{\((?!\?:|\?!|\?<=|\?<!|\?=).+?\)}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(path, options = {}) ⇒ Matcher

Constructs an instance of PathRouter::Matcher.



12
13
14
15
16
# 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

Instance Method Details

#capture_lengthObject

Returns captures parameter length.



109
110
111
112
113
114
115
# 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) ⇒ Object

Expands the path by using parameters.



39
40
41
42
43
44
45
46
47
# 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

#handlerObject

Returns the handler which is an instance of Mustermann or Regexp.



79
80
81
82
83
84
85
86
87
88
89
# 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) ⇒ Object

Matches a pattern with the route matcher.



21
22
23
24
25
26
27
# 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?Boolean

Returns true if handler is an instance of Mustermann.

Returns:

  • (Boolean)


52
53
54
# File 'lib/padrino-core/path_router/matcher.rb', line 52

def mustermann?
  handler.instance_of?(Mustermann::Sinatra)
end

#namesObject

Returns names of the handler.

See Also:

  • Regexp#names


102
103
104
# File 'lib/padrino-core/path_router/matcher.rb', line 102

def names
  handler.names
end

#params_for(pattern, others) ⇒ Object

Builds a parameters, and returns them.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# 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_regexpObject

Returns a regexp from handler.



32
33
34
# File 'lib/padrino-core/path_router/matcher.rb', line 32

def to_regexp
  mustermann? ? handler.to_regexp : handler
end

#to_sObject

Converts the handler into string.



94
95
96
# File 'lib/padrino-core/path_router/matcher.rb', line 94

def to_s
  handler.to_s
end