Class: Rack::RewindableInput

Inherits:
Object
  • Object
show all
Defined in:
rack/rack/rewindable_input.rb

Overview

Class which can make any IO object rewindable, including non-rewindable ones. It does this by buffering the data into a tempfile, which is rewindable.

rack.input is required to be rewindable, so if your input stream IO is non-rewindable by nature (e.g. a pipe or a socket) then you can wrap it in an object of this class to easily make it rewindable.

Don't forget to call #close when you're done. This frees up temporary resources that RewindableInput uses, though it does not close the original IO object.

Defined Under Namespace

Classes: Tempfile

Instance Method Summary (collapse)

Constructor Details

- (RewindableInput) initialize(io)

Returns a new instance of RewindableInput



16
17
18
19
20
# File 'rack/rack/rewindable_input.rb', line 16

def initialize(io)
  @io = io
  @rewindable_io = nil
  @unlinked = false
end

Instance Method Details

- (void) close

Closes this RewindableInput object without closing the originally wrapped IO oject. Cleans up any temporary resources that this RewindableInput has created.

This method may be called multiple times. It does nothing on subsequent calls.



47
48
49
50
51
52
53
54
55
56
# File 'rack/rack/rewindable_input.rb', line 47

def close
  if @rewindable_io
    if @unlinked
      @rewindable_io.close
    else
      @rewindable_io.close!
    end
    @rewindable_io = nil
  end
end

- (void) each(&block)



32
33
34
35
# File 'rack/rack/rewindable_input.rb', line 32

def each(&block)
  make_rewindable unless @rewindable_io
  @rewindable_io.each(&block)
end

- (void) gets



22
23
24
25
# File 'rack/rack/rewindable_input.rb', line 22

def gets
  make_rewindable unless @rewindable_io
  @rewindable_io.gets
end

- (void) read(*args)



27
28
29
30
# File 'rack/rack/rewindable_input.rb', line 27

def read(*args)
  make_rewindable unless @rewindable_io
  @rewindable_io.read(*args)
end

- (void) rewind



37
38
39
40
# File 'rack/rack/rewindable_input.rb', line 37

def rewind
  make_rewindable unless @rewindable_io
  @rewindable_io.rewind
end