Class: Feet::Controller

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ Controller

Returns a new instance of Controller.



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

def initialize(env)
  @env = env
  @routing_params = {}
end

Class Method Details

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



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

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

Instance Method Details

#build_response(text, status = 200, headers = {}) ⇒ Object



27
28
29
30
31
32
# File 'lib/feet/controller.rb', line 27

def build_response(text, status = 200, headers = {})
  raise 'Already responded!' if @response

  a = [text].flatten
  @response = Rack::Response.new(a, status, headers)
end

#class_nameObject



42
43
44
# File 'lib/feet/controller.rb', line 42

def class_name
  self.class
end

#controller_nameObject



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

def controller_name
  # self.class.to_s.split('Controller').first.downcase
  klass = self.class
  klass = klass.to_s.gsub(/Controller$/, '')
  Feet.to_snake_case klass
end

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



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/feet/controller.rb', line 73

def dispatch(action, routing_params = {})
  @routing_params = routing_params

  text = send(action)
  if response
    [response.status, response.headers, [response.body].flatten]
  else
    [200, { 'Content-Type' => 'text/html' }, [text].flatten]
  end

end

#envObject



15
16
17
# File 'lib/feet/controller.rb', line 15

def env
  @env
end

#feet_versionObject



46
47
48
# File 'lib/feet/controller.rb', line 46

def feet_version
  Feet::VERSION
end

#instance_hashObject



57
58
59
60
61
# File 'lib/feet/controller.rb', line 57

def instance_hash
  instance_variables.each_with_object(Hash.new('')) do |iv, hash|
    hash[iv] = instance_variable_get iv
  end
end

#paramsObject



23
24
25
# File 'lib/feet/controller.rb', line 23

def params
  request.params.merge @routing_params
end

#render(view_name) ⇒ Object



63
64
65
66
67
# File 'lib/feet/controller.rb', line 63

def render(view_name)
  filename = File.join 'app', 'views', controller_name, "#{view_name}.html.erb"
  template = File.read filename
  View.new(template, instance_hash).call
end

#render_response(*args) ⇒ Object



38
39
40
# File 'lib/feet/controller.rb', line 38

def render_response(*args)
  build_response(render(*args))
end

#requestObject



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

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

#responseObject



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

def response
  @response
end