Class: Utopia::Middleware::Controller
- Inherits:
-
Object
- Object
- Utopia::Middleware::Controller
show all
- Defined in:
- lib/utopia/middleware/controller.rb
Defined Under Namespace
Modules: Direct
Classes: Base, Variables
Constant Summary
collapse
- CONTROLLER_RB =
"controller.rb"
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(app, options = {}) ⇒ Controller
Returns a new instance of Controller.
245
246
247
248
249
250
251
252
253
254
255
256
257
|
# File 'lib/utopia/middleware/controller.rb', line 245
def initialize(app, options = {})
@app = app
@root = options[:root] || Utopia::Middleware::default_root
@controllers = {}
@cache_controllers = (UTOPIA_ENV == :production)
if options[:controller_file]
@controller_file = options[:controller_file]
else
@controller_file = "controller.rb"
end
end
|
Instance Attribute Details
Returns the value of attribute app.
259
260
261
|
# File 'lib/utopia/middleware/controller.rb', line 259
def app
@app
end
|
Instance Method Details
#call(env) ⇒ Object
309
310
311
312
313
314
315
316
317
318
319
320
321
322
|
# File 'lib/utopia/middleware/controller.rb', line 309
def call(env)
variables = (env["utopia.controller"] ||= Variables.new)
request = Rack::Request.new(env)
path = Path.create(request.path_info)
fetch_controllers(path).each do |controller|
if result = controller.process!(path, request)
return result
end
end
return @app.call(env)
end
|
#fetch_controllers(path) ⇒ Object
299
300
301
302
303
304
305
306
307
|
# File 'lib/utopia/middleware/controller.rb', line 299
def fetch_controllers(path)
controllers = []
path.ascend do |parent_path|
controllers << lookup(parent_path)
end
return controllers.compact.reverse
end
|
#load_file(path) ⇒ Object
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
|
# File 'lib/utopia/middleware/controller.rb', line 271
def load_file(path)
if path.directory?
uri_path = path
base_path = File.join(@root, uri_path.components)
else
uri_path = path.dirname
base_path = File.join(@root, uri_path.components)
end
controller_path = File.join(base_path, CONTROLLER_RB)
if File.exist?(controller_path)
klass = Class.new(Base)
klass.const_set(:BASE_PATH, base_path)
klass.const_set(:URI_PATH, uri_path)
$LOAD_PATH.unshift(base_path)
klass.class_eval(File.read(controller_path), controller_path)
$LOAD_PATH.delete(base_path)
return klass.new(self)
else
return nil
end
end
|
#lookup(path) ⇒ Object
261
262
263
264
265
266
267
268
269
|
# File 'lib/utopia/middleware/controller.rb', line 261
def lookup(path)
if @cache_controllers
return @controllers.fetch(path.to_s) do |key|
@controllers[key] = load_file(path)
end
else
return load_file(path)
end
end
|