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
Defined Under Namespace
Modules: ParentActions
Class Method Summary collapse
-
.actions ⇒ Object
Shortcut for not-inherited public methods: actions.
-
.default_path ⇒ Object
Default root path of the controller for requests.
-
.with_actions ⇒ Object
Re-define public instance method from parent.
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.
-
#respond_to_missing?(m) ⇒ Boolean
Respond to Dispatcher methods.
-
#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
16 17 18 |
# File 'lib/flame/controller.rb', line 16 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`
96 97 98 99 |
# File 'lib/flame/controller.rb', line 96 def method_missing(m, *args, &block) return super unless @dispatcher.respond_to?(m) @dispatcher.send(m, *args, &block) end |
Class Method Details
.actions ⇒ Object
Shortcut for not-inherited public methods: actions
10 11 12 |
# File 'lib/flame/controller.rb', line 10 def self.actions public_instance_methods(false) end |
.default_path ⇒ Object
Default root path of the controller for requests
117 118 119 120 121 122 |
# File 'lib/flame/controller.rb', line 117 def default_path modules = name.underscore.split('/') parts = modules[-1].split('_') - %w(index controller ctrl) return modules[-2] if parts.empty? parts.join('_') end |
.with_actions ⇒ Object
Re-define public instance method from parent
128 129 130 |
# File 'lib/flame/controller.rb', line 128 def with_actions @with_actions ||= Class.new(self) { extend ParentActions } 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.
51 52 53 54 55 56 57 58 |
# File 'lib/flame/controller.rb', line 51 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)
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/flame/controller.rb', line 78 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
21 22 23 24 |
# File 'lib/flame/controller.rb', line 21 def path_to(*args) add_controller_class(args) @dispatcher.path_to(*args) end |
#redirect(path) ⇒ Object #redirect(*args) ⇒ Object
Redirect for response
43 44 45 46 47 |
# File 'lib/flame/controller.rb', line 43 def redirect(*params) response.redirect( params[0].is_a?(String) ? params[0] : path_to(*params) ) end |
#respond_to_missing?(m) ⇒ Boolean
Respond to Dispatcher methods
102 103 104 |
# File 'lib/flame/controller.rb', line 102 def respond_to_missing?(m, *) @dispatcher.respond_to?(m) || super end |
#url_to(*args) ⇒ Object
Build a URI to the given controller and action, or path
27 28 29 30 |
# File 'lib/flame/controller.rb', line 27 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)
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/flame/controller.rb', line 64 def view(path = nil, = {}) cache = .delete(:cache) cache = config[:environment] == 'production' if cache.nil? template = Flame::Render.new( self, (path || caller_locations(1, 1)[0].label.to_sym), ) template.render(cache: cache) end |