Class: SooSV
- Inherits:
-
Object
- Object
- SooSV
- Defined in:
- lib/soogem/soosv.rb,
lib/soogem.rb
Overview
This is a custom replacement for a CSV parser
Instance Attribute Summary collapse
-
#body ⇒ Object
In theory, this file should be able to handle CSV, TSV and other types of files.
-
#delimiter ⇒ Object
In theory, this file should be able to handle CSV, TSV and other types of files.
-
#header ⇒ Object
In theory, this file should be able to handle CSV, TSV and other types of files.
-
#new_line_char ⇒ Object
In theory, this file should be able to handle CSV, TSV and other types of files.
Instance Method Summary collapse
- #get_lines ⇒ Object
-
#initialize(file_name) ⇒ SooSV
constructor
A new instance of SooSV.
-
#line_to_array(line) ⇒ Object
Takes in a line and splits it on the delimiter and turns it into an array.
Constructor Details
#initialize(file_name) ⇒ SooSV
Returns a new instance of SooSV.
10 11 12 13 14 15 16 |
# File 'lib/soogem/soosv.rb', line 10 def initialize(file_name) @file_name = file_name @last_header_line = 1 @new_line_char = "\n" @delimiter = "," self end |
Instance Attribute Details
#body ⇒ Object
In theory, this file should be able to handle CSV, TSV and other types of files.
Example
....
8 9 10 |
# File 'lib/soogem/soosv.rb', line 8 def body @body end |
#delimiter ⇒ Object
In theory, this file should be able to handle CSV, TSV and other types of files.
Example
....
8 9 10 |
# File 'lib/soogem/soosv.rb', line 8 def delimiter @delimiter end |
#header ⇒ Object
In theory, this file should be able to handle CSV, TSV and other types of files.
Example
....
8 9 10 |
# File 'lib/soogem/soosv.rb', line 8 def header @header end |
#new_line_char ⇒ Object
In theory, this file should be able to handle CSV, TSV and other types of files.
Example
....
8 9 10 |
# File 'lib/soogem/soosv.rb', line 8 def new_line_char @new_line_char end |
Instance Method Details
#get_lines ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/soogem/soosv.rb', line 18 def get_lines file = File.open(@file_name) header, body = [], [] i=1 file.each_line(@new_line_char).each do |line| if i <= @last_header_line header << line else body << line end i=i+1 end @header = header.to_s @body = body [header, body] end |
#line_to_array(line) ⇒ Object
Takes in a line and splits it on the delimiter and turns it into an array
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/soogem/soosv.rb', line 38 def line_to_array(line) attributes = [] last_split_index = 0 inside_quote = false line.chars.each_with_index do |char, index| inside_quote = !inside_quote if quote_toggles?(line, index) next unless char == @delimiter && !inside_quote next if line[index-1]=="\\" attributes << line[last_split_index..index-1] last_split_index = index+1 end attributes << line[last_split_index..-1] attributes end |