Class: Doozer::Routing::Routes
Overview
Route manager for drawing and adding routes.
Constant Summary collapse
- @@parts =
stored as [route.name, route.path]
[]
- @@dict =
route hash
{}
- @@cache =
lookup for matches
{}
- @@magics =
hold raw magic routes before processing
[]
- @@inner_apps =
holds the app dedicated to processing this path
{}
Class Method Summary collapse
-
.add(name = nil, path = nil, args = nil) ⇒ Object
An empty path defaults to a path of ‘/’.
-
.cache_request_path(route, path) ⇒ Object
caches the request path and with the route.name.
- .draw(&block) ⇒ Object
-
.get_by_name(name) ⇒ Object
return a route by name.
-
.init_formats(args) ⇒ Object
sets up default formats to initialize a mapped route.
- .init_magic_routes ⇒ Object
- .init_view_helpers ⇒ Object
- .magic(route) ⇒ Object
-
.match(path) ⇒ Object
return the route which matches the request path.
Class Method Details
.add(name = nil, path = nil, args = nil) ⇒ Object
An empty path defaults to a path of ‘/’
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/doozer/route.rb', line 34 def self.add(name=nil, path=nil, args=nil) # p name # p path # p args.inspect if not name.nil? and not path.nil? and not args.nil? args = Routes::init_formats(args) formats = args[:formats] # p formats.inspect for format in formats args.delete(:formats) if name != :magic path = '/' if path == '' raise Doozer::Exceptions::Route.new("Route name must be a symbol. #{name} given.") if not name.kind_of? Symbol raise Doozer::Exceptions::Route.new("Route already exists with the name of #{name}.") if @@dict[name] @@parts.each { |p| raise Doozer::Exceptions::Route.new("Route already defined with a path of '#{path}'") if p[1] == path } parts = [name, path, args] # p parts.inspect args[:format] = format route = Doozer::Routing::Route.new(parts) # p route.inspect @@parts.push([route.name, route.path]) @@dict[route.name] = route else p "magic routes init turned off" # Routes.magic(parts) end end end end |
.cache_request_path(route, path) ⇒ Object
caches the request path and with the route.name
97 98 99 100 |
# File 'lib/doozer/route.rb', line 97 def self.cache_request_path(route,path) # p "route cache request path" @@cache[path] = route.name end |
.draw(&block) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/doozer/route.rb', line 16 def self.draw(&block) # p "draw routes" instance_eval(&block) if block_given? # init magic routes :conrtoller/:action or just /:action with predefined :controller # Routes.init_magic_routes Routes.init_view_helpers # sort routes here @@parts.sort! do |a, b| a[1].length <=> b[1].length end @@parts.reverse! puts "=> Routes drawn and sorted" # @@parts.each { | i | p i[1] } end |
.get_by_name(name) ⇒ Object
return a route by name
75 76 77 78 |
# File 'lib/doozer/route.rb', line 75 def self.get_by_name(name) # p @@dict.inspect return @@dict[name] end |
.init_formats(args) ⇒ Object
sets up default formats to initialize a mapped route
66 67 68 69 70 71 72 |
# File 'lib/doozer/route.rb', line 66 def self.init_formats(args) formats = args[:formats] formats = [] if formats.nil? formats.push(:html) if not formats.include?(:html) args[:formats] = formats return args end |
.init_magic_routes ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/doozer/route.rb', line 106 def self.init_magic_routes @@controllers={} controller_files = Dir.glob(File.join(File.dirname(__FILE__),'../app/controllers/*_controller.rb')) if controller_files.length > 0 i=0 for f in controller_files break if i==0 and f.index('application_controller.rb') if f.index('application_controller.rb') controller_files.insert(0, controller_files.delete(f)) break end i+=1 end end controller_files.each {|f| require f key = f.split("controllers/")[1].split("_controller.rb")[0] if key.index("_") value = key.split('_').each{ | k | k.capitalize! }.join('') else value = key.capitalize end @@controllers[key.to_sym] = "#{value}Controller" # p "cache controller: #{key.to_sym}" } # p @@controllers.inspect # grab all controllers routes = [] dup_lu = {} obj = Doozer::Controller obj.public_instance_methods.each { | name | dup_lu[name]=''} # p dup_lu.inspect @@magics.each { | route | path = route[1] if path.index(':controller') and path.index(':action') ## loop all controller and then loop all #methods @@controllers.each{ |key,value | klass = Object.const_get(value) methods = klass.public_instance_methods() methods.push('index') methods.uniq! # filter duplicate indexes methods.each { | val | if dup_lu[val].nil? controller= route[2][:controller] || key.to_s action = route[2][:action] || val # p "#{controller}##{action}" name = "#{controller}_#{action}".to_sym new_path = path.gsub(/:controller/, controller).gsub(/:action/,action) new_path = new_path.gsub(/\/index/) if new_path.endswith('/index') new_path = "/#{new_path}" if not new_path =~ /^\// add([name, new_path, {:controller=>controller, :action=>action, :status=>200, :formats=>route[2][:formats]}]) end } } elsif path.index(':action') and not route[2][:controller].nil? ## loop all methods on this controller #p "load route controller:" + @@controllers[route[2][:controller].to_sym].inspect controller= route[2][:controller] klass = Object.const_get(@@controllers[controller.to_sym]) methods = klass.public_instance_methods() methods.push('index') methods.uniq! # filter duplicate indexes methods.each { | val | if dup_lu[val].nil? action = val # p "#{controller}##{action}" name = "#{controller}_#{action}".to_sym new_path = path.gsub(/:action/,action) new_path = new_path.gsub(/\/index/,'') if new_path =~ /\/index/ new_path = "/#{new_path}" if not new_path =~ /^\// # p [name, new_path, {:controller=>controller, :action=>action, :status=>200}].inspect add([name, new_path, {:controller=>controller, :action=>action, :status=>200, :formats=>route[2][:formats]}]) end } end } ## make sure to route index to '/' ## loop route/methods pairs # save new path for action controller end |
.init_view_helpers ⇒ Object
193 194 195 196 197 |
# File 'lib/doozer/route.rb', line 193 def self.init_view_helpers for k, route in @@dict Doozer::ViewHelpers.module_eval(route.url_helper_method) end end |
.magic(route) ⇒ Object
102 103 104 |
# File 'lib/doozer/route.rb', line 102 def self.magic(route) @@magics.push(route) end |
.match(path) ⇒ Object
return the route which matches the request path
81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/doozer/route.rb', line 81 def self.match(path) # p path # p @@cache.inspect # return @@dict[@@cache[path]] if @@cache[path] for part in @@parts route = @@dict[part[0]] # p route.inspect if route.match(path) # Routes.cache_request_path(route, path) return route end end return nil end |