Module: ConsoleBuddy::CSV

Included in:
IRB
Defined in:
lib/console_buddy/csv.rb

Instance Method Summary collapse

Instance Method Details

#generate_csv(headers, rows, filename: DateTime.now.to_s, dir: 'tmp') ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/console_buddy/csv.rb', line 7

def generate_csv(headers, rows, filename: DateTime.now.to_s, dir: 'tmp')
  Dir.mkdir(dir) unless Dir.exist?(dir)

  file_path = ::File.join(dir, "#{filename}.csv")
  ::CSV.open(file_path, 'w') do |csv|
    csv << headers
    rows.each do |row|
      csv << row
    end
  end

  puts "CSV created. CSV can be found at: #{file_path}"
end

#read_csv(file_path, skip_headers: true) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/console_buddy/csv.rb', line 21

def read_csv(file_path, skip_headers: true)
  data = []
  ::CSV.foreach(file_path, headers: skip_headers) do |row|
    data << row
  end
  data
end