class Utopia::Controller::Base

The base implementation of a controller class.

Constants

BASE_PATH
CONTROLLER
URI_PATH

Public Class Methods

base_path() click to toggle source

A string which is the full path to the directory which contains the controller.

# File lib/utopia/controller/base.rb, line 36
def self.base_path
        self.const_get(:BASE_PATH)
end
controller() click to toggle source

The controller middleware itself.

# File lib/utopia/controller/base.rb, line 46
def self.controller
        self.const_get(:CONTROLLER)
end
direct?(path) click to toggle source
# File lib/utopia/controller/base.rb, line 78
def direct?(path)
        path.dirname == uri_path
end
freeze() click to toggle source
Calls superclass method
# File lib/utopia/controller/base.rb, line 69
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
inspect() click to toggle source
# File lib/utopia/controller/base.rb, line 50
def self.inspect
        "#{super}#{self.uri_path}"
end
to_s() click to toggle source
# File lib/utopia/controller/base.rb, line 54
def self.to_s
        self.inspect
end
uri_path() click to toggle source

A relative path to the controller directory relative to the controller root directory.

# File lib/utopia/controller/base.rb, line 41
def self.uri_path
        self.const_get(:URI_PATH)
end

Public Instance Methods

body_for(status, headers, options) click to toggle source

Generate the body for the given status, headers and options.

# File lib/utopia/controller/base.rb, line 155
def body_for(status, headers, options)
        if body = options[:body]
                return body
        elsif content = options[:content]
                return [content]
        end
end
call(env) click to toggle source

Call into the next app as defined by rack.

# File lib/utopia/controller/base.rb, line 102
def call(env)
        self.class.controller.app.call(env)
end
catch_response() { |and nil| ... } click to toggle source
# File lib/utopia/controller/base.rb, line 83
def catch_response
        catch(:response) do
                yield and nil
        end
end
copy_instance_variables(from) click to toggle source

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.

# File lib/utopia/controller/base.rb, line 95
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) click to toggle source

Respond with an error which indiciates some kind of failure.

# File lib/utopia/controller/base.rb, line 135
def fail!(error = 400, message = nil)
        status = HTTP::Status.new(error, 400...600)
        
        message ||= status.to_s
        respond! [status.to_i, {}, [message]]
end
goto!(target, status = 302) click to toggle source

Controller relative redirect.

# File lib/utopia/controller/base.rb, line 130
def goto!(target, status = 302)
        redirect! self.class.uri_path + target
end
ignore!() click to toggle source

This will cause the controller middleware to pass on the request.

# File lib/utopia/controller/base.rb, line 117
def ignore!
        throw :response, nil
end
inspect() click to toggle source
# File lib/utopia/controller/base.rb, line 62
def inspect
        details = self.instance_variables.map{|name| " #{name}=#{self.instance_variable_get(name)}"}
        
        "\#<#{self.class}#{details.join}>"
end
process!(request, relative_path) click to toggle source

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.

# File lib/utopia/controller/base.rb, line 90
def process!(request, relative_path)
        return nil
end
redirect!(target, status = 302) click to toggle source

Request relative redirect. Respond with a redirect to the given target.

# File lib/utopia/controller/base.rb, line 122
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) click to toggle source

This will cause the middleware to generate a response.

# File lib/utopia/controller/base.rb, line 107
def respond!(response)
        throw :response, response
end
respond?(response) click to toggle source

Respond with the response, but only if it's not nil.

# File lib/utopia/controller/base.rb, line 112
def respond?(response)
        respond!(response) if response
end
succeed!(status: 200, headers: {}, type: nil, **options) click to toggle source

Succeed the request and immediately respond.

# File lib/utopia/controller/base.rb, line 143
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
to_s() click to toggle source
# File lib/utopia/controller/base.rb, line 58
def to_s
        "\#<#{self.class}>"
end