Class: Utopia::Controller::Base
- Inherits:
-
Object
- Object
- Utopia::Controller::Base
- Defined in:
- lib/utopia/controller/base.rb
Class Method Summary collapse
- .actions ⇒ Object
- .base_path ⇒ Object
- .controller ⇒ Object
- .direct?(path) ⇒ Boolean
- .lookup(path) ⇒ Object
- .on(path, options = {}, &block) ⇒ Object
- .require_local(path) ⇒ Object
- .uri_path ⇒ Object
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#copy_instance_variables(from) ⇒ Object
Copy the instance variables from the previous controller to the next controller (usually only a few).
- #fail!(error = :bad_request) ⇒ Object
- #ignore! ⇒ Object
-
#lookup(path) ⇒ Object
Given a path, look up all matched actions.
-
#passthrough(request, path) ⇒ Object
Given a request, call associated actions if at least one exists.
-
#process!(request, path) ⇒ Object
Return nil if this controller didn’t do anything.
- #redirect!(target, status = 302) ⇒ Object
- #respond!(*args) ⇒ Object
- #respond_with(*args) ⇒ Object
- #rewrite!(location) ⇒ Object
- #success!(*args) ⇒ Object
Class Method Details
.actions ⇒ Object
47 48 49 |
# File 'lib/utopia/controller/base.rb', line 47 def actions @actions ||= Action.new end |
.base_path ⇒ Object
26 27 28 |
# File 'lib/utopia/controller/base.rb', line 26 def self.base_path self.const_get(:BASE_PATH) end |
.controller ⇒ Object
34 35 36 |
# File 'lib/utopia/controller/base.rb', line 34 def self.controller self.const_get(:CONTROLLER) end |
.direct?(path) ⇒ Boolean
43 44 45 |
# File 'lib/utopia/controller/base.rb', line 43 def direct?(path) path.dirname == uri_path end |
.lookup(path) ⇒ Object
59 60 61 |
# File 'lib/utopia/controller/base.rb', line 59 def lookup(path) possible_actions = actions.select((path - uri_path).components) end |
.on(path, options = {}, &block) ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/utopia/controller/base.rb', line 51 def on(path, = {}, &block) if Symbol === path path = ['**', path] end actions.define(Path.create(path).components, , &block) end |
.require_local(path) ⇒ Object
39 40 41 |
# File 'lib/utopia/controller/base.rb', line 39 def require_local(path) require File.join(base_path, path) end |
.uri_path ⇒ Object
30 31 32 |
# File 'lib/utopia/controller/base.rb', line 30 def self.uri_path self.const_get(:URI_PATH) end |
Instance Method Details
#call(env) ⇒ Object
101 102 103 |
# File 'lib/utopia/controller/base.rb', line 101 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.
95 96 97 98 99 |
# File 'lib/utopia/controller/base.rb', line 95 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
121 122 123 |
# File 'lib/utopia/controller/base.rb', line 121 def fail!(error = :bad_request) respond! error end |
#ignore! ⇒ Object
109 110 111 |
# File 'lib/utopia/controller/base.rb', line 109 def ignore! throw :response, nil end |
#lookup(path) ⇒ Object
Given a path, look up all matched actions.
65 66 67 |
# File 'lib/utopia/controller/base.rb', line 65 def lookup(path) self.class.lookup(path) end |
#passthrough(request, path) ⇒ Object
Given a request, call associated actions if at least one exists.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/utopia/controller/base.rb', line 70 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.
168 169 170 171 |
# File 'lib/utopia/controller/base.rb', line 168 def process!(request, path) # puts "process! #{request} #{path}" passthrough(request, path) end |
#redirect!(target, status = 302) ⇒ Object
113 114 115 |
# File 'lib/utopia/controller/base.rb', line 113 def redirect! (target, status = 302) respond! :redirect => target.to_str, :status => status end |
#respond!(*args) ⇒ Object
105 106 107 |
# File 'lib/utopia/controller/base.rb', line 105 def respond!(*args) throw :response, args end |
#respond_with(*args) ⇒ Object
129 130 131 132 133 134 135 136 137 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 |
# File 'lib/utopia/controller/base.rb', line 129 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 return [status, headers, body] end |
#rewrite!(location) ⇒ Object
117 118 119 |
# File 'lib/utopia/controller/base.rb', line 117 def rewrite! location throw :rewrite, location.to_str end |
#success!(*args) ⇒ Object
125 126 127 |
# File 'lib/utopia/controller/base.rb', line 125 def success!(*args) respond! :success, *args end |