Sha256: 18d84155ad84bae92c244d209a822832f3a386541eaa3c1f2718603ab11d91fc
Contents?: true
Size: 1.75 KB
Versions: 2
Compression:
Stored size: 1.75 KB
Contents
# -*- encoding : utf-8 -*- module Dao class Route < ::String Default = '/index'.freeze class << Route def default Default end def like?(route) route.to_s =~ %r{/:[^/]+} end def keys_for(route) route = Path.absolute_path_for(route.to_s) route.scan(%r{/:[^/]+}).map{|key| key.sub(%r{^/:}, '')} end def pattern_for(route) route = Path.absolute_path_for(route.to_s) re = route.gsub(%r{/:[^/]+}, '/([^/]+)') /#{ re }/ioux end def path_for(route, params = {}) path = Path.absolute_path_for(route.to_s) params = Map.new(params) params.each do |key, val| re = %r{/:#{ Regexp.escape(key.to_s) }(\Z|/)} repl = "/#{ val.to_s }\\1" path.gsub!(re, repl) end path end end attr_accessor :keys attr_accessor :pattern def initialize(path) replace(path.to_s) @keys = Route.keys_for(self).freeze @pattern = Route.pattern_for(self).freeze freeze end def path self end def path_for(params) Route.path_for(self, params) end def match(path) match = pattern.match(path).to_a end def params_for(path) match = pattern.match(path).to_a if match map = Map.new ignored = match.shift @keys.each_with_index do |key, index| map[key] = match[index] end map end end class List < ::Array def add(path) route = Route.new(path) push(route) route end def match(name) each do |route| match = route.match(name) return route if match end return nil end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
dao-4.4.4 | lib/dao/route.rb |
dao-4.4.2 | lib/dao/route.rb |