Class: Stream::MultiPart

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

Instance Method Summary collapse

Constructor Details

#initialize(parts) ⇒ MultiPart

Returns a new instance of MultiPart.



3
4
5
6
7
# File 'lib/multi_part/stream.rb', line 3

def initialize(parts)
  @parts = parts
  @part_no = 0
  @part_offset = 0
end

Instance Method Details

#read(how_much) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/multi_part/stream.rb', line 17

def read (how_much)
  return nil if @part_no >= @parts.size
  
  how_much_current_part = @parts[@part_no].size - @part_offset
  
  how_much_current_part = if how_much_current_part > how_much
    how_much
  else
    how_much_current_part
  end
  
  how_much_next_part = how_much - how_much_current_part
  current_part = @parts[@part_no].read(@part_offset, how_much_current_part )
  
  if how_much_next_part > 0
    @part_no += 1
    @part_offset = 0
    next_part = read( how_much_next_part  )
    current_part + if next_part
      next_part
    else
      ''
    end
  else
    @part_offset += how_much_current_part
    current_part
  end
end

#sizeObject



9
10
11
12
13
14
15
# File 'lib/multi_part/stream.rb', line 9

def size
  total = 0
  @parts.each do |part|
    total += part.size
  end
  total
end