Class: Peictt::Controller
Constant Summary collapse
- DEFAULT_LAYOUT =
"application".freeze
Class Attribute Summary collapse
-
.layout(layout_name = nil) ⇒ Object
Returns the value of attribute layout.
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
- #params ⇒ Object
- #redirect_to(url) ⇒ Object
- #render(*args) ⇒ Object
- #render_html(template) ⇒ Object
- #render_json(template) ⇒ Object
- #render_template(template) ⇒ Object
- #response(body, status = 200, headers = {}) ⇒ Object
- #session ⇒ Object
Class Attribute Details
.layout(layout_name = nil) ⇒ Object
Returns the value of attribute layout.
7 8 9 |
# File 'lib/peictt/controller.rb', line 7 def layout @layout end |
Instance Attribute Details
#request ⇒ Object (readonly)
Returns the value of attribute request.
4 5 6 |
# File 'lib/peictt/controller.rb', line 4 def request @request end |
Class Method Details
.action(action_name) ⇒ Object
10 11 12 |
# File 'lib/peictt/controller.rb', line 10 def self.action(action_name) -> (_env) { new.dispatch(action_name) } end |
.asset_file(type, filename) ⇒ Object
36 37 38 |
# File 'lib/peictt/controller.rb', line 36 def self.asset_file(type, filename) File.read File.join(APP_ROOT, "app", "assets", type, filename.to_s) end |
.get_asset(filename) ⇒ Object
26 27 28 29 30 31 32 33 34 |
# File 'lib/peictt/controller.rb', line 26 def self.get_asset(filename) file = "" if /^[a-z_]+\.css$/ =~ filename file = asset_file("css", filename) elsif /^[a-z_]+\.js$/ =~ filename file = asset_file("js", filename) end Rack::Response.new(file, 200, "Content-Type" => "text/css") end |
Instance Method Details
#controller_name ⇒ Object
84 85 86 |
# File 'lib/peictt/controller.rb', line 84 def controller_name self.class.to_s.gsub(/Controller$/, "").to_snake_case end |
#dispatch(action) ⇒ Object
88 89 90 91 92 93 |
# File 'lib/peictt/controller.rb', line 88 def dispatch(action) @action = action send(action) render(action) unless get_response get_response end |
#get_response ⇒ Object
48 49 50 |
# File 'lib/peictt/controller.rb', line 48 def get_response @response end |
#params ⇒ Object
52 53 54 |
# File 'lib/peictt/controller.rb', line 52 def params Peictt::Application.params end |
#redirect_to(url) ⇒ Object
40 41 42 |
# File 'lib/peictt/controller.rb', line 40 def redirect_to(url) response([], 302, "Location" => url) end |
#render(*args) ⇒ Object
60 61 62 63 64 |
# File 'lib/peictt/controller.rb', line 60 def render(*args) headers = Builder::HttpHeader.new(args.dup) template = Builder::Template.new(args.dup, controller_name, @action) response(render_template(template), headers.status, headers.headers) end |
#render_html(template) ⇒ Object
74 75 76 77 78 |
# File 'lib/peictt/controller.rb', line 74 def render_html(template) self.class.layout.render(self, template.locals) do Tilt::HamlTemplate.new(template.body).render(self, template.locals) end end |
#render_json(template) ⇒ Object
80 81 82 |
# File 'lib/peictt/controller.rb', line 80 def render_json(template) Parser::JSON.new(File.read(template.body)).render(self, template.locals) end |
#render_template(template) ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/peictt/controller.rb', line 66 def render_template(template) if template.html? || template.text? return render_html(template) else return render_json(template) end end |
#response(body, status = 200, headers = {}) ⇒ Object
44 45 46 |
# File 'lib/peictt/controller.rb', line 44 def response(body, status = 200, headers = {}) @response = Rack::Response.new(body, status, headers) end |
#session ⇒ Object
56 57 58 |
# File 'lib/peictt/controller.rb', line 56 def session Peictt::Application.session end |