class Forklift::Connection::Csv
Public Class Methods
new(config, forklift)
click to toggle source
# File lib/forklift/transports/csv.rb, line 8 def initialize(config, forklift) @config = config @forklift = forklift end
Public Instance Methods
config()
click to toggle source
# File lib/forklift/transports/csv.rb, line 13 def config @config end
connect()
click to toggle source
# File lib/forklift/transports/csv.rb, line 21 def connect; end
disconnect()
click to toggle source
# File lib/forklift/transports/csv.rb, line 22 def disconnect; end
forklift()
click to toggle source
# File lib/forklift/transports/csv.rb, line 17 def forklift @forklift end
read(size=forklift.config[:batch_size]) { |data| ... }
click to toggle source
# 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)
click to toggle source
# 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