Class: Nitro::Context

Inherits:
Object
  • Object
show all
Includes:
Render, Request, Response
Defined in:
lib/nitro/context.rb,
lib/nitro/test/context.rb

Overview

Override the default Context implementation to include methods useful for testing.

Instance Attribute Summary collapse

Attributes included from Render

#action_name, #context, #controller, #rendering_errors, #request, #response

Attributes included from Response

#response_cookies, #response_headers, #status

Attributes included from Request

#headers, #in, #params

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Render

#render, #send_file, #stream

Methods included from Response

#add_cookie, #content_type, #content_type=, #redirect?, #redirect_url, #response_cookie, #status_ok?

Methods included from Request

#[], #[]=, #action_params, #content_length, #domain, #false?, #fetch, #formatted_post?, #from_gecko?, #from_ie?, #from_khtml?, #from_mac?, #from_opera?, #from_os9?, #from_osx?, #from_unix?, #from_w3c?, #from_windows?, #has_key?, #host, #host_url, #local?, #local_net?, #method, #path, #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(conf) ⇒ Context

Returns a new instance of Context.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/nitro/context.rb', line 53

def initialize(conf)
  @level = 0
  @conf = conf
  @dispatcher = @conf.dispatcher
  @context = self

  # initialize response.
  
  @status = Http::STATUS_OK
  @response_headers = { 'Content-Type' => 'text/html' }

  # initialize the output buffer.

  @out ||= OutputBuffer.new
  
  # Store this instance in a thread local variable for easy
  # access.
  
  Thread.current[:CURRENT_CONTEXT] = self
end

Instance Attribute Details

#confObject

The configuration parameters.



33
34
35
# File 'lib/nitro/context.rb', line 33

def conf
  @conf
end

#cookiesObject



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

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

#dispatcherObject

The dispatcher.



44
45
46
# File 'lib/nitro/context.rb', line 44

def dispatcher
  @dispatcher
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.



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

def level
  @level
end

#sessionObject

Lazy lookup of the session to avoid costly cookie lookup when not needed.



40
41
42
# File 'lib/nitro/context.rb', line 40

def session
  @session
end

Class Method Details

.currentObject

Returns the context for the current thread.



182
183
184
# File 'lib/nitro/context.rb', line 182

def 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.



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/nitro/context.rb', line 88

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

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

Prefer to use the following form:

User.new.assign_with(request)



162
163
164
# File 'lib/nitro/context.rb', line 162

def fill(obj, options = {})
  AttributeUtils.populate_object(obj, @params, options)
end

#globalObject Also known as: application

Access global variables. In a distributed server scenario, these variables can reside outside of the process.



132
133
134
# File 'lib/nitro/context.rb', line 132

def global
  return Global
end

#is_top_level?Boolean Also known as: is_top?, in_top_level?, top_level?

Is the current action the top level action? The level of the top action is 1.

Returns:

  • (Boolean)


171
172
173
# File 'lib/nitro/context.rb', line 171

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.



81
82
83
# File 'lib/nitro/context.rb', line 81

def no_sync!
  @no_sync = true
end

#outObject

The accomulated output for the current context (ie the current request). – FIXME: still something more elegant/efficient. ++



107
108
109
110
111
112
113
114
# File 'lib/nitro/context.rb', line 107

def out
  return @out if @out == ''
  if @rendering_errors
    @out = ''
    render '/error'
  end
  @out
end

#serverObject

Access the server



118
119
120
# File 'lib/nitro/context.rb', line 118

def server
  @dispatcher.server
end