Class: Rack::Utils::Context

Inherits:
Proc
  • Object
show all
Defined in:
lib/gems/rack-0.4.0/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.



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

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.



128
129
130
# File 'lib/gems/rack-0.4.0/lib/rack/utils.rb', line 128

def app
  @app
end

#forObject (readonly)

Returns the value of attribute for.



128
129
130
# File 'lib/gems/rack-0.4.0/lib/rack/utils.rb', line 128

def for
  @for
end

Instance Method Details

#context(app_r) ⇒ Object



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

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



137
138
139
# File 'lib/gems/rack-0.4.0/lib/rack/utils.rb', line 137

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

#old_inspectObject



127
# File 'lib/gems/rack-0.4.0/lib/rack/utils.rb', line 127

alias_method :old_inspect, :inspect

#pretty_print(pp) ⇒ Object



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

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