Class: CSVMonster
- Inherits:
-
Object
- Object
- CSVMonster
- Defined in:
- lib/csvmonster.rb
Instance Attribute Summary collapse
-
#delimiter ⇒ Object
readonly
Returns the value of attribute delimiter.
-
#input_file ⇒ Object
readonly
Returns the value of attribute input_file.
-
#object_attributes ⇒ Object
readonly
Returns the value of attribute object_attributes.
-
#output_table_object ⇒ Object
readonly
Returns the value of attribute output_table_object.
Instance Method Summary collapse
-
#initialize(input_file, delimiter) ⇒ CSVMonster
constructor
A new instance of CSVMonster.
-
#parse_csv ⇒ Object
this is the main method to read the file: csv_monster_obj.parse_csv.
Constructor Details
#initialize(input_file, delimiter) ⇒ CSVMonster
Returns a new instance of CSVMonster.
5 6 7 8 9 10 |
# 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 |
Instance Attribute Details
#delimiter ⇒ Object (readonly)
Returns the value of attribute delimiter.
3 4 5 |
# File 'lib/csvmonster.rb', line 3 def delimiter @delimiter end |
#input_file ⇒ Object (readonly)
Returns the value of attribute input_file.
3 4 5 |
# File 'lib/csvmonster.rb', line 3 def input_file @input_file end |
#object_attributes ⇒ Object (readonly)
Returns the value of attribute object_attributes.
3 4 5 |
# File 'lib/csvmonster.rb', line 3 def object_attributes @object_attributes end |
#output_table_object ⇒ Object (readonly)
Returns the value of attribute output_table_object.
3 4 5 |
# File 'lib/csvmonster.rb', line 3 def output_table_object @output_table_object end |
Instance Method Details
#parse_csv ⇒ Object
this is the main method to read the file: csv_monster_obj.parse_csv
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# 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 |