Class: Utopia::Controller::Base
- Inherits:
-
Object
- Object
- Utopia::Controller::Base
- Defined in:
- lib/utopia/controller/base.rb
Class Method Summary collapse
- .actions ⇒ Object
- .base_path ⇒ Object
- .controller ⇒ Object
- .direct?(path) ⇒ Boolean
- .freeze ⇒ Object
- .lookup(path) ⇒ Object
- .on(first, *path, **options, &block) ⇒ Object
- .uri_path ⇒ Object
Instance Method Summary collapse
-
#actions_for_request(request, path) ⇒ Object
Given a path, look up all matched actions.
-
#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.
-
#passthrough(request, path) ⇒ Object
Given a request, call associated actions if at least one exists.
-
#process!(request, 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.
- #succeed!(status: 200, headers: {}, **options) ⇒ Object (also: #success!)
Class Method Details
.actions ⇒ Object
52 53 54 |
# File 'lib/utopia/controller/base.rb', line 52 def actions @actions ||= Action.new end |
.base_path ⇒ Object
26 27 28 |
# File 'lib/utopia/controller/base.rb', line 26 def self.base_path self.const_get(:BASE_PATH) end |
.controller ⇒ Object
34 35 36 |
# File 'lib/utopia/controller/base.rb', line 34 def self.controller self.const_get(:CONTROLLER) end |
.direct?(path) ⇒ Boolean
48 49 50 |
# File 'lib/utopia/controller/base.rb', line 48 def direct?(path) path.dirname == uri_path end |
.freeze ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'lib/utopia/controller/base.rb', line 39 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 |
.lookup(path) ⇒ Object
64 65 66 67 68 69 70 71 |
# File 'lib/utopia/controller/base.rb', line 64 def lookup(path) if @actions relative_path = (path - uri_path).to_a return @actions.select(relative_path) else [] end end |
.on(first, *path, **options, &block) ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/utopia/controller/base.rb', line 56 def on(first, *path, **, &block) if first.is_a? Symbol first = ['**', first] end actions.define(Path.split(first) + path, , &block) end |
.uri_path ⇒ Object
30 31 32 |
# File 'lib/utopia/controller/base.rb', line 30 def self.uri_path self.const_get(:URI_PATH) end |
Instance Method Details
#actions_for_request(request, path) ⇒ Object
Given a path, look up all matched actions.
75 76 77 |
# File 'lib/utopia/controller/base.rb', line 75 def actions_for_request(request, path) self.class.lookup(path) end |
#body_for(status, headers, options) ⇒ Object
Generate the body for the given status, headers and options.
155 156 157 158 159 160 161 |
# File 'lib/utopia/controller/base.rb', line 155 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.
108 109 110 |
# File 'lib/utopia/controller/base.rb', line 108 def call(env) self.class.controller.app.call(env) end |
#catch_response ⇒ Object
79 80 81 82 83 |
# File 'lib/utopia/controller/base.rb', line 79 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.
101 102 103 104 105 |
# File 'lib/utopia/controller/base.rb', line 101 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.
136 137 138 139 140 141 |
# File 'lib/utopia/controller/base.rb', line 136 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.
131 132 133 |
# File 'lib/utopia/controller/base.rb', line 131 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.
118 119 120 |
# File 'lib/utopia/controller/base.rb', line 118 def ignore! throw :response, nil end |
#passthrough(request, path) ⇒ Object
Given a request, call associated actions if at least one exists.
86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/utopia/controller/base.rb', line 86 def passthrough(request, path) actions = actions_for_request(request, path) unless actions.empty? return catch_response do actions.each do |action| action.call(self, request, path) end end end return nil end |
#process!(request, 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.
167 168 169 |
# File 'lib/utopia/controller/base.rb', line 167 def process!(request, path) passthrough(request, path) end |
#redirect!(target, status = 302) ⇒ Object
Request relative redirect. Respond with a redirect to the given target.
123 124 125 126 127 128 |
# File 'lib/utopia/controller/base.rb', line 123 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.
113 114 115 |
# File 'lib/utopia/controller/base.rb', line 113 def respond!(response) throw :response, response end |
#succeed!(status: 200, headers: {}, **options) ⇒ Object Also known as: success!
143 144 145 146 147 148 149 150 151 152 |
# File 'lib/utopia/controller/base.rb', line 143 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 |