class CsvOption::Utils

Attributes

file[RW]

Public Class Methods

new(file_path) click to toggle source
# File lib/csv_option/utils.rb, line 5
def initialize(file_path)
  @file = file_path
end

Public Instance Methods

determine_column_separator(data=nil) click to toggle source
# File lib/csv_option/utils.rb, line 17
def determine_column_separator(data=nil)
  file_contents = data || first_row 
  counts  = Hash.new(0)
  counts.merge!({"," => 0 , ":" => 0, ";" => 0 , "|" => 0, "\t" => 0 })
  file_contents.each_char do |c|
    next if c.match(/^[[:alpha:]]$/) or c.match(/^[[:digit:]]$/)
    counts[c] += 1
  end
  k,v = counts.max_by{|k,v| v}
  return k  
end
determine_row_separator() click to toggle source
# File lib/csv_option/utils.rb, line 40
def determine_row_separator
  body = full_contents
  counts = {"\n" => 0 , "\r" => 0, "\r\n" => 0}
  quoted_char = false
  body.each_char do |c|
    quoted_char = !quoted_char if c == '"'
    next if quoted_char || c !~ /\r|\n|\r\n/
    counts[c] += 1

  end
  k,v = counts.max_by{|k,v| v}
  return k
end
first_row() click to toggle source
# File lib/csv_option/utils.rb, line 9
def first_row
  open(@file).each_line.first    
end
full_contents() click to toggle source
# File lib/csv_option/utils.rb, line 13
def full_contents 
  open(@file).read 
end
parse_headers() click to toggle source
# File lib/csv_option/utils.rb, line 29
def parse_headers
  data = first_row
  col_sep = determine_column_separator(data)
  headers = if data.include?("\r")
    data.split("\r").first.split(col_sep).map{ |a| a.strip.tr('"', '')}.sort
  else
    data.split(col_sep).map{ |a| a.strip.tr('"', '')}.sort
  end
  headers
end