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.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ Controller

initialize the controller

This creates a new controller instance using the defined classes of renderer, request and response.

Parameters:

  • env (Hash)

    a rack compatible environment



51
52
53
54
55
# File 'lib/zero/controller.rb', line 51

def initialize(env)
  @request  = self.class.request.new(env)
  @response = self.class.response.new
  @renderer = self.class.renderer
end

Instance Attribute Details

#rendererObject (readonly)

the renderer which can be used to render templates



44
45
46
# File 'lib/zero/controller.rb', line 44

def renderer
  @renderer
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(env).response
end

.rendererObject

get the renderer set in the controller



39
40
41
# File 'lib/zero/controller.rb', line 39

def self.renderer
  @@renderer
end

.renderer=(renderer) ⇒ Object

set the renderer to use in the controller



34
35
36
# File 'lib/zero/controller.rb', line 34

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

.requestObject

return the set request class



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

def self.request
  @@request ||= Zero::Request
end

.request=(request_class) ⇒ Object

set a class to use for requests



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

def self.request=(request_class)
  @@request = request_class
end

.responseObject

return the set response class



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

def self.response
  @@response ||= Zero::Response
end

.response=(response_class) ⇒ Object

set the class to use for responses



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

def self.response=(response_class)
  @@response = response_class
end

Instance Method Details

#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



62
63
64
65
66
# File 'lib/zero/controller.rb', line 62

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