Class: Utopia::Middleware::Controller::Base
- Defined in:
- lib/utopia/middleware/controller.rb
Class Method Summary collapse
Instance Method Summary collapse
- #action(path, options = {}, &block) ⇒ Object
- #call(env) ⇒ Object
- #fail!(error = :bad_request) ⇒ Object
- #ignore! ⇒ Object
-
#initialize(controller) ⇒ Base
constructor
A new instance of Base.
- #lookup(path) ⇒ Object
-
#passthrough(path, request) ⇒ Object
Given a request, call an associated action if one exists.
- #process!(path, request) ⇒ Object
- #redirect!(target, status = 302) ⇒ Object
- #respond!(*args) ⇒ Object
- #respond_with(*args) ⇒ Object
Constructor Details
#initialize(controller) ⇒ Base
Returns a new instance of Base.
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/utopia/middleware/controller.rb', line 84 def initialize(controller) @_controller = controller @_actions = {} methods.each do |method_name| next unless method_name.match(/on_(.*)$/) action($1.split("_")) do |path, request| # LOG.debug("Controller: #{method_name}") self.send(method_name, path, request) # Don't pass the result back, instead use pass! or respond! nil end end end |
Class Method Details
.base_path ⇒ Object
213 214 215 |
# File 'lib/utopia/middleware/controller.rb', line 213 def self.base_path self.const_get(:BASE_PATH) end |
.require_local(path) ⇒ Object
221 222 223 |
# File 'lib/utopia/middleware/controller.rb', line 221 def self.require_local(path) require File.join(base_path, path) end |
.uri_path ⇒ Object
217 218 219 |
# File 'lib/utopia/middleware/controller.rb', line 217 def self.uri_path self.const_get(:URI_PATH) end |
Instance Method Details
#action(path, options = {}, &block) ⇒ Object
101 102 103 104 105 106 107 108 109 |
# File 'lib/utopia/middleware/controller.rb', line 101 def action(path, = {}, &block) cur = @_actions path.reverse.each do |name| cur = cur[name] ||= {} end cur[:action] = Proc.new(&block) end |
#call(env) ⇒ Object
150 151 152 |
# File 'lib/utopia/middleware/controller.rb', line 150 def call(env) @_controller.app.call(env) end |
#fail!(error = :bad_request) ⇒ Object
166 167 168 |
# File 'lib/utopia/middleware/controller.rb', line 166 def fail!(error = :bad_request) respond! error end |
#ignore! ⇒ Object
158 159 160 |
# File 'lib/utopia/middleware/controller.rb', line 158 def ignore! throw :response, nil end |
#lookup(path) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/utopia/middleware/controller.rb', line 111 def lookup(path) cur = @_actions path.components.reverse.each do |name| cur = cur[name] return nil if cur == nil if action = cur[:action] return action end end end |
#passthrough(path, request) ⇒ Object
Given a request, call an associated action if one exists.
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/utopia/middleware/controller.rb', line 126 def passthrough(path, request) action = lookup(path) if action variables = request.controller clone = self.dup variables << clone response = catch(:response) do clone.instance_exec(path, request, &action) # By default give nothing - i.e. keep on processing: nil end if response return clone.respond_with(*response) end end return nil end |
#process!(path, request) ⇒ Object
209 210 211 |
# File 'lib/utopia/middleware/controller.rb', line 209 def process!(path, request) passthrough(path, request) end |
#redirect!(target, status = 302) ⇒ Object
162 163 164 |
# File 'lib/utopia/middleware/controller.rb', line 162 def redirect! (target, status = 302) respond! :redirect => target, :status => status end |
#respond!(*args) ⇒ Object
154 155 156 |
# File 'lib/utopia/middleware/controller.rb', line 154 def respond! (*args) throw :response, args end |
#respond_with(*args) ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/utopia/middleware/controller.rb', line 170 def respond_with(*args) return args[0] if args[0] == nil || Array === args[0] status = 200 = nil if Numeric === args[0] || Symbol === args[0] status = args[0] = args[1] || {} else = args[0] status = [:status] || status end status = Utopia::HTTP_STATUS_CODES[status] || status headers = [:headers] || {} if [:type] headers['Content-Type'] ||= [:type] end if [:redirect] headers["Location"] = [:redirect] status = 302 if status < 300 || status >= 400 end body = [] if [:body] body = [:body] elsif [:content] body = [[:content]] elsif status >= 300 body = [Utopia::HTTP_STATUS_DESCRIPTIONS[status] || 'Status #{status}'] end # Utopia::LOG.debug([status, headers, body].inspect) return [status, headers, body] end |