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.
-
#url_to(*args) ⇒ Object
Build a URI to the given controller and action, or path.
-
#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`
89 90 91 92 |
# File 'lib/flame/controller.rb', line 89 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
105 106 107 108 109 110 |
# File 'lib/flame/controller.rb', line 105 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.
46 47 48 49 50 51 52 53 |
# File 'lib/flame/controller.rb', line 46 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)
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/flame/controller.rb', line 71 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
38 39 40 41 42 |
# File 'lib/flame/controller.rb', line 38 def redirect(*params) response.redirect( params[0].is_a?(String) ? params[0] : path_to(*params) ) end |
#url_to(*args) ⇒ Object
Build a URI to the given controller and action, or path
22 23 24 25 |
# File 'lib/flame/controller.rb', line 22 def url_to(*args) path = args.first.is_a?(String) ? args.first : path_to(*args) "#{request.scheme}://#{request.host_with_port}#{path}" end |
#view(path = nil, options = {}) ⇒ String Also known as: render
Render a template with ‘Flame::Render` (based on Tilt-engine)
59 60 61 62 63 64 65 66 |
# File 'lib/flame/controller.rb', line 59 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 |