Class: CommunityTheatre::DisposableSpooler

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

Direct Known Subclasses

SplicingSpooler

Instance Method Summary collapse

Constructor Details

#initialize(dest, page_size: 100) ⇒ DisposableSpooler

Returns a new instance of DisposableSpooler.



7
8
9
10
# File 'lib/community_theatre/disposable_spooler.rb', line 7

def initialize(dest, page_size: 100)
  @page_size = page_size
  @destination = dest
end

Instance Method Details

#spool!(enumerable) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/community_theatre/disposable_spooler.rb', line 12

def spool!(enumerable)
  enumerable.each_slice(@page_size).each_with_index do |page, index|
    file_for_page(index) do |file|
      packer = MessagePack::Packer.new(file)
      packer.write_array_header(page.size)

      page.each { |item| packer.write(item) }

      packer.flush
    end
  end
end

#unspool(starting_at = 0) ⇒ Object

implementation of “starting_at” is only to the page level (see #pages)



27
28
29
30
31
32
33
34
35
36
# File 'lib/community_theatre/disposable_spooler.rb', line 27

def unspool(starting_at = 0)
  Enumerator.new do |yielder|
    pages(starting_at).each do |page_no|
      file_for_page(page_no, 'r') do |file|
        page = MessagePack::Unpacker.new(file).unpack
        page.each { |item| yielder << item }
      end
    end
  end
end