Class: Orbit::Controller

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request) ⇒ Controller

Returns a new instance of Controller.



5
6
7
8
# File 'lib/orbit/controller.rb', line 5

def initialize(request)
  @request = request
  @response = Orbit::Config.response_class.new
end

Instance Attribute Details

#requestObject (readonly)

Returns the value of attribute request.



3
4
5
# File 'lib/orbit/controller.rb', line 3

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



3
4
5
# File 'lib/orbit/controller.rb', line 3

def response
  @response
end

Class Method Details

.add_route(verb, action, &handler) ⇒ Object



85
86
87
88
89
90
91
92
93
94
# File 'lib/orbit/controller.rb', line 85

def self.add_route(verb, action, &handler)
  route = "#{verb.downcase}_#{action}"

  if routes.include?(route)
    update_method(verb, action, &handler)
  else
    routes.push(route)
    create_route(verb, action, &handler)
  end
end

.base_pathObject



57
58
59
# File 'lib/orbit/controller.rb', line 57

def self.base_path
  @base_path ||= '/'
end

.create_route(verb, action, &handler) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/orbit/controller.rb', line 96

def self.create_route(verb, action, &handler)
  method_name = create_method(verb, action, &handler)

  full_path = "#{@base_path}/#{action}"

  Orbit::Router.add(verb, full_path, self, method_name)
end

.delete(action, &handler) ⇒ Object



77
78
79
# File 'lib/orbit/controller.rb', line 77

def self.delete(action, &handler)
  add_route("DELETE", action, &handler)
end

.execute_action(request, action) ⇒ Object



18
19
20
# File 'lib/orbit/controller.rb', line 18

def self.execute_action(request, action)
  new(request).execute_action(action)
end

.get(action, &handler) ⇒ Object



61
62
63
# File 'lib/orbit/controller.rb', line 61

def self.get(action, &handler)
  add_route("GET", action, &handler)
end

.head(action, &handler) ⇒ Object



81
82
83
# File 'lib/orbit/controller.rb', line 81

def self.head(action, &handler)
  add_route("HEAD", action, &handler)
end

.layout(_layout = nil) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/orbit/controller.rb', line 10

def self.layout(_layout = nil)
  if _layout
    @layout = "#{Dir.pwd}/#{_layout}"
  else
    @layout
  end
end

.patch(action, &handler) ⇒ Object



73
74
75
# File 'lib/orbit/controller.rb', line 73

def self.patch(action, &handler)
  add_route("PATCH", action, &handler)
end

.path(path) ⇒ Object



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

def self.path(path)
  @base_path ||= superclass != Orbit::Controller ? superclass.base_path : ''
  @base_path += path
end

.post(action, &handler) ⇒ Object



65
66
67
# File 'lib/orbit/controller.rb', line 65

def self.post(action, &handler)
  add_route("POST", action, &handler)
end

.put(action, &handler) ⇒ Object



69
70
71
# File 'lib/orbit/controller.rb', line 69

def self.put(action, &handler)
  add_route("PUT", action, &handler)
end

.routesObject



53
54
55
# File 'lib/orbit/controller.rb', line 53

def self.routes
  @routes ||= []
end

Instance Method Details



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

def cookie_domain
  request.host
end

#cookiesObject



108
109
110
# File 'lib/orbit/controller.rb', line 108

def cookies
  @_cookies ||= Session::Cookie.new(self, request.cookies)
end

#execute_action(action) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/orbit/controller.rb', line 22

def execute_action(action)
  result = send(action)

  result = result.to_s if result.respond_to? :to_s

  response.tap do |res|
    res.body = Array(result)
  end
end

#headersObject



120
121
122
# File 'lib/orbit/controller.rb', line 120

def headers
  request.headers
end

#last_request_update_allowed?Boolean

Returns:

  • (Boolean)


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

def last_request_update_allowed?
  true
end

#localsObject



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

def locals
  @_locals ||= TemplateBinding.new(params)
end

#paramsObject



116
117
118
# File 'lib/orbit/controller.rb', line 116

def params
  @request.params
end

#render(template) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/orbit/controller.rb', line 32

def render(template)
  root_path = Dir.pwd

  template_location = "#{root_path}/#{template}"

  templates = [template_location, self.class.layout].compact

  templates.inject(nil) do | prev, template |
    _render(template) { prev }
  end
end

#responds_to_last_request_update_allowed?Boolean

Returns:

  • (Boolean)


132
133
134
# File 'lib/orbit/controller.rb', line 132

def responds_to_last_request_update_allowed?
  true
end

#sessionObject



104
105
106
# File 'lib/orbit/controller.rb', line 104

def session
  @request.session
end

#statusObject



124
125
126
# File 'lib/orbit/controller.rb', line 124

def status
  response.status
end

#status=(code) ⇒ Object



128
129
130
# File 'lib/orbit/controller.rb', line 128

def status=(code)
  response.status = code
end