Class: Mack::Routes::RouteMap
- Inherits:
-
Object
- Object
- 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:.
-
#connect(path, options = {}, &block) ⇒ Object
Connects a url pattern to a controller, an action, and an HTTP verb.
-
#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, &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:.
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 |
#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 |
#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 |
# File 'lib/mack/routing/route_map.rb', line 95 def defaults [: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
102 103 104 |
# File 'lib/mack/routing/route_map.rb', line 102 def handle_error(error, ) @_route_map[:errors][error] = end |
#inspect ⇒ Object
:nodoc:
126 127 128 |
# File 'lib/mack/routing/route_map.rb', line 126 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, &block) ⇒ Object
Sets up mappings and named routes for a resource.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/mack/routing/route_map.rb', line 107 def resource(controller, &block) # yield up to add other resources: if block_given? proxy = ResourceProxy.new(controller) yield proxy proxy.routes.each do |route| connect_with_name("#{controller}_#{route[:name]}", route[:path], route[:options]) end end # connect the default resources: connect_with_name("#{controller}_index", "/#{controller}", {:controller => controller, :action => :index, :method => :get}) connect_with_name("#{controller}_create", "/#{controller}", {:controller => controller, :action => :create, :method => :post}) connect_with_name("#{controller}_new", "/#{controller}/new", {:controller => controller, :action => :new, :method => :get}) connect_with_name("#{controller}_show", "/#{controller}/:id", {:controller => controller, :action => :show, :method => :get}) connect_with_name("#{controller}_edit", "/#{controller}/:id/edit", {:controller => controller, :action => :edit, :method => :get}) connect_with_name("#{controller}_update", "/#{controller}/:id", {:controller => controller, :action => :update, :method => :put}) connect_with_name("#{controller}_delete", "/#{controller}/:id", {:controller => controller, :action => :delete, :method => :delete}) 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 route.[:host].downcase == 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) 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 |