class Base
The base implementation of a controller class.
Definitions
def self.base_path
A string which is the full path to the directory which contains the controller.
Implementation
def self.base_path
self.const_get(:BASE_PATH)
end
def self.uri_path
A relative path to the controller directory relative to the controller root directory.
Implementation
def self.uri_path
self.const_get(:URI_PATH)
end
def self.controller
The controller middleware itself.
Implementation
def self.controller
self.const_get(:CONTROLLER)
end
def self.inspect
Generate a debug representation of this object.
Signature
-
returns
String The resulting string.
Implementation
def self.inspect
"#{super}#{self.uri_path}"
end
def inspect
Generate a debug representation of this object.
Signature
-
returns
String The resulting string.
Implementation
def inspect
details = self.instance_variables.map{|name| " #{name}=#{self.instance_variable_get(name)}"}
"\#<#{self.class}#{details.join}>"
end
def self.to_s
Convert this object to a string.
Signature
-
returns
String The resulting string.
Implementation
def self.to_s
self.inspect
end
def to_s
Convert this object to a string.
Signature
-
returns
String The resulting string.
Implementation
def to_s
"\#<#{self.class}>"
end
def self.freeze
Freeze this object and its internal state.
Signature
-
returns
self This object.
Implementation
def self.freeze
# This ensures that all class variables are frozen.
self.instance_variables.each do |name|
self.instance_variable_get(name).freeze
end
super
end
def self.direct?(path)
Check whether the path refers directly to this controller.
Signature
-
parameter
pathUtopia::Path | String The path.
-
returns
Boolean Whether the path is directly contained by this controller's URI path.
Implementation
def self.direct?(path)
path.dirname == uri_path
end
def catch_response
Catch and return a response thrown while executing the block.
Signature
-
returns
Array | Nil The thrown response, or
nilif the block completes.
Implementation
def catch_response
catch(:response) do
yield and nil
end
end
def process!(request, relative_path)
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.
Implementation
def process!(request, relative_path)
return nil
end
def copy_instance_variables(from)
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.
Implementation
def copy_instance_variables(from)
from.instance_variables.each do |name|
self.instance_variable_set(name, from.instance_variable_get(name))
end
end
def call(env)
Call into the next app as defined by rack.
Implementation
def call(env)
self.class.controller.app.call(env)
end
def respond!(response)
This will cause the middleware to generate a response.
Implementation
def respond!(response)
throw :response, response
end
def respond?(response)
Respond with the response, but only if it's not nil.
Implementation
def respond?(response)
respond!(response) if response
end
def ignore!
This will cause the controller middleware to pass on the request.
Implementation
def ignore!
throw :response, nil
end
def redirect!(target, status = 302)
Request relative redirect. Respond with a redirect to the given target.
Implementation
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
def goto!(target, status = 302)
Controller relative redirect.
Implementation
def goto!(target, status = 302)
redirect! self.class.uri_path + target
end
def fail!(error = 400, message = nil)
Respond with an error which indiciates some kind of failure.
Implementation
def fail!(error = 400, message = nil)
status = HTTP::Status.new(error, 400...600)
message ||= status.to_s
respond! [status.to_i, {}, [message]]
end
def succeed!(status: 200, headers: {}, type: nil, **options)
Succeed the request and immediately respond.
Implementation
def succeed!(status: 200, headers: {}, type: nil, **options)
status = HTTP::Status.new(status, 200...300)
if type
headers[CONTENT_TYPE] = type.to_s
end
body = body_for(status, headers, options)
respond! [status.to_i, headers, body || []]
end
def body_for(status, headers, options)
Generate the body for the given status, headers and options.
Implementation
def body_for(status, headers, options)
if body = options[:body]
return body
elsif content = options[:content]
return [content]
end
end