Class: Racket::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/racket/controller.rb

Overview

Base controller class. Your controllers should inherit this class.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.after(*methods, &blk) ⇒ nil

Adds a before hook to one or more actions. Actions should be given as a list of symbols. If no symbols are provided, all actions on the controller is affected.

Parameters:

  • methods (Array)

Returns:

  • (nil)


49
50
51
# File 'lib/racket/controller.rb', line 49

def self.after(*methods, &blk)
  __register_hook(:after, methods, blk) if block_given?
end

.before(*methods, &blk) ⇒ nil

Adds an after hook to one or more actions. Actions should be given as a list of symbols. If no symbols are provided, all actions on the controller is affected.

Parameters:

  • methods (Array)

Returns:

  • (nil)


58
59
60
# File 'lib/racket/controller.rb', line 58

def self.before(*methods, &blk)
  __register_hook(:before, methods, blk) if block_given?
end

.get_option(key) ⇒ Object

Returns an option for the current controller class or any of the controller classes it is inheriting from.

Parameters:

  • key (Symbol)

    The option to retrieve

Returns:

  • (Object)


72
73
74
75
76
77
78
# File 'lib/racket/controller.rb', line 72

def self.get_option(key)
  @options ||= {}
  return @options[key] if @options.key?(key)
  # We are running out of controller options, do one final lookup in Application.options
  return Application.options.fetch(key, nil) if superclass == Controller
  superclass.get_option(key)
end

.inherited(klass) ⇒ Object

:nodoc:



63
64
65
# File 'lib/racket/controller.rb', line 63

def self.inherited(klass)
  Application.options[:last_added_controller].push(klass)
end

.set_option(key, value) ⇒ Object

Sets an option for the current controller class.

Parameters:

  • key (Symbol)
  • value (Object)


84
85
86
87
# File 'lib/racket/controller.rb', line 84

def self.set_option(key, value)
  @options ||= {}
  @options[key] = value
end

Instance Method Details

#controller_option(key) ⇒ Object

Returns an option from the current controller class.

Parameters:

  • key (Symbol)

Returns:



93
94
95
# File 'lib/racket/controller.rb', line 93

def controller_option(key)
  self.class.get_option(key)
end

#r(controller, action, *params) ⇒ String

Returns a route to an action within another controller.

Parameters:

  • controller (Class)
  • action (Symbol)
  • params (Array)

Returns:

  • (String)


112
113
114
# File 'lib/racket/controller.rb', line 112

def r(controller, action, *params)
  Application.get_route(controller, action, params)
end

#redirect(target, status = 302, run_after_hook = true) ⇒ Object

Redirects the client. After hooks are

Parameters:

  • target (String)

    URL to redirect to

  • status (Fixnum) (defaults to: 302)

    HTTP status to send

  • run_after_hook (true|false) (defaults to: true)

    Whether after hook should be run before redirecting

Returns:

  • (Object)


122
123
124
125
# File 'lib/racket/controller.rb', line 122

def redirect(target, status = 302, run_after_hook = true)
  response.redirect(target, status)
  respond(response.status, response.headers, '', run_after_hook);
end

#render(action) ⇒ String

Renders an action.

Parameters:

  • action (Symbol)

Returns:

  • (String)


144
145
146
147
# File 'lib/racket/controller.rb', line 144

def render(action)
  __execute(action)
  Application.view_cache.render(self)
end

#respond(status = 200, headers = {}, body = '', run_after_hook = false) ⇒ Object

Stop processing request and send a custom response. After calling this method, no further processing of the request is done unless run_after_hook is set to true. If run_after_hook is set to true, any after hook associated with the action (but no other code) will be run.

Parameters:

  • status (Fixnum) (defaults to: 200)
  • headers (Hash) (defaults to: {})
  • body (String) (defaults to: '')
  • run_after_hook (true|false) (defaults to: false)

    Whether after hook should be run



135
136
137
138
# File 'lib/racket/controller.rb', line 135

def respond(status = 200, headers = {}, body = '', run_after_hook = false)
  __run_hook(:after, racket.action) if run_after_hook
  throw :response, [status, headers, body]
end

#rs(action, *params) ⇒ String

Returns a route to an action within the current controller.

Parameters:

  • action (Symbol)
  • params (Array)

Returns:

  • (String)


102
103
104
# File 'lib/racket/controller.rb', line 102

def rs(action, *params)
  Application.get_route(self.class, action, params)
end