Class: Utopia::Middleware::Controller::Base

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.actionsObject



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

def actions
	@actions ||= Action.new
end

.base_pathObject



28
29
30
# File 'lib/utopia/middleware/controller/base.rb', line 28

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

.controllerObject



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

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

.direct?(path) ⇒ Boolean

Returns:

  • (Boolean)


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

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

.lookup(path) ⇒ Object



61
62
63
# File 'lib/utopia/middleware/controller/base.rb', line 61

def lookup(path)
	possible_actions = actions.select((path - uri_path).components)
end

.method_added(name) ⇒ Object



65
66
67
68
69
70
# File 'lib/utopia/middleware/controller/base.rb', line 65

def method_added(name)
	if name.match(/^on_(.*)$/)
		warn "Method #{name} using legacy definition mechanism."
		on($1.split("_").join('/'), unbound: true, &name)
	end
end

.on(path, options = {}, &block) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/utopia/middleware/controller/base.rb', line 53

def on(path, options = {}, &block)
	if Symbol === path
		path = ['**', path]
	end
	
	actions.define(Path[path].components, options, &block)
end

.require_local(path) ⇒ Object



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

def require_local(path)
	require File.join(base_path, path)
end

.uri_pathObject



32
33
34
# File 'lib/utopia/middleware/controller/base.rb', line 32

def self.uri_path
	self.const_get(:URI_PATH)
end

Instance Method Details

#call(env) ⇒ Object



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

def call(env)
	self.class.controller.app.call(env)
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.



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

def copy_instance_variables(from)
	from.instance_variables.each do |name|
		instance_variable_set(name, from.instance_variable_get(name))
	end
end

#fail!(error = :bad_request) ⇒ Object



130
131
132
# File 'lib/utopia/middleware/controller/base.rb', line 130

def fail!(error = :bad_request)
	respond! error
end

#ignore!Object



118
119
120
# File 'lib/utopia/middleware/controller/base.rb', line 118

def ignore!
	throw :response, nil
end

#lookup(path) ⇒ Object

Given a path, look up all matched actions.



74
75
76
# File 'lib/utopia/middleware/controller/base.rb', line 74

def lookup(path)
	self.class.lookup(path)
end

#passthrough(request, path) ⇒ Object

Given a request, call associated actions if at least one exists.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/utopia/middleware/controller/base.rb', line 79

def passthrough(request, path)
	actions = lookup(path)
	
	if actions.size > 0
		variables = request.controller
		controller_clone = self.clone
		
		variables << controller_clone
		
		response = catch(:response) do
			# By default give nothing - i.e. keep on processing:
			actions.each do |action|
				action.invoke!(controller_clone, request, path)
			end and nil
		end
		
		if response
			return controller_clone.respond_with(*response)
		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.



177
178
179
180
# File 'lib/utopia/middleware/controller/base.rb', line 177

def process!(request, path)
	# puts "process! #{request} #{path}"
	passthrough(request, path)
end

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



122
123
124
# File 'lib/utopia/middleware/controller/base.rb', line 122

def redirect! (target, status = 302)
	respond! :redirect => target.to_str, :status => status
end

#respond!(*args) ⇒ Object



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

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

#respond_with(*args) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/utopia/middleware/controller/base.rb', line 138

def respond_with(*args)
	return args[0] if args[0] == nil || Array === args[0]

	status = 200
	options = nil

	if Numeric === args[0] || Symbol === args[0]
		status = args[0]
		options = args[1] || {}
	else
		options = args[0]
		status = options[:status] || status
	end

	status = Utopia::HTTP::STATUS_CODES[status] || status
	headers = options[:headers] || {}

	if options[:type]
		headers['Content-Type'] ||= options[:type]
	end

	if options[:redirect]
		headers["Location"] = options[:redirect]
		status = 302 if status < 300 || status >= 400
	end

	body = []
	if options[:body]
		body = options[:body]
	elsif options[:content]
		body = [options[:content]]
	elsif status >= 300
		body = [Utopia::HTTP::STATUS_DESCRIPTIONS[status] || "Status #{status}"]
	end

	return [status, headers, body]
end

#rewrite!(location) ⇒ Object



126
127
128
# File 'lib/utopia/middleware/controller/base.rb', line 126

def rewrite! location
	throw :rewrite, location.to_str
end

#success!(*args) ⇒ Object



134
135
136
# File 'lib/utopia/middleware/controller/base.rb', line 134

def success!(*args)
	respond! :success, *args
end