Class: Rack::Utils::Context

Inherits:
Proc
  • Object
show all
Defined in:
lib/gems/rack-0.9.1/lib/rack/utils.rb

Overview

The recommended manner in which to implement a contexting application is to define a method #context in which a new Context is instantiated.

As a Context is a glorified block, it is highly recommended that you define the contextual block within the application’s operational scope. This would typically the application as you’re place into Rack’s stack.

class MyObject
  ...
  def context app
    Rack::Utils::Context.new app do |env|
      do_stuff
      response = app.call(env)
      do_more_stuff
    end
  end
  ...
end

mobj = MyObject.new app = mobj.context other_app Rack::Handler::Mongrel.new app

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_f, app_r) ⇒ Context

Returns a new instance of Context.



130
131
132
133
134
135
136
137
# File 'lib/gems/rack-0.9.1/lib/rack/utils.rb', line 130

def initialize app_f, app_r
  raise 'running context not provided' unless app_f
  raise 'running context does not respond to #context' unless app_f.respond_to? :context
  raise 'application context not provided' unless app_r
  raise 'application context does not respond to #call' unless app_r.respond_to? :call
  @for = app_f
  @app = app_r
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



129
130
131
# File 'lib/gems/rack-0.9.1/lib/rack/utils.rb', line 129

def app
  @app
end

#forObject (readonly)

Returns the value of attribute for.



129
130
131
# File 'lib/gems/rack-0.9.1/lib/rack/utils.rb', line 129

def for
  @for
end

Instance Method Details

#context(app_r) ⇒ Object



141
142
143
144
145
# File 'lib/gems/rack-0.9.1/lib/rack/utils.rb', line 141

def context app_r
  raise 'new application context not provided' unless app_r
  raise 'new application context does not respond to #call' unless app_r.respond_to? :call
  @for.context app_r
end

#inspectObject



138
139
140
# File 'lib/gems/rack-0.9.1/lib/rack/utils.rb', line 138

def inspect
  "#{old_inspect} ==> #{@for.inspect} ==> #{@app.inspect}"
end

#old_inspectObject



128
# File 'lib/gems/rack-0.9.1/lib/rack/utils.rb', line 128

alias_method :old_inspect, :inspect

#pretty_print(pp) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
# File 'lib/gems/rack-0.9.1/lib/rack/utils.rb', line 146

def pretty_print pp
  pp.text old_inspect
  pp.nest 1 do
    pp.breakable
    pp.text '=for> '
    pp.pp @for
    pp.breakable
    pp.text '=app> '
    pp.pp @app
  end
end