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 ⇒ Object
Default root path of the controller for requests.
Instance Method Summary collapse
-
#attachment(filename = nil, disposition = :attachment) ⇒ Object
Set the Content-Disposition to "attachment" with the specified filename, instructing the user agents to prompt to save.
-
#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
11 12 13 |
# File 'lib/flame/controller.rb', line 11 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
83 84 85 86 |
# File 'lib/flame/controller.rb', line 83 def method_missing(m, *args, &block) return super unless @dispatcher.respond_to?(m) @dispatcher.send(m, *args, &block) end |
Class Method Details
.default_path ⇒ Object
Default root path of the controller for requests
99 100 101 102 103 104 |
# File 'lib/flame/controller.rb', line 99 def default_path modules = name.underscore.split('/') parts = modules[-1].split('_') - %w(index controller ctrl) return modules[-2] if parts.empty? parts.join('_') end |
Instance Method Details
#attachment(filename = nil, disposition = :attachment) ⇒ Object
Set the Content-Disposition to "attachment" with the specified filename, instructing the user agents to prompt to save.
40 41 42 43 44 45 46 47 |
# File 'lib/flame/controller.rb', line 40 def (filename = nil, disposition = :attachment) content_dis = 'Content-Disposition'.freeze response[content_dis] = disposition.to_s return unless filename response[content_dis] << "; filename=\"#{File.basename(filename)}\"" ext = File.extname(filename) content_type(ext) unless response[Rack::CONTENT_TYPE] || ext.empty? end |
#execute(method) ⇒ Object
Execute the method of the controller with hooks (may be overloaded)
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/flame/controller.rb', line 65 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
16 17 18 19 |
# File 'lib/flame/controller.rb', line 16 def path_to(*args) add_controller_class(args) @dispatcher.path_to(*args) end |
#redirect(path) ⇒ Object #redirect(*args) ⇒ Object
Redirect for response
32 33 34 35 36 |
# File 'lib/flame/controller.rb', line 32 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)
53 54 55 56 57 58 59 60 |
# File 'lib/flame/controller.rb', line 53 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 |