class CSVMonster

Constants

VERSION

Attributes

filepaths[R]

Public Class Methods

new(filepaths = nil) click to toggle source
# File lib/csv_monster.rb, line 7
def initialize filepaths = nil
  self.filepaths = filepaths
end

Public Instance Methods

'/'(split_count)
Alias for: split
+(other_csv_monster) click to toggle source
# File lib/csv_monster.rb, line 15
def + other_csv_monster
  self.class.new self.filepaths + other_csv_monster.filepaths
end
==(other_csv_monster) click to toggle source
# File lib/csv_monster.rb, line 19
def == other_csv_monster
  content == other_csv_monster.content
end
content() click to toggle source
# File lib/csv_monster.rb, line 23
def content
  return @content if defined? @content

  @content ||= []
  @filepaths.each_with_index do |csv_filepath, i|
    csv = CSV.parse(File.read(csv_filepath))
    csv.shift unless i == 0

    csv.each do |row|
      @content << row
    end
  end

  @content
end
content_length() click to toggle source
# File lib/csv_monster.rb, line 39
def content_length
  content.length
end
filepaths=(filepaths) click to toggle source
# File lib/csv_monster.rb, line 11
def filepaths= filepaths
  @filepaths = [*filepaths]
end
split(split_count) click to toggle source
# File lib/csv_monster.rb, line 43
def split(split_count)
  header, *entries = self.content
  splits = []

  entries.each_with_index do |row, i|
    if (i % (entries.length / split_count)).zero? &&
          split_count > splits.length
      splits << self.class.new
      splits.last.content << header
    end

    splits.last.content << row
  end

  splits
end
Also aliased as: '/'
write!(outfile = nil) click to toggle source
# File lib/csv_monster.rb, line 62
def write! outfile = nil
  outfile ||= default_outfile_name
  CSV.open(outfile, "wb") do |csv|
    content.each do |row|
      csv << row
    end
  end

  outfile
end

Private Instance Methods

default_outfile_name() click to toggle source
# File lib/csv_monster.rb, line 75
def default_outfile_name
  "#{Time.now.strftime("%Y%m%d%H%M%S")}_#{@filepaths.length}_files.csv"
end