Class: Jimmu::Context
- Inherits:
-
Object
- Object
- Jimmu::Context
- Defined in:
- lib/jimmu.rb
Overview
Context is instantiated once per HTTP request. It is the self
against which ERB templates, layouts, and before/after hook blocks
all run (via instance_eval / a bound top-level binding), which is
what makes bare DSL calls like params, redirect "/login", or
session[:user] work naturally inside a .erb file.
Instance Attribute Summary collapse
-
#error ⇒ Object
Returns the value of attribute error.
-
#response_body ⇒ Object
Returns the value of attribute response_body.
-
#response_headers ⇒ Object
readonly
Returns the value of attribute response_headers.
-
#session_id ⇒ Object
readonly
Returns the value of attribute session_id.
-
#status_code ⇒ Object
readonly
Returns the value of attribute status_code.
Instance Method Summary collapse
- #cookie ⇒ Object
- #db ⇒ Object
- #header(name, value) ⇒ Object
- #html(str) ⇒ Object
-
#initialize(application, request, cookie_jar, session_id, session_hash, new_session) ⇒ Context
constructor
A new instance of Context.
- #json(data) ⇒ Object
- #layout(enabled) ⇒ Object
- #log(message) ⇒ Object
-
#params ⇒ Object
---- DSL: read accessors -------------------------------------------.
-
#redirect(location, code = 302) ⇒ Object
---- DSL: response-terminating actions ------------------------------ Each of these fully determines the response and immediately unwinds out of whatever ERB template (or before-hook) called it, via
throw Jimmu::HALT. - #render(name) ⇒ Object
-
#render_error_page(code) ⇒ Object
Renders
views/<code>.erbif present (through the normal ERB+layout pipeline, so it has full access to params/session/etc), falling back to a built-in page if it is missing or itself raises. -
#render_file(path) ⇒ Object
Renders the .erb file at
path, wrapping it in views/layout.erb unless the template disabled that vialayout false. - #request ⇒ Object
-
#serve_static_file(path) ⇒ Object
Renders a static file's bytes directly, without ERB or layout.
- #session ⇒ Object
-
#status(code) ⇒ Object
---- DSL: response building -----------------------------------------.
- #text(str) ⇒ Object
Constructor Details
#initialize(application, request, cookie_jar, session_id, session_hash, new_session) ⇒ Context
Returns a new instance of Context.
684 685 686 687 688 689 690 691 692 693 694 695 696 |
# File 'lib/jimmu.rb', line 684 def initialize(application, request, , session_id, session_hash, new_session) @application = application @request = request @cookie_jar = @session_id = session_id @session_hash = session_hash @new_session = new_session @status_code = 200 @response_headers = {} @response_body = nil @use_layout = true @error = nil end |
Instance Attribute Details
#error ⇒ Object
Returns the value of attribute error.
682 683 684 |
# File 'lib/jimmu.rb', line 682 def error @error end |
#response_body ⇒ Object
Returns the value of attribute response_body.
682 683 684 |
# File 'lib/jimmu.rb', line 682 def response_body @response_body end |
#response_headers ⇒ Object (readonly)
Returns the value of attribute response_headers.
681 682 683 |
# File 'lib/jimmu.rb', line 681 def response_headers @response_headers end |
#session_id ⇒ Object (readonly)
Returns the value of attribute session_id.
681 682 683 |
# File 'lib/jimmu.rb', line 681 def session_id @session_id end |
#status_code ⇒ Object (readonly)
Returns the value of attribute status_code.
681 682 683 |
# File 'lib/jimmu.rb', line 681 def status_code @status_code end |
Instance Method Details
#cookie ⇒ Object
708 709 710 |
# File 'lib/jimmu.rb', line 708 def @cookie_jar end |
#db ⇒ Object
733 734 735 |
# File 'lib/jimmu.rb', line 733 def db @application.default_db end |
#header(name, value) ⇒ Object
723 724 725 726 |
# File 'lib/jimmu.rb', line 723 def header(name, value) @response_headers[name.to_s] = value.to_s nil end |
#html(str) ⇒ Object
768 769 770 771 772 |
# File 'lib/jimmu.rb', line 768 def html(str) @response_headers['Content-Type'] = 'text/html; charset=utf-8' @response_body = str.to_s throw HALT end |
#json(data) ⇒ Object
756 757 758 759 760 |
# File 'lib/jimmu.rb', line 756 def json(data) @response_headers['Content-Type'] = 'application/json; charset=utf-8' @response_body = data.to_json throw HALT end |
#layout(enabled) ⇒ Object
737 738 739 740 |
# File 'lib/jimmu.rb', line 737 def layout(enabled) @use_layout = enabled ? true : false nil end |
#log(message) ⇒ Object
728 729 730 731 |
# File 'lib/jimmu.rb', line 728 def log() @application.log() nil end |
#params ⇒ Object
---- DSL: read accessors -------------------------------------------
700 701 702 |
# File 'lib/jimmu.rb', line 700 def params @request.params end |
#redirect(location, code = 302) ⇒ Object
---- DSL: response-terminating actions ------------------------------
Each of these fully determines the response and immediately
unwinds out of whatever ERB template (or before-hook) called it,
via throw Jimmu::HALT. The dispatch loop in Application wraps
request handling in catch(Jimmu::HALT), so this jump is always
caught -- see Application#dispatch.
749 750 751 752 753 754 |
# File 'lib/jimmu.rb', line 749 def redirect(location, code = 302) @status_code = code @response_headers['Location'] = location.to_s @response_body = '' throw HALT end |
#render(name) ⇒ Object
774 775 776 777 778 |
# File 'lib/jimmu.rb', line 774 def render(name) @response_body = render_file(resolve_view_path(name)) @response_headers['Content-Type'] ||= 'text/html; charset=utf-8' throw HALT end |
#render_error_page(code) ⇒ Object
Renders views/<code>.erb if present (through the normal
ERB+layout pipeline, so it has full access to params/session/etc),
falling back to a built-in page if it is missing or itself
raises.
793 794 795 796 797 798 799 800 801 802 803 804 |
# File 'lib/jimmu.rb', line 793 def render_error_page(code) @status_code = code path = resolve_view_path(code.to_s) if File.file?(path) begin return render_file(path) rescue Exception => e @application.log("[jimmu] error while rendering views/#{code}.erb: #{e.class}: #{e.}") end end built_in_error_page(code) end |
#render_file(path) ⇒ Object
Renders the .erb file at path, wrapping it in views/layout.erb
unless the template disabled that via layout false. This is
used both for the initially-matched route and by the render
DSL method above.
810 811 812 813 814 815 816 817 818 819 820 821 822 |
# File 'lib/jimmu.rb', line 810 def render_file(path) raise Jimmu::Error, "Template not found: #{path}" unless File.file?(path) @use_layout = true content = evaluate_erb(read_template(path)) return content unless @use_layout layout_path = File.join(@application.views_dir, 'layout.erb') return content unless File.file?(layout_path) layout_src = read_template(layout_path) wrap_with_layout(content, layout_src) end |
#request ⇒ Object
704 705 706 |
# File 'lib/jimmu.rb', line 704 def request @request end |
#serve_static_file(path) ⇒ Object
Renders a static file's bytes directly, without ERB or layout.
783 784 785 786 787 |
# File 'lib/jimmu.rb', line 783 def serve_static_file(path) @response_body = File.binread(path) @response_headers['Content-Type'] = MIME_TYPES[File.extname(path).downcase] nil end |
#session ⇒ Object
712 713 714 |
# File 'lib/jimmu.rb', line 712 def session @session_hash end |
#status(code) ⇒ Object
---- DSL: response building -----------------------------------------
718 719 720 721 |
# File 'lib/jimmu.rb', line 718 def status(code) @status_code = code.to_i nil end |
#text(str) ⇒ Object
762 763 764 765 766 |
# File 'lib/jimmu.rb', line 762 def text(str) @response_headers['Content-Type'] = 'text/plain; charset=utf-8' @response_body = str.to_s throw HALT end |