module Nyara # provide route preprocessing utils module Route; end class << Route # note that controller may be not defined yet def register_controller scope, controller unless scope.is_a?(String) raise ArgumentError, "route prefix should be a string" end scope = scope.dup.freeze (@controllers ||= {})[scope] = controller end def compile global_path_templates = {} # "name#id" => path @path_templates = {} # klass => {any_id => path} a = @controllers.map do |scope, c| if c.is_a?(String) c = name2const c end name = c.controller_name || const2name(c) raise "#{c.inspect} is not a Nyara::Controller" unless Controller > c if @path_templates[c] raise "controller #{c.inspect} was already mapped" end route_entries = c.preprocess_actions @path_templates[c] = {} route_entries.each do |e| id = e.id.to_s path = File.join scope, e.path global_path_templates[name + id] = path @path_templates[c][id] = path end [scope, c, route_entries] end @path_templates.keys.each do |c| @path_templates[c] = global_path_templates.merge @path_templates[c] end Ext.clear_route process(a).each do |entry| entry.validate Ext.register_route entry end end def path_template klass, id @path_templates[klass][id] end def clear # gc mark fail if wrong order? Ext.clear_route @controllers = {} @path_templates = {} end # private def const2name c name = c.to_s.sub /Controller$/, '' name.gsub!(/(?