Class: Perro::Route

Inherits:
Object
  • Object
show all
Defined in:
lib/perro/route.rb

Instance Method Summary collapse

Constructor Details

#initialize(r) ⇒ Route



3
4
5
6
# File 'lib/perro/route.rb', line 3

def initialize( r )
  @regexp = Regexp.new( Regexp.escape( r ).gsub(/:[\w]*/ , '(.*?)' )+"(\\?.*)?$" )
  @params_symbols = r.scan(/:(\w+)/).collect{ |m| m[0] }
end

Instance Method Details

#match(string) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'lib/perro/route.rb', line 8

def match( string )
  match = string.match( @regexp )
  return nil if match.nil?
  
  params = {}
  @params_symbols.each_index do |i|
    params[@params_symbols[i].to_sym] = match[i+1]
  end
  params
end