class Rasti::Web::Controller

Attributes

render[R]
request[R]
response[R]

Public Class Methods

>>(action_name)
Alias for: action
action(action_name) click to toggle source
# 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
Also aliased as: >>
after_action(action_name=nil, &block) click to toggle source
# 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() click to toggle source
# 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) click to toggle source
# 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() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# File lib/rasti/web/controller.rb, line 82
def handled_exceptions
  @handled_exceptions ||= ClassAncestrySort.desc exception_handlers.keys
end
new(request, response, render) click to toggle source
# File lib/rasti/web/controller.rb, line 12
def initialize(request, response, render)
  @request = request
  @response = response
  @render = render
end
rescue_from(exception_class, &block) click to toggle source
# File lib/rasti/web/controller.rb, line 46
def rescue_from(exception_class, &block)
  exception_handlers[exception_class] = block
end