Class: PseudoIO

Inherits:
Object
  • Object
show all
Defined in:
lib/dropbox_uploader.rb

Overview

This class provides a pseudo read-only IO, constructed from some strings and ios.

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ PseudoIO

PseudoIO.new(string or array, …)

string : an instance of String array : [stream, size]



189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/dropbox_uploader.rb', line 189

def initialize(*args)
  @totalsize = 0
  @ios = []
  args.each do |arg|
    if arg.is_a? String
      @ios << StringIO.new(arg)
      @totalsize += arg.size
    else
      @ios << arg[0]
      @totalsize += arg[1]
    end
  end
end

Instance Method Details

#read(size = nil, result = nil) ⇒ Object

pseudoIo.read([size, [buffer]]) -> string, buffer, or nil

See IO#read.



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/dropbox_uploader.rb', line 218

def read(size = nil, result = nil)
  if result
    result.replace('')
  else
    result = ''
  end
  if size
    return nil if @ios.empty?
    until @ios.empty?
      cur = @ios.first.read(size)
      if cur
        result << cur
        break if cur.size >= size
        size -= cur.size
      end
      @ios.shift.close
    end
  else
    return "" if @ios.empty?
    until @ios.empty?
      io = @ios.shift
      result << io.read
      io.close
    end
  end
  result
end

#sizeObject Also known as: length

pseudoIo.size -> integer

Returns the total size of strings and streams



208
209
210
# File 'lib/dropbox_uploader.rb', line 208

def size
  @totalsize
end