Class: Raw::Context

Inherits:
Object
  • Object
show all
Includes:
Request, Response
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

Attributes included from Response

#response_cookies, #response_headers, #status

Attributes included from Request

#get_params, #headers, #in, #post_params

Class Method Summary collapse

Instance Method Summary collapse

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

#applicationObject

The application of this context. Contains configuration parameters.



29
30
31
# File 'lib/raw/context.rb', line 29

def application
  @application
end

#cookiesObject



49
50
51
# File 'lib/raw/test/context.rb', line 49

def cookies
  @cookies || @cookies = {}
end

#formatObject

The resource representation format for this request.



47
48
49
# File 'lib/raw/context.rb', line 47

def format
  @format
end

#levelObject

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_bufferObject

The output buffer accumulates the generated output.



51
52
53
# File 'lib/raw/context.rb', line 51

def output_buffer
  @output_buffer
end

#sessionObject

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

.currentObject

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

#closeObject 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

#dispatcherObject

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, options = {})
  AttributeUtils.populate_object(obj, params, options)
end

#globalObject

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.

Returns:

  • (Boolean)


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