Class: Orbit::Controller
- Inherits:
-
Object
- Object
- Orbit::Controller
- Defined in:
- lib/orbit/controller.rb
Instance Attribute Summary collapse
-
#request ⇒ Object
readonly
Returns the value of attribute request.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Class Method Summary collapse
- .add_route(verb, action, &handler) ⇒ Object
- .base_path ⇒ Object
- .create_route(verb, action, &handler) ⇒ Object
- .delete(action, &handler) ⇒ Object
- .execute_action(request, action) ⇒ Object
- .get(action, &handler) ⇒ Object
- .head(action, &handler) ⇒ Object
- .layout(_layout = nil) ⇒ Object
- .patch(action, &handler) ⇒ Object
- .path(path) ⇒ Object
- .post(action, &handler) ⇒ Object
- .put(action, &handler) ⇒ Object
- .routes ⇒ Object
Instance Method Summary collapse
- #execute_action(action) ⇒ Object
-
#initialize(request) ⇒ Controller
constructor
A new instance of Controller.
- #locals ⇒ Object
- #render(template) ⇒ Object
Constructor Details
#initialize(request) ⇒ Controller
Returns a new instance of Controller.
5 6 7 8 |
# File 'lib/orbit/controller.rb', line 5 def initialize(request) @request = request @response = Orbit::Config.response_class.new end |
Instance Attribute Details
#request ⇒ Object (readonly)
Returns the value of attribute request.
3 4 5 |
# File 'lib/orbit/controller.rb', line 3 def request @request end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
3 4 5 |
# File 'lib/orbit/controller.rb', line 3 def response @response end |
Class Method Details
.add_route(verb, action, &handler) ⇒ Object
85 86 87 88 89 90 91 92 93 94 |
# File 'lib/orbit/controller.rb', line 85 def self.add_route(verb, action, &handler) route = "#{verb.downcase}_#{action}" if routes.include?(route) update_method(verb, action, &handler) else routes.push(route) create_route(verb, action, &handler) end end |
.base_path ⇒ Object
57 58 59 |
# File 'lib/orbit/controller.rb', line 57 def self.base_path @base_path ||= '/' end |
.create_route(verb, action, &handler) ⇒ Object
96 97 98 99 100 101 102 |
# File 'lib/orbit/controller.rb', line 96 def self.create_route(verb, action, &handler) method_name = create_method(verb, action, &handler) full_path = "#{@base_path}/#{action}" Orbit::Router.add(verb, full_path, self, method_name) end |
.delete(action, &handler) ⇒ Object
77 78 79 |
# File 'lib/orbit/controller.rb', line 77 def self.delete(action, &handler) add_route("DELETE", action, &handler) end |
.execute_action(request, action) ⇒ Object
18 19 20 |
# File 'lib/orbit/controller.rb', line 18 def self.execute_action(request, action) new(request).execute_action(action) end |
.get(action, &handler) ⇒ Object
61 62 63 |
# File 'lib/orbit/controller.rb', line 61 def self.get(action, &handler) add_route("GET", action, &handler) end |
.head(action, &handler) ⇒ Object
81 82 83 |
# File 'lib/orbit/controller.rb', line 81 def self.head(action, &handler) add_route("HEAD", action, &handler) end |
.layout(_layout = nil) ⇒ Object
10 11 12 13 14 15 16 |
# File 'lib/orbit/controller.rb', line 10 def self.layout(_layout = nil) if _layout @layout = "#{Dir.pwd}/#{_layout}" else @layout end end |
.patch(action, &handler) ⇒ Object
73 74 75 |
# File 'lib/orbit/controller.rb', line 73 def self.patch(action, &handler) add_route("PATCH", action, &handler) end |
.path(path) ⇒ Object
48 49 50 51 |
# File 'lib/orbit/controller.rb', line 48 def self.path(path) @base_path ||= superclass != Orbit::Controller ? superclass.base_path : '' @base_path += path end |
.post(action, &handler) ⇒ Object
65 66 67 |
# File 'lib/orbit/controller.rb', line 65 def self.post(action, &handler) add_route("POST", action, &handler) end |
.put(action, &handler) ⇒ Object
69 70 71 |
# File 'lib/orbit/controller.rb', line 69 def self.put(action, &handler) add_route("PUT", action, &handler) end |
.routes ⇒ Object
53 54 55 |
# File 'lib/orbit/controller.rb', line 53 def self.routes @routes ||= [] end |
Instance Method Details
#execute_action(action) ⇒ Object
22 23 24 25 26 27 28 29 30 |
# File 'lib/orbit/controller.rb', line 22 def execute_action(action) result = send(action) result = result.to_s if result.respond_to? :to_s response.tap do |res| res.body = Array(result) end end |
#locals ⇒ Object
44 45 46 |
# File 'lib/orbit/controller.rb', line 44 def locals @_locals ||= TemplateBinding.locals(params) end |
#render(template) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/orbit/controller.rb', line 32 def render(template) root_path = Dir.pwd template_location = "#{root_path}/#{template}" templates = [template_location, self.class.layout].compact templates.inject(nil) do | prev, template | _render(template) { prev } end end |