Class: Drugbank::Collector

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

Instance Method Summary collapse

Constructor Details

#initialize(batch_size, &block) ⇒ Collector

Returns a new instance of Collector.



3
4
5
6
7
8
# File 'lib/drugbank/collector.rb', line 3

def initialize(batch_size, &block)
  @batch_size = batch_size
  @counter = 0
  @batch = []
  self.block = block
end

Instance Method Details

#<<(value) ⇒ Object



10
11
12
13
14
# File 'lib/drugbank/collector.rb', line 10

def <<(value)
  @batch << value
  @counter += 1
  collect_batch if @counter % @batch_size == 0
end

#block=(block) ⇒ Object



22
23
24
# File 'lib/drugbank/collector.rb', line 22

def block=(block)
  @block = block
end

#collect_batchObject



16
17
18
19
20
# File 'lib/drugbank/collector.rb', line 16

def collect_batch
  process_batch unless @batch.empty?
  @batch = []
  puts "  #{@counter}"
end

#process_batchObject



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/drugbank/collector.rb', line 26

def process_batch
  puts "Processing batch .."
  errors = []
  begin
    block.call(@batch, errors)
  rescue Exception => e
    errors << "Error: BATCH FAILED - #{e.message}"
  ensure
    puts errors.join("\n")
    puts "..done"
  end
end