Utopia SourceUtopiaControllerBase

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 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