Class: Utopia::Controller::Base
- Inherits:
-
Object
- Object
- Utopia::Controller::Base
- Defined in:
- lib/utopia/controller/base.rb
Overview
The base implementation of a controller class.
Class Method Summary collapse
-
.base_path ⇒ Object
A string which is the full path to the directory which contains the controller.
-
.controller ⇒ Object
The controller middleware itself.
- .direct?(path) ⇒ Boolean
- .freeze ⇒ Object
-
.uri_path ⇒ Object
A relative path to the controller directory relative to the controller root directory.
Instance Method Summary collapse
-
#body_for(status, headers, options) ⇒ Object
Generate the body for the given status, headers and options.
-
#call(env) ⇒ Object
Call into the next app as defined by rack.
- #catch_response ⇒ Object
-
#copy_instance_variables(from) ⇒ Object
Copy the instance variables from the previous controller to the next controller (usually only a few).
-
#fail!(error = 400, message = nil) ⇒ Object
Respond with an error which indiciates some kind of failure.
-
#goto!(target, status = 302) ⇒ Object
Controller relative redirect.
-
#ignore! ⇒ Object
This will cause the controller middleware to pass on the request.
-
#process!(request, relative_path) ⇒ Object
Return nil if this controller didn't do anything.
-
#redirect!(target, status = 302) ⇒ Object
Request relative redirect.
-
#respond!(response) ⇒ Object
This will cause the middleware to generate a response.
-
#respond?(response) ⇒ Boolean
Respond with the response, but only if it's not nil.
-
#succeed!(status: 200, headers: {}, **options) ⇒ Object
Succeed the request and immediately respond.
Class Method Details
.base_path ⇒ Object
A string which is the full path to the directory which contains the controller.
30 31 32 |
# File 'lib/utopia/controller/base.rb', line 30 def self.base_path self.const_get(:BASE_PATH) end |
.controller ⇒ Object
The controller middleware itself.
40 41 42 |
# File 'lib/utopia/controller/base.rb', line 40 def self.controller self.const_get(:CONTROLLER) end |
.direct?(path) ⇒ Boolean
54 55 56 |
# File 'lib/utopia/controller/base.rb', line 54 def direct?(path) path.dirname == uri_path end |
.freeze ⇒ Object
45 46 47 48 49 50 51 52 |
# File 'lib/utopia/controller/base.rb', line 45 def freeze # This ensures that all class variables are frozen. self.instance_variables.each do |name| self.instance_variable_get(name).freeze end super end |
.uri_path ⇒ Object
A relative path to the controller directory relative to the controller root directory.
35 36 37 |
# File 'lib/utopia/controller/base.rb', line 35 def self.uri_path self.const_get(:URI_PATH) end |
Instance Method Details
#body_for(status, headers, options) ⇒ Object
Generate the body for the given status, headers and options.
131 132 133 134 135 136 137 |
# File 'lib/utopia/controller/base.rb', line 131 def body_for(status, headers, ) if body = [:body] return body elsif content = [:content] return [content] end end |
#call(env) ⇒ Object
Call into the next app as defined by rack.
78 79 80 |
# File 'lib/utopia/controller/base.rb', line 78 def call(env) self.class.controller.app.call(env) end |
#catch_response ⇒ Object
59 60 61 62 63 |
# File 'lib/utopia/controller/base.rb', line 59 def catch_response catch(:response) do yield and nil end end |
#copy_instance_variables(from) ⇒ Object
Copy the instance variables from the previous controller to the next controller (usually only a few). This allows controllers to share effectively the same instance variables while still being separate classes/instances.
71 72 73 74 75 |
# File 'lib/utopia/controller/base.rb', line 71 def copy_instance_variables(from) from.instance_variables.each do |name| self.instance_variable_set(name, from.instance_variable_get(name)) end end |
#fail!(error = 400, message = nil) ⇒ Object
Respond with an error which indiciates some kind of failure.
111 112 113 114 115 116 |
# File 'lib/utopia/controller/base.rb', line 111 def fail!(error = 400, = nil) status = HTTP::Status.new(error, 400...600) ||= status.to_s respond! [status.to_i, {}, []] end |
#goto!(target, status = 302) ⇒ Object
Controller relative redirect.
106 107 108 |
# File 'lib/utopia/controller/base.rb', line 106 def goto!(target, status = 302) redirect! self.class.uri_path + target end |
#ignore! ⇒ Object
This will cause the controller middleware to pass on the request.
93 94 95 |
# File 'lib/utopia/controller/base.rb', line 93 def ignore! throw :response, nil end |
#process!(request, relative_path) ⇒ Object
Return nil if this controller didn't do anything. Request will keep on processing. Return a valid rack response if the controller can do so.
66 67 68 |
# File 'lib/utopia/controller/base.rb', line 66 def process!(request, relative_path) return nil end |
#redirect!(target, status = 302) ⇒ Object
Request relative redirect. Respond with a redirect to the given target.
98 99 100 101 102 103 |
# File 'lib/utopia/controller/base.rb', line 98 def redirect!(target, status = 302) status = HTTP::Status.new(status, 300...400) location = target.to_s respond! [status.to_i, {HTTP::LOCATION => location}, [status.to_s]] end |
#respond!(response) ⇒ Object
This will cause the middleware to generate a response.
83 84 85 |
# File 'lib/utopia/controller/base.rb', line 83 def respond!(response) throw :response, response end |
#respond?(response) ⇒ Boolean
Respond with the response, but only if it's not nil.
88 89 90 |
# File 'lib/utopia/controller/base.rb', line 88 def respond?(response) respond!(response) if response end |
#succeed!(status: 200, headers: {}, **options) ⇒ Object
Succeed the request and immediately respond.
119 120 121 122 123 124 125 126 127 128 |
# File 'lib/utopia/controller/base.rb', line 119 def succeed!(status: 200, headers: {}, **) status = HTTP::Status.new(status, 200...300) if [:type] headers[Rack::CONTENT_TYPE] = [:type].to_s end body = body_for(status, headers, ) respond! [status.to_i, headers, body || []] end |