Class: Raw::Context
- Inherits:
-
Object
- Object
- Raw::Context
- Defined in:
- lib/raw/context.rb,
lib/raw/test/context.rb
Overview
Override the default Context implementation to include methods useful for testing.
Instance Attribute Summary collapse
-
#application ⇒ Object
The application of this context.
- #cookies ⇒ Object
-
#format ⇒ Object
The resource representation format for this request.
-
#level ⇒ Object
The rendering level.
-
#output_buffer ⇒ Object
The output buffer accumulates the generated output.
-
#session ⇒ Object
Lazy lookup of the session to avoid costly cookie lookup when not needed.
Attributes included from Response
#response_cookies, #response_headers, #status
Attributes included from Request
#get_params, #headers, #in, #post_params
Class Method Summary collapse
-
.current ⇒ Object
Returns the context for the current thread.
Instance Method Summary collapse
-
#close ⇒ Object
(also: #finish)
Close the context, should be called at the end of the HTTP request handling code.
-
#dispatcher ⇒ Object
Access the dispactcher.
-
#fill(obj, options = {}) ⇒ Object
(also: #populate, #assign)
Automagically populate an object from request parameters.
-
#global ⇒ Object
Access global variables.
-
#initialize(application) ⇒ Context
constructor
A new instance of Context.
-
#is_top_level? ⇒ Boolean
(also: #top?, #is_top?, #top_level?)
Is the current action the top level action? The level of the top action is 1.
-
#no_sync! ⇒ Object
Don’t sync session.
Methods included from Response
#add_cookie, #body, #content_type, #content_type=, #redirect?, #redirect_uri, #response_cookie, #status_ok?
Methods included from Request
#[], #[]=, #content_length, #domain, #false?, #fetch, #formatted_post?, #has_key?, #host, #host_uri, #keys, #local?, #local_net?, #method, #params, #params=, #path_info, #port, #post_format, #protocol, #query_string, #raw_body, #referer, #remote_ip, #ssl?, #subdomains, #true?, #uri, #user_agent, #xml_http_request?, #xml_post?, #yaml_post?
Constructor Details
#initialize(application) ⇒ Context
Returns a new instance of Context.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/raw/context.rb', line 53 def initialize(application) @level = 0 @application = application @post_params = {} @get_params = {} @response_headers = {} @output_buffer = "" @status = Http::STATUS_OK # Store this instance in a thread local variable for easy # access. Thread.current[:CURRENT_CONTEXT] = self end |
Instance Attribute Details
#application ⇒ Object
The application of this context. Contains configuration parameters.
29 30 31 |
# File 'lib/raw/context.rb', line 29 def application @application end |
#cookies ⇒ Object
49 50 51 |
# File 'lib/raw/test/context.rb', line 49 def @cookies || @cookies = {} end |
#format ⇒ Object
The resource representation format for this request.
47 48 49 |
# File 'lib/raw/context.rb', line 47 def format @format end |
#level ⇒ Object
The rendering level. An action may include sub-actions, each time the action is called, the level is increased, when the action returns the level decreases. The first action, called top level action has a level of 1.
43 44 45 |
# File 'lib/raw/context.rb', line 43 def level @level end |
#output_buffer ⇒ Object
The output buffer accumulates the generated output.
51 52 53 |
# File 'lib/raw/context.rb', line 51 def output_buffer @output_buffer end |
#session ⇒ Object
Lazy lookup of the session to avoid costly cookie lookup when not needed.
36 37 38 |
# File 'lib/raw/context.rb', line 36 def session @session end |
Class Method Details
.current ⇒ Object
Returns the context for the current thread.
148 149 150 |
# File 'lib/raw/context.rb', line 148 def self.current Thread.current[:CURRENT_CONTEXT] end |
Instance Method Details
#close ⇒ Object Also known as: finish
Close the context, should be called at the end of the HTTP request handling code.
81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/raw/context.rb', line 81 def close if @session # Ugly hack: need to use AOP instead if @session.has_key?(:FLASH) @session[:FLASH].clean end # INVESTIGATE: is this needed? @session.sync unless @no_sync end end |
#dispatcher ⇒ Object
Access the dispactcher
96 97 98 |
# File 'lib/raw/context.rb', line 96 def dispatcher @application.dispatcher end |
#fill(obj, options = {}) ⇒ Object Also known as: populate, assign
Automagically populate an object from request parameters. This is a truly dangerous method.
Options
-
name
-
force_boolean
Example
request.fill(User.new)
This method is deprecated, Prefer to use the following form:
User.new.assign_with(request)
130 131 132 |
# File 'lib/raw/context.rb', line 130 def fill(obj, = {}) AttributeUtils.populate_object(obj, params, ) end |
#global ⇒ Object
Access global variables. In a distributed server scenario, these variables can reside outside of the process.
110 111 112 |
# File 'lib/raw/context.rb', line 110 def global return Global end |
#is_top_level? ⇒ Boolean Also known as: top?, is_top?, top_level?
Is the current action the top level action? The level of the top action is 1.
139 140 141 |
# File 'lib/raw/context.rb', line 139 def is_top_level? @level == 1 end |
#no_sync! ⇒ Object
Don’t sync session. This method may be needed in low level hacks with the session code. For example if you updade an entity (managed) object that is cached in the session, you may dissable the syncing to avoid resetting the old value after the sync.
74 75 76 |
# File 'lib/raw/context.rb', line 74 def no_sync! @no_sync = true end |