Class: Zephyre::Controller
Instance Attribute Summary collapse
-
#request ⇒ Object
readonly
Returns the value of attribute request.
Class Method Summary collapse
Instance Method Summary collapse
- #controller_name ⇒ Object
- #dispatch(action) ⇒ Object
- #get_response ⇒ Object
-
#initialize(env) ⇒ Controller
constructor
A new instance of Controller.
- #params ⇒ Object
- #render(*args) ⇒ Object
- #render_template(view_name, locals = {}) ⇒ Object
- #response(body, status = 200, header = {}) ⇒ Object
Constructor Details
#initialize(env) ⇒ Controller
Returns a new instance of Controller.
5 6 7 |
# File 'lib/zephyre/controller.rb', line 5 def initialize(env) @request ||= Rack::Request.new(env) end |
Instance Attribute Details
#request ⇒ Object (readonly)
Returns the value of attribute request.
3 4 5 |
# File 'lib/zephyre/controller.rb', line 3 def request @request end |
Class Method Details
.action(action_name) ⇒ Object
53 54 55 |
# File 'lib/zephyre/controller.rb', line 53 def self.action(action_name) -> (env) { self.new(env).dispatch(action_name) } end |
Instance Method Details
#controller_name ⇒ Object
38 39 40 |
# File 'lib/zephyre/controller.rb', line 38 def controller_name self.class.to_s.gsub(/Controller$/, "").to_snake_case end |
#dispatch(action) ⇒ Object
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/zephyre/controller.rb', line 42 def dispatch(action) content = self.send(action) if get_response get_response else render(action) get_response end end |
#get_response ⇒ Object
17 18 19 |
# File 'lib/zephyre/controller.rb', line 17 def get_response @response end |
#params ⇒ Object
9 10 11 |
# File 'lib/zephyre/controller.rb', line 9 def params request.params end |
#render(*args) ⇒ Object
21 22 23 |
# File 'lib/zephyre/controller.rb', line 21 def render(*args) response(render_template(*args), 200, {"Content-Type" => "text/html"}) end |
#render_template(view_name, locals = {}) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/zephyre/controller.rb', line 25 def render_template(view_name, locals = {}) filename = File.join("app", "views", controller_name, "#{view_name}.erb") template = File.read(filename) vars = {} instance_variables.each do |var| key = var.to_s.gsub("@", "").to_sym vars[key] = instance_variable_get(var) end ERB.new(template).result(binding) end |
#response(body, status = 200, header = {}) ⇒ Object
13 14 15 |
# File 'lib/zephyre/controller.rb', line 13 def response(body, status=200, header={}) @response = Rack::Response.new(body, status, header) end |