Class: Rasti::Web::Controller
- Inherits:
-
Object
- Object
- Rasti::Web::Controller
- Extended by:
- Forwardable
- Defined in:
- lib/rasti/web/controller.rb
Instance Attribute Summary collapse
-
#render ⇒ Object
readonly
Returns the value of attribute render.
-
#request ⇒ Object
readonly
Returns the value of attribute request.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Class Method Summary collapse
- .action(action_name) ⇒ Object (also: >>)
- .after_action(action_name = nil, &block) ⇒ Object
- .after_action_hooks ⇒ Object
- .before_action(action_name = nil, &block) ⇒ Object
- .before_action_hooks ⇒ Object
- .call_after_action(controller, action_name) ⇒ Object
- .call_before_action(controller, action_name) ⇒ Object
- .call_exception_handler(controller, exception) ⇒ Object
- .exception_handlers ⇒ Object
- .handled_exceptions ⇒ Object
- .rescue_from(exception_class, &block) ⇒ Object
Instance Method Summary collapse
-
#initialize(request, response, render) ⇒ Controller
constructor
A new instance of Controller.
Constructor Details
#initialize(request, response, render) ⇒ Controller
Returns a new instance of Controller.
12 13 14 15 16 |
# File 'lib/rasti/web/controller.rb', line 12 def initialize(request, response, render) @request = request @response = response @render = render end |
Instance Attribute Details
#render ⇒ Object (readonly)
Returns the value of attribute render.
10 11 12 |
# File 'lib/rasti/web/controller.rb', line 10 def render @render end |
#request ⇒ Object (readonly)
Returns the value of attribute request.
10 11 12 |
# File 'lib/rasti/web/controller.rb', line 10 def request @request end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
10 11 12 |
# File 'lib/rasti/web/controller.rb', line 10 def response @response end |
Class Method Details
.action(action_name) ⇒ Object Also known as: >>
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/rasti/web/controller.rb', line 20 def action(action_name) raise "Undefined action '#{action_name}' in #{name}" unless instance_methods.include? action_name.to_sym Endpoint.new do |req, res, render| controller = new req, res, render begin call_before_action controller, action_name controller.public_send action_name rescue => ex call_exception_handler controller, ex ensure call_after_action controller, action_name end end end |
.after_action(action_name = nil, &block) ⇒ Object
42 43 44 |
# File 'lib/rasti/web/controller.rb', line 42 def after_action(action_name=nil, &block) after_action_hooks[action_name] = block end |
.after_action_hooks ⇒ Object
74 75 76 |
# File 'lib/rasti/web/controller.rb', line 74 def after_action_hooks @after_action_hooks ||= superclass.respond_to?(:after_action_hooks) ? superclass.after_action_hooks.dup : {} end |
.before_action(action_name = nil, &block) ⇒ Object
38 39 40 |
# File 'lib/rasti/web/controller.rb', line 38 def before_action(action_name=nil, &block) before_action_hooks[action_name] = block end |
.before_action_hooks ⇒ Object
70 71 72 |
# File 'lib/rasti/web/controller.rb', line 70 def before_action_hooks @before_action_hooks ||= superclass.respond_to?(:before_action_hooks) ? superclass.before_action_hooks.dup : {} end |
.call_after_action(controller, action_name) ⇒ Object
55 56 57 58 |
# File 'lib/rasti/web/controller.rb', line 55 def call_after_action(controller, action_name) hook = after_action_hooks[action_name] || after_action_hooks[nil] controller.instance_exec action_name, &hook if hook end |
.call_before_action(controller, action_name) ⇒ Object
50 51 52 53 |
# File 'lib/rasti/web/controller.rb', line 50 def call_before_action(controller, action_name) hook = before_action_hooks[action_name] || before_action_hooks[nil] controller.instance_exec action_name, &hook if hook end |
.call_exception_handler(controller, exception) ⇒ Object
60 61 62 63 64 65 66 67 68 |
# File 'lib/rasti/web/controller.rb', line 60 def call_exception_handler(controller, exception) exception_class = handled_exceptions.detect { |klass| exception.is_a? klass } exception_handler = exception_handlers[exception_class] if exception_handler controller.instance_exec exception, &exception_handler else raise exception end end |
.exception_handlers ⇒ Object
78 79 80 |
# File 'lib/rasti/web/controller.rb', line 78 def exception_handlers @exception_handlers ||= superclass.respond_to?(:exception_handlers) ? superclass.exception_handlers.dup : {} end |
.handled_exceptions ⇒ Object
82 83 84 |
# File 'lib/rasti/web/controller.rb', line 82 def handled_exceptions @handled_exceptions ||= ClassAncestrySort.desc exception_handlers.keys end |
.rescue_from(exception_class, &block) ⇒ Object
46 47 48 |
# File 'lib/rasti/web/controller.rb', line 46 def rescue_from(exception_class, &block) exception_handlers[exception_class] = block end |