Class: Rack::BodyProxy
| Relationships & Source Files | |
| Extension / Inclusion / Inheritance Descendants | |
| Subclasses: | |
| Inherits: | Object | 
| Defined in: | lib/rack/body_proxy.rb | 
Overview
Proxy for response bodies allowing calling a block when the response body is closed (after the response has been fully sent to the client).
Class Method Summary
- 
    
      .new(body, &block)  ⇒ BodyProxy 
    
    constructor
    Set the response body to wrap, and the block to call when the response has been fully sent. 
Instance Attribute Summary
- 
    
      #closed?  ⇒ Boolean 
    
    readonly
    Whether the proxy is closed. 
Instance Method Summary
- 
    
      #close  
    
    If not already closed, close the wrapped body and then call the block the proxy was initialized with. 
- 
    
      #method_missing(method_name, *args, &block)  
    
    Delegate missing methods to the wrapped body. 
- 
    
      #respond_to_missing?(method_name, include_all = false)  ⇒ Boolean 
    
    Return whether the wrapped body responds to the method. 
Constructor Details
    .new(body, &block)  ⇒ BodyProxy 
  
Set the response body to wrap, and the block to call when the response has been fully sent.
# File 'lib/rack/body_proxy.rb', line 10
def initialize(body, &block) @body = body @block = block @closed = false end
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block)
Delegate missing methods to the wrapped body.
# File 'lib/rack/body_proxy.rb', line 45
def method_missing(method_name, *args, &block) case method_name when :to_str super when :to_ary begin @body.__send__(method_name, *args, &block) ensure close end else @body.__send__(method_name, *args, &block) end end
Instance Attribute Details
    #closed?  ⇒ Boolean  (readonly)
  
Whether the proxy is closed. The proxy starts as not closed, and becomes closed on the first call to close.
# File 'lib/rack/body_proxy.rb', line 40
def closed? @closed end
Instance Method Details
#close
If not already closed, close the wrapped body and then call the block the proxy was initialized with.
# File 'lib/rack/body_proxy.rb', line 28
def close return if @closed @closed = true begin @body.close if @body.respond_to?(:close) ensure @block.call end end
    #respond_to_missing?(method_name, include_all = false)  ⇒ Boolean 
  
Return whether the wrapped body responds to the method.
# File 'lib/rack/body_proxy.rb', line 17
def respond_to_missing?(method_name, include_all = false) case method_name when :to_str false else super or @body.respond_to?(method_name, include_all) end end