Class: Mack::Routes::RouteMap

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/mack/routing/route_map.rb

Overview

See Mack::Routes for more information.

Instance Method Summary collapse

Constructor Details

#initializeRouteMap

: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:



80
81
82
# File 'lib/mack/routing/route_map.rb', line 80

def method_missing(sym, *args, &block) # :nodoc:
  connect_with_name(sym, *args, &block)
end

Instance Method Details

#any?Boolean

:nodoc:

Returns:

  • (Boolean)


19
20
21
22
23
24
# File 'lib/mack/routing/route_map.rb', line 19

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:



136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/mack/routing/route_map.rb', line 136

def build_resource_routes(method_base, path_base, controller, options) # :nodoc:
  deferred = options.delete(:deferred?) || []
  if deferred && !deferred.is_a?(Array)
    deferred = [:create, :update]
  end
  connect_with_name("#{method_base}_index", "/#{path_base}", {:controller => controller, :action => :index, :method => :get, :deferred? => deferred.include?(:index)}.merge(options))
  connect_with_name("#{method_base}_create", "/#{path_base}", {:controller => controller, :action => :create, :method => :post, :deferred? => deferred.include?(:create)}.merge(options))
  connect_with_name("#{method_base}_new", "/#{path_base}/new", {:controller => controller, :action => :new, :method => :get, :deferred? => deferred.include?(:new)}.merge(options))
  connect_with_name("#{method_base}_show", "/#{path_base}/:id", {:controller => controller, :action => :show, :method => :get, :deferred? => deferred.include?(:show)}.merge(options))
  connect_with_name("#{method_base}_edit", "/#{path_base}/:id/edit", {:controller => controller, :action => :edit, :method => :get, :deferred? => deferred.include?(:edit)}.merge(options))
  connect_with_name("#{method_base}_update", "/#{path_base}/:id", {:controller => controller, :action => :update, :method => :put, :deferred? => deferred.include?(:update)}.merge(options))
  connect_with_name("#{method_base}_delete", "/#{path_base}/:id", {:controller => controller, :action => :delete, :method => :delete, :deferred? => deferred.include?(:delete)}.merge(options))
end

#connect(path, options = {}, &block) ⇒ Object

Connects a url pattern to a controller, an action, and an HTTP verb.



69
70
71
72
73
74
75
76
77
78
# File 'lib/mack/routing/route_map.rb', line 69

def connect(path, options = {}, &block)
  options = handle_options(options, &block)
  if path.is_a?(String)
    path = "/#{path}" unless path.match(/^\//)
  end
  route = RouteObject.new(path, options)
  @_route_map[options[:method]] << route
  @_deferred_route_map[options[:method]] << route if route.deferred
  route
end

#connect_with_name(name, path, options = {}, &block) ⇒ Object

:nodoc:



130
131
132
133
134
# File 'lib/mack/routing/route_map.rb', line 130

def connect_with_name(name, path, options = {}, &block) # :nodoc:
  n_route = name.methodize
  route = connect(path, {:action => n_route.to_sym}.merge(options), &block)
  Mack::Routes::Urls.create_method(n_route, route)        
end

#default_routes_listObject

:nodoc:



126
127
128
# File 'lib/mack/routing/route_map.rb', line 126

def default_routes_list # :nodoc:
  @_default_routes || []
end

#defaultsObject

Creates ‘Rails’ style default mappings:

"/:controller/:action/:id"
"/:controller/:action"

These get created for each of the 4 HTTP verbs.



88
89
90
91
92
93
94
# File 'lib/mack/routing/route_map.rb', line 88

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

#deferred_routes_listObject

:nodoc:



122
123
124
# File 'lib/mack/routing/route_map.rb', line 122

def deferred_routes_list # :nodoc:
  @_deferred_route_map
end

#empty?Boolean

:nodoc:

Returns:

  • (Boolean)


26
27
28
29
30
31
# File 'lib/mack/routing/route_map.rb', line 26

def empty? # :nodoc:
  @_route_map.each do |k, v|
    return false if v.any?
  end
  return true
end

#handle_error(error, options) ⇒ Object



96
97
98
# File 'lib/mack/routing/route_map.rb', line 96

def handle_error(error, options)
  @_route_map[:errors][error] = options
end

#inspectObject

:nodoc:



114
115
116
# File 'lib/mack/routing/route_map.rb', line 114

def inspect # :nodoc:
  @_route_map.inspect
end

#reset!Object

:nodoc:



13
14
15
16
17
# File 'lib/mack/routing/route_map.rb', line 13

def reset! # :nodoc:
  @_default_routes = []
  @_route_map = {:get => [], :post => [], :put => [], :delete => [], :errors => {}}
  @_deferred_route_map = {:get => [], :post => [], :put => [], :delete => [], :errors => {}}
end

#resource(controller, options = {}, &block) ⇒ Object

Sets up mappings and named routes for a resource.



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/mack/routing/route_map.rb', line 101

def resource(controller, options = {}, &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], options.merge(route[:options]))
    end
  end
  # connect the default resources:
  build_resource_routes(controller, controller, controller, options)
end

#retrieve(url_or_request, verb = :get) ⇒ Object

:nodoc:



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
# File 'lib/mack/routing/route_map.rb', line 33

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.match?(:uri => path, :host => host, :scheme => scheme, :port => port)
      ret_val = route.options_with_parameters(path, host)
      return ret_val
    end
  end
  @_default_routes.each do |route|
    if route.match?(:uri => path) && route.options[:method] == verb
      ret_val = route.options_with_parameters(path)
      return ret_val
    end
  end
  raise Mack::Errors::UndefinedRoute.new(path)
end

#retrieve_from_error(error) ⇒ Object

:nodoc:



64
65
66
# File 'lib/mack/routing/route_map.rb', line 64

def retrieve_from_error(error) # :nodoc:
  @_route_map[:errors][error]
end

#routes_listObject

:nodoc:



118
119
120
# File 'lib/mack/routing/route_map.rb', line 118

def routes_list # :nodoc:
  @_route_map
end