class CSVMonster
Attributes
delimiter[R]
input_file[R]
object_attributes[R]
output_table_object[R]
Public Class Methods
new(input_file , delimiter)
click to toggle source
# File lib/csvmonster.rb, line 5 def initialize(input_file , delimiter) @input_file = input_file @delimiter = delimiter || "," @output_table_object = [] @object_attributes = extract_the_line_of_attributes end
Public Instance Methods
parse_csv()
click to toggle source
this is the main method to read the file: csv_monster_obj.parse_csv
# File lib/csvmonster.rb, line 13 def parse_csv i=0 output_matrix = [] trim_return_carriage(fulltext).each_line do |line| 5.times { |i| avoid_commas_in_special_strings(line) } output_matrix << trim_line_ends(line).split(delimiter) unless i == 0 i+=1 end output_matrix.each do |rec| temp_hsh = {} (0..rec.size-1).each do |i| temp_hsh.merge! object_attributes[i] => rec[i] end @output_table_object << temp_hsh end output_table_object end
Protected Instance Methods
avoid_commas_in_special_strings(text)
click to toggle source
# File lib/csvmonster.rb, line 44 def avoid_commas_in_special_strings(text) text.gsub!(/\"(.*),+(.*)\"/, '"\1;\2"') end
extract_the_line_of_attributes()
click to toggle source
# File lib/csvmonster.rb, line 34 def extract_the_line_of_attributes trim_return_carriage(fulltext).each_line do |line| return trim_line_ends(line).split(delimiter || ',').map{|x| x.to_s.downcase.to_sym} end end
fulltext()
click to toggle source
# File lib/csvmonster.rb, line 40 def fulltext File.open(input_file).read end
trim_line_ends(text)
click to toggle source
# File lib/csvmonster.rb, line 52 def trim_line_ends(text) text.gsub!("\n","") text.gsub!("\t","") text end
trim_return_carriage(text)
click to toggle source
# File lib/csvmonster.rb, line 48 def trim_return_carriage(text) text.gsub!(/\r\n?/, "\n") end