Class: Mack::Routes::RouteMap
- Includes:
- Singleton
- Defined in:
- lib/mack/routing/route_map.rb
Overview
See Mack::Routes for more information.
Instance Method Summary collapse
-
#any? ⇒ Boolean
:nodoc:.
-
#build_resource_routes(method_base, path_base, controller, options) ⇒ Object
:nodoc:.
-
#connect(path, options = {}, &block) ⇒ Object
Connects a url pattern to a controller, an action, and an HTTP verb.
-
#connect_with_name(name, path, options = {}, &block) ⇒ Object
:nodoc:.
-
#default_routes_list ⇒ Object
:nodoc:.
-
#defaults ⇒ Object
Creates ‘Rails’ style default mappings: “/:controller/:action/:id” “/:controller/:action” These get created for each of the 4 HTTP verbs.
-
#empty? ⇒ Boolean
:nodoc:.
- #handle_error(error, options) ⇒ Object
-
#initialize ⇒ RouteMap
constructor
:nodoc:.
-
#inspect ⇒ Object
:nodoc:.
-
#method_missing(sym, *args, &block) ⇒ Object
:nodoc:.
-
#reset! ⇒ Object
:nodoc:.
-
#resource(controller, options = {}, &block) ⇒ Object
Sets up mappings and named routes for a resource.
-
#retrieve(url_or_request, verb = :get) ⇒ Object
:nodoc:.
-
#retrieve_from_error(error) ⇒ Object
:nodoc:.
-
#routes_list ⇒ Object
:nodoc:.
Constructor Details
#initialize ⇒ RouteMap
:nodoc:
9 10 11 |
# File 'lib/mack/routing/route_map.rb', line 9 def initialize # :nodoc: reset! end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args, &block) ⇒ Object
:nodoc:
87 88 89 |
# File 'lib/mack/routing/route_map.rb', line 87 def method_missing(sym, *args, &block) # :nodoc: connect_with_name(sym, *args, &block) end |
Instance Method Details
#any? ⇒ Boolean
:nodoc:
18 19 20 21 22 23 |
# File 'lib/mack/routing/route_map.rb', line 18 def any? # :nodoc: @_route_map.each do |k, v| return true if v.any? end return false end |
#build_resource_routes(method_base, path_base, controller, options) ⇒ Object
:nodoc:
153 154 155 156 157 158 159 160 161 |
# File 'lib/mack/routing/route_map.rb', line 153 def build_resource_routes(method_base, path_base, controller, ) # :nodoc: connect_with_name("#{method_base}_index", "/#{path_base}", {:controller => controller, :action => :index, :method => :get}.merge()) connect_with_name("#{method_base}_create", "/#{path_base}", {:controller => controller, :action => :create, :method => :post}.merge()) connect_with_name("#{method_base}_new", "/#{path_base}/new", {:controller => controller, :action => :new, :method => :get}.merge()) connect_with_name("#{method_base}_show", "/#{path_base}/:id", {:controller => controller, :action => :show, :method => :get}.merge()) connect_with_name("#{method_base}_edit", "/#{path_base}/:id/edit", {:controller => controller, :action => :edit, :method => :get}.merge()) connect_with_name("#{method_base}_update", "/#{path_base}/:id", {:controller => controller, :action => :update, :method => :put}.merge()) connect_with_name("#{method_base}_delete", "/#{path_base}/:id", {:controller => controller, :action => :delete, :method => :delete}.merge()) end |
#connect(path, options = {}, &block) ⇒ Object
Connects a url pattern to a controller, an action, and an HTTP verb.
77 78 79 80 81 82 83 84 85 |
# File 'lib/mack/routing/route_map.rb', line 77 def connect(path, = {}, &block) = (, &block) if path.is_a?(String) path = "/#{path}" unless path.match(/^\//) end route = RouteObject.new(path, ) @_route_map[[:method]] << route route end |
#connect_with_name(name, path, options = {}, &block) ⇒ Object
:nodoc:
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/mack/routing/route_map.rb', line 133 def connect_with_name(name, path, = {}, &block) # :nodoc: n_route = name.methodize route = connect(path, {:action => n_route.to_sym}.merge(), &block) Mack::Routes::Urls.create_method("#{n_route}_url") do |*| = * = {} if .nil? || !.is_a?(Hash) url_for_pattern(route.path, (route..reject{|k,v| k.to_sym == :action || k.to_sym == :controller || k.to_sym == :method}).merge()) end Mack::Routes::Urls.create_method("#{n_route}_full_url") do |*| = * = {} if .nil? || !.is_a?(Hash) if @request = {:host => @request.host, :scheme => @request.scheme, :port => @request.port}.merge() end self.send("#{n_route}_url", ) end end |
#default_routes_list ⇒ Object
:nodoc:
129 130 131 |
# File 'lib/mack/routing/route_map.rb', line 129 def default_routes_list # :nodoc: @_default_routes || [] end |
#defaults ⇒ Object
Creates ‘Rails’ style default mappings:
"/:controller/:action/:id"
"/:controller/:action"
These get created for each of the 4 HTTP verbs.
95 96 97 98 99 100 101 |
# File 'lib/mack/routing/route_map.rb', line 95 def defaults @_default_routes = [] [:get, :post, :put, :delete].each do |verb| @_default_routes << RouteObject.new("/:controller/:action/:id", :method => verb) @_default_routes << RouteObject.new("/:controller/:action", :method => verb) end end |
#empty? ⇒ Boolean
:nodoc:
25 26 27 28 29 30 |
# File 'lib/mack/routing/route_map.rb', line 25 def empty? # :nodoc: @_route_map.each do |k, v| return false if v.any? end return true end |
#handle_error(error, options) ⇒ Object
103 104 105 |
# File 'lib/mack/routing/route_map.rb', line 103 def handle_error(error, ) @_route_map[:errors][error] = end |
#inspect ⇒ Object
:nodoc:
121 122 123 |
# File 'lib/mack/routing/route_map.rb', line 121 def inspect # :nodoc: @_route_map.inspect end |
#reset! ⇒ Object
:nodoc:
13 14 15 16 |
# File 'lib/mack/routing/route_map.rb', line 13 def reset! # :nodoc: @_default_routes = [] @_route_map = {:get => [], :post => [], :put => [], :delete => [], :errors => {}} end |
#resource(controller, options = {}, &block) ⇒ Object
Sets up mappings and named routes for a resource.
108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/mack/routing/route_map.rb', line 108 def resource(controller, = {}, &block) # yield up to add other resources: if block_given? proxy = ResourceProxy.new(controller, [controller.to_sym]) yield proxy proxy.routes.each do |route| connect_with_name("#{controller}_#{route[:name]}", route[:path], .merge(route[:options])) end end # connect the default resources: build_resource_routes(controller, controller, controller, ) end |
#retrieve(url_or_request, verb = :get) ⇒ Object
:nodoc:
32 33 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 64 65 66 67 68 69 70 |
# File 'lib/mack/routing/route_map.rb', line 32 def retrieve(url_or_request, verb = :get) # :nodoc: path = url_or_request host = nil scheme = nil port = nil if url_or_request.is_a?(Mack::Request) path = url_or_request.path_info host = url_or_request.host scheme = url_or_request.scheme port = url_or_request.port verb = (url_or_request.params["_method"] || url_or_request.request_method.downcase).to_sym end path = path.dup format = (File.extname(path).blank? ? '.html' : File.extname(path)) format = format[1..format.length] routes = @_route_map[verb] routes.each do |route| if route.[:host] next unless !host.nil? && host.match(route.regex_patterns[:host]) end if route.[:scheme] next unless route.[:scheme].downcase == scheme end if route.[:port] next unless route.[:port].to_i == port.to_i end if route.match?(path) ret_val = route.(path, host) return ret_val end end @_default_routes.each do |route| if route.match?(path) && route.[:method] == verb ret_val = route.(path) return ret_val end end raise Mack::Errors::UndefinedRoute.new(path) end |
#retrieve_from_error(error) ⇒ Object
:nodoc:
72 73 74 |
# File 'lib/mack/routing/route_map.rb', line 72 def retrieve_from_error(error) # :nodoc: @_route_map[:errors][error] end |
#routes_list ⇒ Object
:nodoc:
125 126 127 |
# File 'lib/mack/routing/route_map.rb', line 125 def routes_list # :nodoc: @_route_map end |