5 SYMBOL_FIND = /:[a-z_]+/.freeze
6 PARENTHETICAL_SEGMENT_STRING = "([^\/.,;?]+)".freeze
8 attr_reader :regex, :params
10 def initialize(template)
11 @template = template.to_s.strip
12 @default_params = { :format => 'html' }
21 param_values = path.match(@regex).captures.compact rescue nil
24 keys = @keys.size < param_values.size ? @keys.concat([:format]) : @keys
25 @params = @default_params.merge(@keys.zip(param_values).to_hash)
35 @keys = @template.scan(SYMBOL_FIND).map { |raw| eval(raw) }
39 def genereate_route_without_format
40 template = @template.dup
41 template.gsub!(/\.:format$/, '')
42 to_regex_route(template)
45 def genereate_route_with_format
46 template = @template.dup
47 if template =~ /\.:format$|\.([\w\d]+)$/
48 @default_params[:format] = $1 if $1
50 template << '.:format'
52 to_regex_route(template)
55 def to_regex_route(template)
56 /^#{template.gsub(/\./, '\.').gsub(SYMBOL_FIND, PARENTHETICAL_SEGMENT_STRING)}$/
60 @regex = Regexp.union(genereate_route_without_format, genereate_route_with_format)