Class: Rack::RewindableInput

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

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: Middleware

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ RewindableInput

Returns a new instance of RewindableInput.



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

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

Instance Method Details

#closeObject

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

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



68
69
70
71
72
73
74
75
76
77
# File 'lib/rack/rewindable_input.rb', line 68

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

#each(&block) ⇒ Object



48
49
50
51
# File 'lib/rack/rewindable_input.rb', line 48

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

#getsObject



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

def gets
  make_rewindable unless @rewindable_io
  @rewindable_io.gets
end

#read(*args) ⇒ Object



43
44
45
46
# File 'lib/rack/rewindable_input.rb', line 43

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

#rewindObject



53
54
55
56
# File 'lib/rack/rewindable_input.rb', line 53

def rewind
  make_rewindable unless @rewindable_io
  @rewindable_io.rewind
end

#sizeObject



58
59
60
61
# File 'lib/rack/rewindable_input.rb', line 58

def size
  make_rewindable unless @rewindable_io
  @rewindable_io.size
end