Class: Forklift::Connection::Csv

Inherits:
Base::Connection show all
Defined in:
lib/forklift/transports/csv.rb

Instance Method Summary collapse

Methods inherited from Base::Connection

#client, #exec, #exec!, #exec_ruby, #exec_script, #pipe

Constructor Details

#initialize(config, forklift) ⇒ Csv

Returns a new instance of Csv.



8
9
10
11
# File 'lib/forklift/transports/csv.rb', line 8

def initialize(config, forklift)
  @config = config
  @forklift = forklift
end

Instance Method Details

#configObject



13
14
15
# File 'lib/forklift/transports/csv.rb', line 13

def config
  @config
end

#connectObject



21
# File 'lib/forklift/transports/csv.rb', line 21

def connect; end

#disconnectObject



22
# File 'lib/forklift/transports/csv.rb', line 22

def disconnect; end

#forkliftObject



17
18
19
# File 'lib/forklift/transports/csv.rb', line 17

def forklift
  @forklift
end

#read(size = forklift.config[:batch_size]) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/forklift/transports/csv.rb', line 24

def read(size=forklift.config[:batch_size])
  data = []
  CSV.foreach(config[:file], headers: true, converters: :all) do |row|
    data << row.to_hash.symbolize_keys
    if(data.length == size)
      if block_given?
        yield data
        data = []
      else
        return data
      end
    end
  end

  if block_given?
    yield data
  else
    return data
  end
end

#write(data, append = true) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/forklift/transports/csv.rb', line 45

def write(data, append=true)
  if (append == false)
    FileUtils.rm(config[:file], {force: true})
  end

  if( !File.exists?(config[:file]) )
    keys = data.first.keys
    row = {}
    keys.each do |k|
      row[k] = k
    end
    data = [row] + data
  end

  CSV.open(config[:file],'a') do |file|
    data.each do |row|
      file << row.values
    end
  end

end