Class: Flame::Controller
- Inherits:
-
Object
- Object
- Flame::Controller
- Defined in:
- lib/flame/controller.rb
Overview
Class initialize when Dispatcher found route with it For new request and response
Class Method Summary collapse
-
.default_path(last = false) ⇒ Object
Default root path of the controller for requests.
Instance Method Summary collapse
-
#execute(method) ⇒ Object
Execute the method of the controller with hooks (may be overloaded).
-
#initialize(dispatcher) ⇒ Controller
constructor
Initialize the controller for request execution.
-
#method_missing(m, *args, &block) ⇒ Object
Call helpers methods from
Flame::Dispatcher. -
#path_to(*args) ⇒ Object
Helpers.
-
#redirect(*params) ⇒ Object
Redirect for response.
-
#view(path = nil, options = {}) ⇒ String
(also: #render)
Render a template with
Flame::Render(based on Tilt-engine).
Constructor Details
#initialize(dispatcher) ⇒ Controller
Initialize the controller for request execution
10 11 12 |
# File 'lib/flame/controller.rb', line 10 def initialize(dispatcher) @dispatcher = dispatcher end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args, &block) ⇒ Object
Call helpers methods from Flame::Dispatcher
71 72 73 74 |
# File 'lib/flame/controller.rb', line 71 def method_missing(m, *args, &block) return super unless @dispatcher.respond_to?(m) @dispatcher.send(m, *args, &block) end |
Class Method Details
.default_path(last = false) ⇒ Object
Default root path of the controller for requests
80 81 82 83 84 |
# File 'lib/flame/controller.rb', line 80 def default_path(last = false) (name.split('::').last.underscore.split('_') - %w(index controller ctrl)) .join('/').split('/') .unshift(nil)[(last ? -1 : 0)..-1].join('/') end |
Instance Method Details
#execute(method) ⇒ Object
Execute the method of the controller with hooks (may be overloaded)
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/flame/controller.rb', line 53 def execute(method) # send method body send( method, *params.values_at( *self.class.instance_method(method).parameters.map { |par| par[1] } ) ) rescue => exception # p 'rescue from controller' status 500 dump_error(exception) ## Re-raise exception for inherited controllers or `Flame::Dispatcher` raise exception end |
#path_to(*args) ⇒ Object
Helpers
15 16 17 18 |
# File 'lib/flame/controller.rb', line 15 def path_to(*args) args.unshift self.class if args[0].is_a? Symbol @dispatcher.path_to(*args) end |
#redirect(path) ⇒ Object #redirect(*args) ⇒ Object
Redirect for response
31 32 33 34 35 |
# File 'lib/flame/controller.rb', line 31 def redirect(*params) response.redirect( params[0].is_a?(String) ? params[0] : path_to(*params) ) end |
#view(path = nil, options = {}) ⇒ String Also known as: render
Render a template with Flame::Render (based on Tilt-engine)
41 42 43 44 45 46 47 48 |
# File 'lib/flame/controller.rb', line 41 def view(path = nil, = {}) template = Flame::Render.new( self, (path || caller_locations(1, 1)[0].label.to_sym), ) template.render(cache: config[:environment] == 'production') end |