Class: Spider::Controller
Defined Under Namespace
Modules: SceneMethods
Classes: BadRequest, ControllerError, Forbidden, Maintenance, NotFound
Instance Attribute Summary collapse
Attributes included from Dispatcher
#dispatch_previous
Class Method Summary
collapse
-
.app ⇒ Object
-
.before(conditions, method, params = {}) ⇒ Object
Defines a method that will be called before the controller’s before, if the action matches the given conditions.
-
.before_methods ⇒ Object
-
.before_unless(condition, method, params = {}) ⇒ Object
-
.controller_action?(method) ⇒ Boolean
-
.controller_actions(*methods) ⇒ Object
-
.default_action ⇒ Object
-
.find_resource(type, name, cur_path = nil) ⇒ Object
-
.find_resource_path(type, name, cur_path = nil) ⇒ Object
-
.layout_path ⇒ Object
-
.option(k, v) ⇒ Object
-
.options ⇒ Object
-
.template_path ⇒ Object
-
.url(action = nil) ⇒ Object
Returns the canonical url for this controller.
Instance Method Summary
collapse
-
#action_target? ⇒ Boolean
Returns true if this controller is the final target for the current action, that is, if it does not dispatch to any route.
-
#after(action = '', *arguments) ⇒ Object
-
#before(action = '', *arguments) ⇒ Object
-
#call_after(action = '', *arguments) ⇒ Object
-
#call_before(action = '', *arguments) ⇒ Object
-
#call_path ⇒ Object
-
#check_action(action, c) ⇒ Object
-
#done ⇒ Object
-
#done=(val) ⇒ Object
-
#done? ⇒ Boolean
-
#execute(action = '', *arguments) ⇒ Object
-
#get_action_method(action) ⇒ Object
-
#get_route(*args) ⇒ Object
-
#get_scene(scene = nil) ⇒ Object
-
#init ⇒ Object
Override this for controller initialization.
-
#initialize(request, response, scene = nil) ⇒ Controller
constructor
A new instance of Controller.
-
#inspect ⇒ Object
-
#is_target? ⇒ Boolean
Returns false if the target of the call is a widget, true otherwise.
-
#prepare_scene(scene) ⇒ Object
-
#request_path ⇒ Object
included
Methods included from Logger
add, check_request_level, close, close_all, datetime_format, datetime_format=, #debug, debug, debug?, #debug?, enquire_loggers, #error, error, #error?, error?, fatal, #fatal, fatal?, #fatal?, info, #info, info?, #info?, #log, log, method_missing, open, reopen, request_level, send_to_loggers, set_request_level, unknown, #unknown, warn, #warn, warn?, #warn?
Methods included from Dispatcher
#add_chain_item, #can_dispatch?, #dispatch, #dispatch_chain, #dispatch_next, #dispatcher_get_route, #do_dispatch, included, #route, #routes, #run_chain
Constructor Details
#initialize(request, response, scene = nil) ⇒ Controller
Returns a new instance of Controller.
140
141
142
143
144
145
146
147
148
|
# File 'lib/spiderfw/controller/controller.rb', line 140
def initialize(request, response, scene=nil)
@request = request
@response = response
@scene = scene || get_scene
@dispatch_path = ''
@is_target = true
init
end
|
Instance Attribute Details
#dispatch_action ⇒ Object
Returns the value of attribute dispatch_action.
138
139
140
|
# File 'lib/spiderfw/controller/controller.rb', line 138
def dispatch_action
@dispatch_action
end
|
#executed_method ⇒ Object
Returns the value of attribute executed_method.
137
138
139
|
# File 'lib/spiderfw/controller/controller.rb', line 137
def executed_method
@executed_method
end
|
#is_target ⇒ Object
Returns the value of attribute is_target.
138
139
140
|
# File 'lib/spiderfw/controller/controller.rb', line 138
def is_target
@is_target
end
|
Returns the value of attribute request.
137
138
139
|
# File 'lib/spiderfw/controller/controller.rb', line 137
def request
@request
end
|
Returns the value of attribute response.
137
138
139
|
# File 'lib/spiderfw/controller/controller.rb', line 137
def response
@response
end
|
Returns the value of attribute scene.
137
138
139
|
# File 'lib/spiderfw/controller/controller.rb', line 137
def scene
@scene
end
|
Class Method Details
41
42
43
44
45
46
47
48
49
|
# File 'lib/spiderfw/controller/controller.rb', line 41
def app
return @app if @app
@app ||= self.parent_module
while @app && !@app.include?(Spider::App) && @app != Object
@app = @app.parent_module
end
@app = nil if @app && !@app.include?(Spider::App)
return @app
end
|
.before(conditions, method, params = {}) ⇒ Object
Defines a method that will be called before the controller’s before, if the action matches the given conditions.
-
The first argument, the condition(s), may be a String, a Regexp, a Proc or a Symbol,
that will be checked against the action, or an Array containing several conditions.
-
The second argument, a Symbol, is the method to be called if the conditions match.
-
The third optional argument, an Hash, may contain :unless => true: in this case,
the conditions will be inverted, that is, the method will be executed unless the conditions match. Example:
before('list_', :before_lists)
will call the method before_lists if the action starts with ‘list_’
72
73
74
75
76
|
# File 'lib/spiderfw/controller/controller.rb', line 72
def before(conditions, method, params={})
@dispatch_methods ||= {}
@dispatch_methods[:before] ||= []
@dispatch_methods[:before] << [conditions, method, params]
end
|
.before_methods ⇒ Object
78
79
80
|
# File 'lib/spiderfw/controller/controller.rb', line 78
def before_methods
@dispatch_methods && @dispatch_methods[:before] ? @dispatch_methods[:before] : []
end
|
.before_unless(condition, method, params = {}) ⇒ Object
82
83
84
85
86
87
|
# File 'lib/spiderfw/controller/controller.rb', line 82
def before_unless(condition, method, params={})
@dispatch_methods ||= {}
@dispatch_methods[:before] ||= []
params[:unless] = true
@dispatch_methods[:before] << [condition, method, params]
end
|
.controller_action?(method) ⇒ Boolean
97
98
99
100
101
102
103
104
105
106
107
108
109
|
# File 'lib/spiderfw/controller/controller.rb', line 97
def controller_action?(method)
return false unless self.method_defined?(method)
return true if default_action && method == default_action.to_sym
if @controller_actions
res = @controller_actions.include?(method)
if (!res)
Spider.logger.info("Method #{method} is not a controller action for #{self}")
end
return res
else
return true
end
end
|
.controller_actions(*methods) ⇒ Object
89
90
91
92
93
94
95
|
# File 'lib/spiderfw/controller/controller.rb', line 89
def controller_actions(*methods)
if (methods.length > 0)
@controller_actions ||= []
@controller_actions += methods
end
@controller_actions
end
|
.default_action ⇒ Object
37
38
39
|
# File 'lib/spiderfw/controller/controller.rb', line 37
def default_action
'index'
end
|
.find_resource(type, name, cur_path = nil) ⇒ Object
111
112
113
|
# File 'lib/spiderfw/controller/controller.rb', line 111
def find_resource(type, name, cur_path=nil)
Spider.find_resource(type, name, cur_path, self)
end
|
.find_resource_path(type, name, cur_path = nil) ⇒ Object
115
116
117
118
|
# File 'lib/spiderfw/controller/controller.rb', line 115
def find_resource_path(type, name, cur_path=nil)
res = Spider.find_resource(type, name, cur_path, self)
return res ? res.path : nil
end
|
.layout_path ⇒ Object
56
57
58
59
|
# File 'lib/spiderfw/controller/controller.rb', line 56
def layout_path
return nil unless self.app
return self.app.path+'/views'
end
|
.option(k, v) ⇒ Object
33
34
35
|
# File 'lib/spiderfw/controller/controller.rb', line 33
def option(k, v)
self.option[k] = v
end
|
29
30
31
|
# File 'lib/spiderfw/controller/controller.rb', line 29
def options
@options ||= {}
end
|
.template_path ⇒ Object
51
52
53
54
|
# File 'lib/spiderfw/controller/controller.rb', line 51
def template_path
return nil unless self.app
return self.app.path+'/views'
end
|
.url(action = nil) ⇒ Object
Returns the canonical url for this controller
121
122
123
124
125
126
127
128
129
130
|
# File 'lib/spiderfw/controller/controller.rb', line 121
def url(action=nil)
u = @default_route || ''
u += "/#{action}" if action
if @default_dispatcher
u = @default_dispatcher.url + '/' + u
elsif self.app
u = self.app.url + '/' + u
end
u
end
|
Instance Method Details
#action_target? ⇒ Boolean
Returns true if this controller is the final target for the current action, that is, if it does not dispatch to any route
194
195
196
|
# File 'lib/spiderfw/controller/controller.rb', line 194
def action_target?
!@dispatch_next[@call_path] || @dispatch_next[@call_path].dest == self
end
|
#after(action = '', *arguments) ⇒ Object
271
272
|
# File 'lib/spiderfw/controller/controller.rb', line 271
def after(action='', *arguments)
end
|
#before(action = '', *arguments) ⇒ Object
250
251
|
# File 'lib/spiderfw/controller/controller.rb', line 250
def before(action='', *arguments)
end
|
#call_after(action = '', *arguments) ⇒ Object
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
|
# File 'lib/spiderfw/controller/controller.rb', line 254
def call_after(action='', *arguments)
return if respond_to?(:serving_static?) && self.serving_static?
after(action, *arguments)
catch(:done) do
d_next = dispatch_next(action)
unless d_next && d_next.obj == self
do_dispatch(:call_after, action, *arguments)
end
end
end
|
#call_before(action = '', *arguments) ⇒ Object
237
238
239
240
241
242
243
244
245
246
247
248
|
# File 'lib/spiderfw/controller/controller.rb', line 237
def call_before(action='', *arguments)
return if respond_to?(:serving_static?) && self.serving_static?
@call_path = action
before(action, *arguments)
catch(:done) do
d_next = dispatch_next(action)
unless d_next && d_next.obj == self
do_dispatch(:call_before, action, *arguments)
end
end
end
|
#call_path ⇒ Object
159
160
161
162
163
164
165
166
|
# File 'lib/spiderfw/controller/controller.rb', line 159
def call_path
act = @dispatch_action || ''
if (@dispatch_previous)
prev = @dispatch_previous.call_path
act = prev+'/'+act unless prev.empty?
end
return ('/'+act).gsub(/\/+/, '/').sub(/\/$/, '')
end
|
#check_action(action, c) ⇒ Object
288
289
290
|
# File 'lib/spiderfw/controller/controller.rb', line 288
def check_action(action, c)
self.class.check_action(action, c)
end
|
278
279
280
281
|
# File 'lib/spiderfw/controller/controller.rb', line 278
def done
self.done = true
throw :done
end
|
#done=(val) ⇒ Object
283
284
285
286
|
# File 'lib/spiderfw/controller/controller.rb', line 283
def done=(val)
@__done = val
@dispatch_previous.done = val if @dispatch_previous
end
|
#done? ⇒ Boolean
274
275
276
|
# File 'lib/spiderfw/controller/controller.rb', line 274
def done?
@__done
end
|
#execute(action = '', *arguments) ⇒ Object
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
|
# File 'lib/spiderfw/controller/controller.rb', line 204
def execute(action='', *arguments)
return if @__done
debug("Controller #{self} executing #{action} with arguments #{arguments}")
catch(:done) do
if can_dispatch?(:execute, action)
d_next = dispatch_next(action)
return do_dispatch(:execute, action) if d_next.dest != self
arguments = d_next.params
end
if d_next && d_next.dest == self
set_executed_method(d_next.action)
end
if @executed_method
meth = self.method(@executed_method)
args = arguments + @executed_method_arguments
@controller_action = args[0]
arity = meth.arity
unless arity == -1
arity = (-arity + 1) if arity < 0
args = arity == 0 ? [] : args[0..(arity-1)]
args = [nil] if meth.arity == 1 && args.empty?
end
Spider.logger.info("Executing: #{self.class.name}##{@executed_method}.#{@request.format}")
spider_main_controller_send = true
send(@executed_method, *args)
else
raise NotFound.new(action)
end
end
end
|
#get_action_method(action) ⇒ Object
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
# File 'lib/spiderfw/controller/controller.rb', line 172
def get_action_method(action)
method = nil
additional_arguments = nil
if (action =~ /^([^:]+)(:.+)$/)
method = $1
elsif (action =~ /^([^\/]+)\/(.+)$/) method = $1
additional_arguments = [$2]
else
method = action
end
method = method[0..-2] if !method.blank? && method[-1].chr == '/'
method, rest = method.split('.', 2) if method
method = self.class.default_action if !method || method.empty?
return nil if method.empty?
return [method.to_sym, additional_arguments]
end
|
#get_route(*args) ⇒ Object
315
316
317
318
319
320
321
322
323
324
|
# File 'lib/spiderfw/controller/controller.rb', line 315
def get_route(*args)
route = super
return route unless route
action = route.path.split('/').first
action_method, action_params = get_action_method(action)
if route.nil_route && !action.blank? && self.respond_to?(action_method)
route.action = action
end
route
end
|
#get_scene(scene = nil) ⇒ Object
292
293
294
295
296
297
298
|
# File 'lib/spiderfw/controller/controller.rb', line 292
def get_scene(scene=nil)
scene = Scene.new(scene) if scene.class == Hash
scene ||= Scene.new
return scene
end
|
Override this for controller initialization
151
152
153
|
# File 'lib/spiderfw/controller/controller.rb', line 151
def init
end
|
155
156
157
|
# File 'lib/spiderfw/controller/controller.rb', line 155
def inspect
self.class.to_s
end
|
#is_target? ⇒ Boolean
Returns false if the target of the call is a widget, true otherwise
199
200
201
|
# File 'lib/spiderfw/controller/controller.rb', line 199
def is_target?
@is_target
end
|
#prepare_scene(scene) ⇒ Object
300
301
302
303
304
305
306
307
308
309
310
311
312
313
|
# File 'lib/spiderfw/controller/controller.rb', line 300
def prepare_scene(scene)
req_path = @request.path
req_path += 'index' if !req_path.blank? && req_path[-1].chr == '/'
scene.request = {
:path => @request.path,
:page_path => req_path
}
scene.controller = {
:request_path => request_path,
:class => self.class
}
scene.content = {}
return scene
end
|
#request_path ⇒ Object
168
169
170
|
# File 'lib/spiderfw/controller/controller.rb', line 168
def request_path
call_path
end
|