| 31 | | @@matcher.route_request(path) |
|---|
| 32 | | end |
|---|
| 33 | | |
|---|
| 34 | | def generate(method, *args) |
|---|
| 35 | | @@generator.generate(method, *args) |
|---|
| 36 | | end |
|---|
| 37 | | |
|---|
| 38 | | def resources(res, opts={}) |
|---|
| 39 | | opts[:prefix] ||= "" |
|---|
| 40 | | if block_given? |
|---|
| 41 | | procs = [] |
|---|
| 42 | | yield Resource.new(res, procs, opts) |
|---|
| 43 | | procs.reverse.each &:call |
|---|
| 44 | | else |
|---|
| 45 | | generate_resources_routes(res,opts) |
|---|
| 46 | | end |
|---|
| 47 | | end |
|---|
| 48 | | |
|---|
| 49 | | def resource(res, opts={}) |
|---|
| 50 | | opts[:prefix] ||= "" |
|---|
| 51 | | if block_given? |
|---|
| 52 | | procs = [] |
|---|
| 53 | | yield Resource.new(res, procs, opts) |
|---|
| 54 | | procs.reverse.each &:call |
|---|
| 55 | | else |
|---|
| 56 | | generate_singleton_routes(res,opts) |
|---|
| 57 | | end |
|---|
| 58 | | end |
|---|
| 59 | | |
|---|
| 60 | | def default_routes(*a) |
|---|
| 61 | | @@matcher.default_routes(*a) |
|---|
| 62 | | end |
|---|
| 63 | | |
|---|
| 64 | | def compiled_statement |
|---|
| 65 | | @@matcher.compiled_statement |
|---|
| 66 | | end |
|---|
| 67 | | |
|---|
| 68 | | def compiled_regexen |
|---|
| 69 | | @@matcher.compiled_regexen |
|---|
| 70 | | end |
|---|
| 71 | | |
|---|
| 72 | | def generate_resources_routes(res, opts) |
|---|
| 73 | | [@@matcher,@@generator].each { |r| r.generate_resources_routes(res, opts) } |
|---|
| 74 | | end |
|---|
| 75 | | |
|---|
| 76 | | def generate_singleton_routes(res, opts) |
|---|
| 77 | | [@@matcher,@@generator].each { |r| r.generate_singleton_routes(res, opts) } |
|---|
| 78 | | end |
|---|
| 79 | | |
|---|
| 80 | | end # class << self |
|---|
| 81 | | |
|---|
| 82 | | class RouteMatcher |
|---|
| 83 | | attr_reader :routes, :compiled_statement, :compiled_regexen |
|---|
| 84 | | |
|---|
| 85 | | def initialize |
|---|
| 86 | | @routes = Array.new |
|---|
| 87 | | # The final compiled lambda that gets used as the body of the route_request method. |
|---|
| 88 | | @compiled_statement = String.new |
|---|
| 89 | | @compiled_regexen = Array.new |
|---|
| 90 | | end |
|---|
| 91 | | |
|---|
| 92 | | # Add a route to be compiled. |
|---|
| 93 | | def add(*route) |
|---|
| 94 | | opt = Hash === route.last ? route.pop : {} |
|---|
| 95 | | if n = opt[:namespace] |
|---|
| 96 | | path = "/#{n}#{route[0]}" |
|---|
| 97 | | else |
|---|
| 98 | | path = route[0] |
|---|
| 99 | | end |
|---|
| 100 | | @routes << [path, opt] |
|---|
| 101 | | end |
|---|
| 102 | | |
|---|
| 103 | | def raw_add(*route) |
|---|
| 104 | | @routes << [route[0], (route[1]||{})] |
|---|
| 105 | | end |
|---|
| 106 | | |
|---|
| 107 | | # Build up a string that defines a lambda that does a case statement on |
|---|
| 108 | | # the PATH_INFO against each of the compiled routes in turn. First route |
|---|
| 109 | | # that matches wins. |
|---|
| 110 | | def compile_router |
|---|
| 111 | | router_lambda = @routes.inject("lambda{|path| \n sections={}\n case path\n") { |m,r| |
|---|
| 112 | | m << compile(r) |
|---|
| 113 | | } << " else\n return {:controller=>'Noroutefound', :action=>'noroute'}\n end\n}" |
|---|
| 114 | | @compiled_statement = router_lambda |
|---|
| 115 | | meta_def(:route_request, &eval(router_lambda)) |
|---|
| 116 | | end |
|---|
| 117 | | |
|---|
| 118 | | # Compile each individual route into a when /.../ component of the case |
|---|
| 119 | | # statement. Takes /:sections of the route def that start with : and |
|---|
| 120 | | # turns them into placeholders for whatever urls match against the route |
|---|
| 121 | | # in question. |
|---|
| 122 | | def compile(route) |
|---|
| 123 | | raise ArgumentError unless String === route[0] |
|---|
| 124 | | code, count = '', 0 |
|---|
| 125 | | while route[0] =~ Router::SECTION_REGEXP |
|---|
| 126 | | route[0] = route[0].dup |
|---|
| 127 | | name = $1 |
|---|
| 128 | | (name =~ /(\*+)(\w+)/) ? (flag = true; name = $2) : (flag = false) |
|---|
| 129 | | count += 1 |
|---|
| 130 | | if flag |
|---|
| 131 | | route[0].sub!(Router::SECTION_REGEXP, "([^,?]+)") |
|---|
| 132 | | else |
|---|
| 133 | | route[0].sub!(Router::SECTION_REGEXP, "([^\/,?]+)") |
|---|
| 134 | | end |
|---|
| 135 | | code << " sections[:#{name}] = $#{count}\n" |
|---|
| 136 | | end |
|---|
| 137 | | @compiled_regexen << Regexp.new(route[0]) |
|---|
| 138 | | index = @compiled_regexen.size - 1 |
|---|
| 139 | | condition = " when @compiled_regexen[#{index}] " |
|---|
| 140 | | statement = "#{condition}\n#{code}" |
|---|
| 141 | | statement << " return #{route[1].inspect}.update(sections)\n" |
|---|
| 142 | | statement |
|---|
| 143 | | end |
|---|
| 144 | | |
|---|
| 145 | | def generate_resources_routes(res,opt) |
|---|
| 146 | | with_options opt.merge(:controller => res.to_s, :rest => true) do |r| |
|---|
| 147 | | r.raw_add "#{opt[:prefix]}/#{res}/:id[;/]edit", :allowed => {:get => 'edit'} |
|---|
| 148 | | r.raw_add "#{opt[:prefix]}/#{res}/new[;/]:action", :allowed => {:get => 'new', :post => 'new', :put => 'new', :delete => 'new'} |
|---|
| 149 | | r.raw_add "#{opt[:prefix]}/#{res}/new" , :allowed => {:get => 'new'} |
|---|
| 150 | | if mem = opt[:member] |
|---|
| 151 | | mem.keys.sort_by{|x| "#{x}"}.each {|action| |
|---|
| 152 | | allowed = mem[action].injecting({}) {|h, verb| h[verb] = "#{action}"} |
|---|
| 153 | | r.raw_add "#{opt[:prefix]}/#{res}/:id[;/]+#{action}", :allowed => allowed |
|---|
| 154 | | } |
|---|
| 155 | | end |
|---|
| 156 | | if coll = opt[:collection] |
|---|
| 157 | | coll.keys.sort_by{|x| "#{x}"}.each {|action| |
|---|
| 158 | | allowed = coll[action].injecting({}) {|h, verb| h[verb] = "#{action}"} |
|---|
| 159 | | r.raw_add "#{opt[:prefix]}/#{res}[;/]#{action}", :allowed => allowed |
|---|
| 160 | | } |
|---|
| 161 | | end |
|---|
| 162 | | r.raw_add "#{opt[:prefix]}/#{res}/:id\\.:format", :allowed => {:get => 'show', :put => 'update', :delete => 'destroy'} |
|---|
| 163 | | r.raw_add "#{opt[:prefix]}/#{res}\\.:format", :allowed => {:get => 'index', :post => 'create'} |
|---|
| 164 | | r.raw_add "#{opt[:prefix]}/#{res}/:id", :allowed => {:get => 'show', :put => 'update', :delete => 'destroy'} |
|---|
| 165 | | r.raw_add "#{opt[:prefix]}/#{res}/?", :allowed => {:get => 'index', :post => 'create'} |
|---|
| 166 | | end |
|---|
| 167 | | end |
|---|
| 168 | | |
|---|
| 169 | | def generate_singleton_routes(res,opt) |
|---|
| 170 | | with_options opt.merge(:controller => res.to_s, :rest => true ) do |r| |
|---|
| 171 | | r.raw_add "#{opt[:prefix]}/#{res}[;/]edit", :allowed => {:get => 'edit'} |
|---|
| 172 | | r.raw_add "#{opt[:prefix]}/#{res}\\.:format", :allowed => {:get => 'show'} |
|---|
| 173 | | r.raw_add "#{opt[:prefix]}/#{res}/new" , :allowed => {:get => 'new'} |
|---|
| 174 | | r.raw_add "#{opt[:prefix]}/#{res}/?", :allowed => {:get => 'show', :post => 'create', :put => 'update', :delete => 'destroy'} |
|---|
| 175 | | end |
|---|
| 176 | | end |
|---|
| 177 | | |
|---|
| 178 | | def default_routes(opt={}) |
|---|
| 179 | | namespace = opt[:namespace] ? "/#{opt[:namespace]}" : "" |
|---|
| 180 | | with_options opt do |r| |
|---|
| 181 | | r.raw_add namespace + "/:controller/:action/:id\\.:format" |
|---|
| 182 | | r.raw_add namespace + "/:controller/:action/:id" |
|---|
| 183 | | r.raw_add namespace + "/:controller/:action\\.:format" |
|---|
| 184 | | r.raw_add namespace + "/:controller/:action" |
|---|
| 185 | | r.raw_add namespace + "/:controller\\.:format", :action => 'index' |
|---|
| 186 | | r.raw_add namespace + "/:controller", :action => 'index' |
|---|
| 187 | | end |
|---|
| 188 | | end |
|---|
| 189 | | end |
|---|
| 190 | | |
|---|
| 191 | | class RouteGenerator |
|---|
| 192 | | |
|---|
| 193 | | attr_accessor :paths |
|---|
| 194 | | |
|---|
| 195 | | def initialize |
|---|
| 196 | | @paths = {} |
|---|
| 197 | | end |
|---|
| 198 | | |
|---|
| 199 | | def add(name, path) |
|---|
| 200 | | @paths[name.to_sym] = path |
|---|
| 201 | | end |
|---|
| 202 | | |
|---|
| 203 | | def generate(name, *args) |
|---|
| 204 | | options = Hash === args.last ? args.pop : {} |
|---|
| 205 | | obj = args[0] |
|---|
| 206 | | options.each do |key, value| |
|---|
| 207 | | next unless value.respond_to?(:to_param) |
|---|
| 208 | | unless key.to_s =~ /_?id$/ |
|---|
| 209 | | old_key = key |
|---|
| 210 | | options[old_key] = value.to_param |
|---|
| 211 | | key = "#{key}_id".intern |
|---|
| 212 | | end |
|---|
| 213 | | options[key] = value.to_param |
|---|
| 214 | | end |
|---|
| 215 | | |
|---|
| 216 | | path = @paths[name].dup |
|---|
| 217 | | while path =~ Router::SECTION_REGEXP |
|---|
| 218 | | if obj.respond_to?($1) && ! obj.nil? |
|---|
| 219 | | path.sub!(Router::SECTION_REGEXP, obj.send($1).to_s) |
|---|
| 220 | | else |
|---|
| 221 | | path.sub!(Router::SECTION_REGEXP, options[$1.intern].to_s) |
|---|
| 222 | | end |
|---|
| 223 | | end |
|---|
| 224 | | if f = options[:format] |
|---|
| 225 | | "#{path}.#{f}" |
|---|
| 226 | | else |
|---|
| 227 | | path |
|---|
| 228 | | end |
|---|
| 229 | | end |
|---|
| 230 | | |
|---|
| 231 | | def generate_singleton_routes(res,opt) |
|---|
| 232 | | res = res.to_s |
|---|
| 233 | | if opt[:namespace] |
|---|
| 234 | | namespace = "#{opt[:namespace]}_" |
|---|
| 235 | | name = "/#{opt[:namespace]}" |
|---|
| 236 | | else |
|---|
| 237 | | namespace = '' |
|---|
| 238 | | name = '' |
|---|
| 239 | | end |
|---|
| 240 | | add namespace + "edit_#{res}", name + "#{opt[:prefix]}/#{res}/edit" |
|---|
| 241 | | add namespace + "new_#{res}", name + "#{opt[:prefix]}/#{res}/new" |
|---|
| 242 | | add namespace + res, name + "#{opt[:prefix]}/#{res}" |
|---|
| 243 | | end |
|---|
| 244 | | |
|---|
| 245 | | def generate_resources_routes(res,opt) |
|---|
| 246 | | res = res.to_s |
|---|
| 247 | | res_singular = res.singularize |
|---|
| 248 | | if opt[:namespace] |
|---|
| 249 | | namespace = "#{opt[:namespace]}_" |
|---|
| 250 | | name = "/#{opt[:namespace]}" |
|---|
| 251 | | else |
|---|
| 252 | | namespace = '' |
|---|
| 253 | | name = '' |
|---|
| 254 | | end |
|---|
| 255 | | add namespace + res, name + "#{opt[:prefix]}/#{res}" |
|---|
| 256 | | add namespace + res_singular, name + "#{opt[:prefix]}/#{res}/:id" |
|---|
| 257 | | add namespace + "new_#{res_singular}", name + "#{opt[:prefix]}/#{res}/new" |
|---|
| 258 | | add namespace + "custom_new_#{res_singular}", name + "#{opt[:prefix]}/#{res}/new/:action" |
|---|
| 259 | | add namespace + "edit_#{res_singular}", name + "#{opt[:prefix]}/#{res}/:id/edit" |
|---|
| 260 | | if mem = opt[:member] |
|---|
| 261 | | mem.keys.sort_by{|x| "#{x}"}.each {|action| |
|---|
| 262 | | add namespace + "#{action}_#{res_singular}", name + "#{opt[:prefix]}/#{res}/:id/#{action}" |
|---|
| 263 | | } |
|---|
| 264 | | end |
|---|
| 265 | | if coll = opt[:collection] |
|---|
| 266 | | coll.keys.sort_by{|x| "#{x}"}.each {|action| |
|---|
| 267 | | add namespace + "#{action}_#{res_singular}", name + "#{opt[:prefix]}/#{res}/#{action}" |
|---|
| 268 | | } |
|---|
| 269 | | end |
|---|
| 270 | | end |
|---|
| 271 | | end |
|---|
| 272 | | |
|---|
| 273 | | class Resource |
|---|
| 274 | | |
|---|
| 275 | | def initialize(resource, procs=[], opts={}) |
|---|
| 276 | | @resource, @procs, @opts = resource, procs, opts |
|---|
| 277 | | @procs << proc do |
|---|
| 278 | | [::Merb::Router.matcher, ::Merb::Router.generator].each {|r| r.generate_resources_routes(@resource, @opts) } |
|---|
| 279 | | end |
|---|
| 280 | | end |
|---|
| 281 | | |
|---|
| 282 | | def resources(res, opts={}) |
|---|
| 283 | | (opts[:prefix]||='') << "/#{@resource}/:#{@resource.to_s.singularize}_id" |
|---|
| 284 | | |
|---|
| 285 | | opts[:prefix] = @opts[:prefix] + opts[:prefix] |
|---|
| 286 | | if block_given? |
|---|
| 287 | | yield self.class.new(res, @procs, opts) |
|---|
| 288 | | else |
|---|
| 289 | | @procs << proc do |
|---|
| 290 | | [::Merb::Router.matcher, ::Merb::Router.generator].each {|r| r.generate_resources_routes(res, opts) } |
|---|
| 291 | | end |
|---|
| 292 | | end |
|---|
| 293 | | end |
|---|
| 294 | | |
|---|
| 295 | | def resource(res, opts={}) |
|---|
| 296 | | (opts[:prefix]||='') << "/#{@resource}/:#{@resource.to_s.singularize}_id" |
|---|
| 297 | | |
|---|
| 298 | | opts[:prefix] = @opts[:prefix] + opts[:prefix] |
|---|
| 299 | | if block_given? |
|---|
| 300 | | yield self.class.new(res, @procs, opts) |
|---|
| 301 | | else |
|---|
| 302 | | @procs << proc do |
|---|
| 303 | | [::Merb::Router.matcher, ::Merb::Router.generator].each { |r| r.generate_singleton_routes(res, opts) } |
|---|
| 304 | | end |
|---|
| | 23 | # this can be optimized a lot. |
|---|
| | 24 | sections = path.split('/') |
|---|
| | 25 | sections.length.times do |
|---|
| | 26 | j = sections.join('/') |
|---|
| | 27 | return table[j] if table.has_key?(j) |
|---|
| | 28 | sections.pops |
|---|