Class: RelationalExporter::CsvBuilder

Inherits:
Object
  • Object
show all
Includes:
Celluloid, Celluloid::Logger
Defined in:
lib/relational_exporter/csv_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path = nil) ⇒ CsvBuilder

Returns a new instance of CsvBuilder.



15
16
17
18
19
20
21
# File 'lib/relational_exporter/csv_builder.rb', line 15

def initialize(file_path=nil)
  @header_row = []
  @index = 0
  @end_index = nil
  @queue = {}
  @file_path = file_path
end

Instance Attribute Details

#end_indexObject

Returns the value of attribute end_index.



8
9
10
# File 'lib/relational_exporter/csv_builder.rb', line 8

def end_index
  @end_index
end

#queueObject

Returns the value of attribute queue.



8
9
10
# File 'lib/relational_exporter/csv_builder.rb', line 8

def queue
  @queue
end

Instance Method Details

#actor_died(actor, reason) ⇒ Object



11
12
13
# File 'lib/relational_exporter/csv_builder.rb', line 11

def actor_died(actor, reason)
  warn "Oh no! #{actor.inspect} has died because of a #{reason.class}" unless reason.nil?
end

#remainingObject



49
50
51
# File 'lib/relational_exporter/csv_builder.rb', line 49

def remaining
  @end_index - @index if @end_index
end

#startObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/relational_exporter/csv_builder.rb', line 23

def start
  csv_args = @file_path.blank? ? STDOUT : @file_path

  csv_options = {headers: true}
  if @file_path.blank?
    csv_method = :instance
    csv_args = [STDOUT, csv_options]
  else
    csv_method = :open
    csv_args = [@file_path, 'wb', csv_options]
  end

  ::CSV.send(csv_method, *csv_args) do |csv|
    until @index == @end_index
      if row = @queue.delete(@index)
        write_row(row, csv)
        @index += 1
      else
        sleep 1
      end
    end
  end

  true
end