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
- #success!(*args) ⇒ Object
Constructor Details
#initialize(controller) ⇒ Base
Returns a new instance of Base.
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/utopia/middleware/controller.rb', line 99 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
232 233 234 |
# File 'lib/utopia/middleware/controller.rb', line 232 def self.base_path self.const_get(:BASE_PATH) end |
.require_local(path) ⇒ Object
240 241 242 |
# File 'lib/utopia/middleware/controller.rb', line 240 def self.require_local(path) require File.join(base_path, path) end |
.uri_path ⇒ Object
236 237 238 |
# File 'lib/utopia/middleware/controller.rb', line 236 def self.uri_path self.const_get(:URI_PATH) end |
Instance Method Details
#action(path, options = {}, &block) ⇒ Object
116 117 118 119 120 121 122 123 124 |
# File 'lib/utopia/middleware/controller.rb', line 116 def action(path, = {}, &block) cur = @_actions path.reverse.each do |name| cur = cur[name] ||= {} end cur[:action] = Proc.new(&block) end |
#call(env) ⇒ Object
165 166 167 |
# File 'lib/utopia/middleware/controller.rb', line 165 def call(env) @_controller.app.call(env) end |
#fail!(error = :bad_request) ⇒ Object
181 182 183 |
# File 'lib/utopia/middleware/controller.rb', line 181 def fail!(error = :bad_request) respond! error end |
#ignore! ⇒ Object
173 174 175 |
# File 'lib/utopia/middleware/controller.rb', line 173 def ignore! throw :response, nil end |
#lookup(path) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/utopia/middleware/controller.rb', line 126 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.
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/utopia/middleware/controller.rb', line 141 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
228 229 230 |
# File 'lib/utopia/middleware/controller.rb', line 228 def process!(path, request) passthrough(path, request) end |
#redirect!(target, status = 302) ⇒ Object
177 178 179 |
# File 'lib/utopia/middleware/controller.rb', line 177 def redirect! (target, status = 302) respond! :redirect => target, :status => status end |
#respond!(*args) ⇒ Object
169 170 171 |
# File 'lib/utopia/middleware/controller.rb', line 169 def respond!(*args) throw :response, args end |
#respond_with(*args) ⇒ Object
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/utopia/middleware/controller.rb', line 189 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 |
#success!(*args) ⇒ Object
185 186 187 |
# File 'lib/utopia/middleware/controller.rb', line 185 def success!(*args) respond! :success, *args end |