Class: Utopia::Controller
- Inherits:
-
Object
- Object
- Utopia::Controller
show all
- Defined in:
- lib/utopia/controller.rb,
lib/utopia/controller/base.rb,
lib/utopia/controller/action.rb,
lib/utopia/controller/respond.rb,
lib/utopia/controller/rewrite.rb,
lib/utopia/controller/variables.rb
Defined Under Namespace
Modules: Respond, Rewrite
Classes: Action, Base, Variables
Constant Summary
collapse
- CONTROLLER_RB =
'controller.rb'.freeze
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(app, root: nil, cache_controllers: false) ⇒ Controller
Returns a new instance of Controller.
54
55
56
57
58
59
60
61
62
63
|
# File 'lib/utopia/controller.rb', line 54
def initialize(app, root: nil, cache_controllers: false)
@app = app
@root = root || Utopia::default_root
if cache_controllers
@controller_cache = Concurrent::Map.new
else
@controller_cache = nil
end
end
|
Instance Attribute Details
#app ⇒ Object
Returns the value of attribute app.
65
66
67
|
# File 'lib/utopia/controller.rb', line 65
def app
@app
end
|
Class Method Details
.[](request) ⇒ Object
50
51
52
|
# File 'lib/utopia/controller.rb', line 50
def self.[] request
request.env[VARIABLES_KEY]
end
|
Instance Method Details
#call(env) ⇒ Object
143
144
145
146
147
148
149
150
151
152
153
|
# File 'lib/utopia/controller.rb', line 143
def call(env)
env[VARIABLES_KEY] ||= Variables.new
request = Rack::Request.new(env)
if result = invoke_controllers(request)
return result
end
return @app.call(env)
end
|
#freeze ⇒ Object
67
68
69
70
71
|
# File 'lib/utopia/controller.rb', line 67
def freeze
@root.freeze
super
end
|
#invoke_controllers(request) ⇒ Object
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
# File 'lib/utopia/controller.rb', line 115
def invoke_controllers(request)
relative_path = Path[request.path_info]
controller_path = Path.new
variables = request.env[VARIABLES_KEY]
while relative_path.components.any?
controller_path.components << relative_path.components.shift
if controller = lookup_controller(controller_path)
controller = controller.clone
variables << controller
if result = controller.process!(request, relative_path)
return result
end
end
end
request.env[Rack::PATH_INFO] = controller_path.to_s
return nil
end
|
#load_controller_file(uri_path) ⇒ Object
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
108
109
110
111
112
113
|
# File 'lib/utopia/controller.rb', line 83
def load_controller_file(uri_path)
base_path = File.join(@root, uri_path.components)
controller_path = File.join(base_path, CONTROLLER_RB)
if File.exist?(controller_path)
klass = Class.new(Base)
klass.const_set(:BASE_PATH, base_path.freeze)
klass.const_set(:URI_PATH, uri_path.dup.freeze)
klass.const_set(:CONTROLLER, self)
klass.class_eval(File.read(controller_path), controller_path)
klass.freeze
return klass.new
else
return nil
end
end
|
#lookup_controller(path) ⇒ Object
73
74
75
76
77
78
79
80
81
|
# File 'lib/utopia/controller.rb', line 73
def lookup_controller(path)
if @controller_cache
@controller_cache.fetch_or_store(path.to_s) do
load_controller_file(path)
end
else
load_controller_file(path)
end
end
|