Class: Wings::Controller

Inherits:
Object show all
Includes:
Model
Defined in:
lib/wings/controller.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ Controller

Returns a new instance of Controller.



18
19
20
21
22
# File 'lib/wings/controller.rb', line 18

def initialize(env)
  @env     = env
  @request = Rack::Request.new(env)
  @params  = request.params
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



10
11
12
# File 'lib/wings/controller.rb', line 10

def env
  @env
end

#paramsObject (readonly)

Returns the value of attribute params.



10
11
12
# File 'lib/wings/controller.rb', line 10

def params
  @params
end

#requestObject (readonly)

Returns the value of attribute request.



10
11
12
# File 'lib/wings/controller.rb', line 10

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



10
11
12
# File 'lib/wings/controller.rb', line 10

def response
  @response
end

Class Method Details

.action(act, rp = {}) ⇒ Object

Respond to Rack The action will be used like a Rack app



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

def self.action(act, rp = {})
  proc { |e| self.new(e).dispatch(act, rp) }
end

Instance Method Details

#dispatch(action, routing_params = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/wings/controller.rb', line 24

def dispatch(action, routing_params = {})
  @params.merge!(routing_params)

  self.send(action)

  if response
    [response.status, response.headers, response.body]
  else
    render(action)
  end
end

#render(template, **opts) ⇒ Object

options for render

locals: local variables status: status code headers: headers



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

def render(template, **opts)
  raise 'You can only render once!' if @response

  v = View.new(params_for_view(template, opts))
  @response = Rack::Response.new(v.text, v.status, v.headers)
end