Class: Zero::Controller

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

Overview

abstract class to make creation of controllers easier

This abstract class creates an interface to make it easier to write rack compatible controllers. It catches #call and creates a new instance with the environment and calls #render on it.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request) ⇒ Controller

initialize the controller

Parameters:

  • request (Request)

    a request object



30
31
32
33
# File 'lib/zero/controller.rb', line 30

def initialize(request)
  @request  = request
  @response = Zero::Response.new
end

Class Method Details

.call(env) ⇒ Object

initialize a new instance of the controller and call response on it



9
10
11
# File 'lib/zero/controller.rb', line 9

def self.call(env)
  new(Zero::Request.create(env)).response
end

.rendererObject

get the renderer set in the controller



19
20
21
# File 'lib/zero/controller.rb', line 19

def self.renderer
  @@renderer
end

.renderer=(renderer) ⇒ Object

set the renderer to use in the controller



14
15
16
# File 'lib/zero/controller.rb', line 14

def self.renderer=(renderer)
  @@renderer = renderer
end

Instance Method Details

#rendererObject

a small helper to get the actual renderer



24
25
26
# File 'lib/zero/controller.rb', line 24

def renderer
  self.class.renderer
end

#responseObject

build the response and return it

This method calls #process if it was defined so make it easier to process the request before rendering stuff.

Returns:

  • Response a rack conform response



40
41
42
43
44
# File 'lib/zero/controller.rb', line 40

def response
  process if respond_to?(:process)
  render
  @response.to_a
end