Class: Rad::Router
- Inherits:
-
Object
show all
- Defined in:
- lib/rad/router/_router.rb,
lib/rad/router/_require.rb
Defined Under Namespace
Modules: AbstractRouter, CoreRoutingHelper
Classes: AliasRouter, BasicRouter, Configurator, DefaultFormatProcessor, ObjectRouter, RestfulRouter, SimpleRouter
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#decode(path, params) ⇒ Object
-
#dont_persist_params(&block) ⇒ Object
-
#encode(klass, method, params = {}) ⇒ Object
-
#encode_method(route_method, has_id) ⇒ Object
-
#initialize(class_variable = :class, routers = [[:default_router, Rad::Router::SimpleRouter.new]], format_processor = Rad::Router::DefaultFormatProcessor.new) ⇒ Router
constructor
A new instance of Router.
-
#persist_params(&block) ⇒ Object
-
#persist_params? ⇒ Boolean
-
#persistent_params ⇒ Object
-
#url_for(*args) ⇒ Object
-
#url_for_class(klass, method, params) ⇒ Object
-
#url_for_path(path, params) ⇒ Object
Constructor Details
#initialize(class_variable = :class, routers = [[:default_router, Rad::Router::SimpleRouter.new]], format_processor = Rad::Router::DefaultFormatProcessor.new) ⇒ Router
Returns a new instance of Router.
7
8
9
10
11
12
13
14
|
# File 'lib/rad/router/_router.rb', line 7
def initialize (
class_variable = :class,
routers = [[:default_router, Rad::Router::SimpleRouter.new]],
format_processor = Rad::Router::DefaultFormatProcessor.new
)
@class_variable, @format_processor = class_variable, format_processor
self.routers = routers
end
|
Instance Attribute Details
#default_method ⇒ Object
Returns the value of attribute default_method.
5
6
7
|
# File 'lib/rad/router/_router.rb', line 5
def default_method
@default_method
end
|
#default_path ⇒ Object
Returns the value of attribute default_path.
5
6
7
|
# File 'lib/rad/router/_router.rb', line 5
def default_path
@default_path
end
|
Returns the value of attribute format_processor.
4
5
6
|
# File 'lib/rad/router/_router.rb', line 4
def format_processor
@format_processor
end
|
Returns the value of attribute routers.
4
5
6
|
# File 'lib/rad/router/_router.rb', line 4
def routers
@routers
end
|
Instance Method Details
#decode(path, params) ⇒ Object
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
# File 'lib/rad/router/_router.rb', line 54
def decode path, params
path.first.must == '/'
path, format = format_processor.remove_format path if format_processor
result = nil
routers.each do |name, route|
result = route.decode path, params
break if result
end
raise "no route for '#{safe_workspace.path}' request!" unless result
klass, method, params = result
method ||= default_method
params.must_be.defined
method.must_be.a Symbol
raise "invalid route! No method '#{method}' for #{klass}!" unless klass.instance_methods.include? method
raise "invalid route! You try to call protected method '#{method}' for '#{path}'!" unless klass.public_instance_methods.include? method
return klass, method, params
end
|
#dont_persist_params(&block) ⇒ Object
144
145
146
147
148
149
150
151
152
153
154
155
156
|
# File 'lib/rad/router/_router.rb', line 144
def dont_persist_params &block
if block
before = safe_workspace.persist_params?
begin
safe_workspace.delete :persist_params
block.call
ensure
safe_workspace[:persist_params] = true if before
end
else
safe_workspace.delete :persist_params
end
end
|
#encode(klass, method, params = {}) ⇒ Object
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
# File 'lib/rad/router/_router.rb', line 79
def encode klass, method, params = {}
klass.must_be.defined
method.must_be.a Symbol
params = params.clone
format = params.delete(:format)
inject_persistent_params! params
result = nil
routers.each do |name, route|
result = route.encode klass, method, params
break if result
end
raise "no route for '#{klass}.#{method}'!" unless result
path, params = result
path.must_be.defined
params.must_be.defined
path = format_processor.add_format path, format if format_processor and format
return path, params
end
|
#encode_method(route_method, has_id) ⇒ Object
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
# File 'lib/rad/router/_router.rb', line 109
def encode_method route_method, has_id
route_method.must_be.present
result = nil
routers.each do |name, route|
result = route.respond_to :encode_method, route_method, has_id
break if result
end
raise "no route for '#{route_method}' route method!" unless result
klass, method = result
klass.must_be.defined
method.must_be.defined
method.must =~ /^[_a-zA-Z0-9]+$/
return [klass, method]
end
|
#persist_params(&block) ⇒ Object
130
131
132
133
134
135
136
137
138
139
140
141
142
|
# File 'lib/rad/router/_router.rb', line 130
def persist_params &block
if block
before = safe_workspace.persist_params?
begin
safe_workspace[:persist_params] = true
block.call
ensure
safe_workspace.delete :persist_params unless before
end
else
safe_workspace[:persist_params] = true
end
end
|
#persist_params? ⇒ Boolean
158
159
160
|
# File 'lib/rad/router/_router.rb', line 158
def persist_params?
safe_workspace.persist_params?
end
|
#persistent_params ⇒ Object
162
|
# File 'lib/rad/router/_router.rb', line 162
def persistent_params; @persistent_params ||= [] end
|
#url_for(*args) ⇒ Object
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
# File 'lib/rad/router/_router.rb', line 28
def url_for *args
if args.first.is_a?(Class)
args.size.must_be.in 2..3
klass, method, options = args
url_for_class(klass, method, (options || {}))
else
first = args.first.to_s
if first =~ /^\//
args.size.must_be.in 1..2
path, options = args
url_for_path(path, (options || {}))
elsif first =~ /^http:\/\//
args.size.must_be.in 1..2
raise "can't use option with full url (#{first})" if args.size == 2 and !args.last.blank?
first
else
args.size.must_be.in 1..2
method, options = args
url_for_class(nil, method, (options || {}))
end
end
end
|
#url_for_class(klass, method, params) ⇒ Object
213
214
215
216
217
218
219
220
221
222
223
224
225
226
|
# File 'lib/rad/router/_router.rb', line 213
def url_for_class klass, method, params
klass ||= current_class
format = params[:format]
path, params = encode klass, method, params
url = url_for_path path, params
url.marks.format = format.to_s if format
url
end
|
#url_for_path(path, params) ⇒ Object
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
# File 'lib/rad/router/_router.rb', line 164
def url_for_path path, params
url_root = params.delete(:url_root)
url = (url_root && url_root != '/') ? url_root + path : path.clone
host, port = params.delete(:host), params.delete(:port)
format = params.delete(:format)
params = {json: params.to_json} if params.delete :as_json
if format
params[:format] = format
url.marks.format = format.to_s
end
to_delete = []
params.each{|k, v| to_delete << k if v.nil?}
to_delete.each{|k| params.delete k}
delimiter = path.include?('?') ? '&' : '?'
url << "#{delimiter}#{params.to_query}" unless params.empty?
if host.blank?
url
else
%{http://#{host}#{":#{port}" unless port.blank?}#{url}}
end
end
|