Class: Utopia::Controller::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/utopia/controller/base.rb

Overview

The base implementation of a controller class.

Constant Summary collapse

URI_PATH =
nil
BASE_PATH =
nil
CONTROLLER =
nil

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.base_pathObject

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



36
37
38
# File 'lib/utopia/controller/base.rb', line 36

def self.base_path
	self.const_get(:BASE_PATH)
end

.controllerObject

The controller middleware itself.



46
47
48
# File 'lib/utopia/controller/base.rb', line 46

def self.controller
	self.const_get(:CONTROLLER)
end

.direct?(path) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/utopia/controller/base.rb', line 77

def self.direct?(path)
	path.dirname == uri_path
end

.freezeObject



68
69
70
71
72
73
74
75
# File 'lib/utopia/controller/base.rb', line 68

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

.inspectObject



50
51
52
# File 'lib/utopia/controller/base.rb', line 50

def self.inspect
	"#{super}#{self.uri_path}"
end

.to_sObject



54
55
56
# File 'lib/utopia/controller/base.rb', line 54

def self.to_s
	self.inspect
end

.uri_pathObject

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



41
42
43
# File 'lib/utopia/controller/base.rb', line 41

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.



153
154
155
156
157
158
159
# File 'lib/utopia/controller/base.rb', line 153

def body_for(status, headers, options)
	if body = options[:body]
		return body
	elsif content = options[:content]
		return [content]
	end
end

#call(env) ⇒ Object

Call into the next app as defined by rack.



100
101
102
# File 'lib/utopia/controller/base.rb', line 100

def call(env)
	self.class.controller.app.call(env)
end

#catch_responseObject



81
82
83
84
85
# File 'lib/utopia/controller/base.rb', line 81

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.



93
94
95
96
97
# File 'lib/utopia/controller/base.rb', line 93

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.



133
134
135
136
137
138
# File 'lib/utopia/controller/base.rb', line 133

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) ⇒ Object

Controller relative redirect.



128
129
130
# File 'lib/utopia/controller/base.rb', line 128

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.



115
116
117
# File 'lib/utopia/controller/base.rb', line 115

def ignore!
	throw :response, nil
end

#inspectObject



62
63
64
65
66
# 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) ⇒ 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.



88
89
90
# File 'lib/utopia/controller/base.rb', line 88

def process!(request, relative_path)
	return nil
end

#redirect!(target, status = 302) ⇒ Object

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



120
121
122
123
124
125
# File 'lib/utopia/controller/base.rb', line 120

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.



105
106
107
# File 'lib/utopia/controller/base.rb', line 105

def respond!(response)
	throw :response, response
end

#respond?(response) ⇒ Boolean

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

Returns:

  • (Boolean)


110
111
112
# File 'lib/utopia/controller/base.rb', line 110

def respond?(response)
	respond!(response) if response
end

#succeed!(status: 200, headers: {}, type: nil, **options) ⇒ Object

Succeed the request and immediately respond.



141
142
143
144
145
146
147
148
149
150
# File 'lib/utopia/controller/base.rb', line 141

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_sObject



58
59
60
# File 'lib/utopia/controller/base.rb', line 58

def to_s
	"\#<#{self.class}>"
end