Class: Gooby::CsvReader
- Inherits:
-
GoobyObject
- Object
- GoobyObject
- Gooby::CsvReader
- Defined in:
- lib/gooby_csv_reader.rb
Instance Attribute Summary collapse
-
#col_names ⇒ Object
readonly
Returns the value of attribute col_names.
-
#csv_points ⇒ Object
readonly
Returns the value of attribute csv_points.
-
#files ⇒ Object
readonly
Returns the value of attribute files.
Instance Method Summary collapse
- #add_file(filename) ⇒ Object
- #display_formatted_record(record_index = 2) ⇒ Object
-
#initialize(array_of_filenames = nil) ⇒ CsvReader
constructor
A new instance of CsvReader.
- #read ⇒ Object
- #to_s ⇒ Object
Methods included from GoobyKernel
#character_align, #default_delimiter, #invalid_altitude, #invalid_heartbeat, #invalid_latitude, #invalid_longitude, #invalid_time, #project_author, #project_copyright, #project_date, #project_embedded_comment, #project_license, #project_name, #project_version_number, #project_year, #read_as_ascii_lines, #read_lines, #strip_lines, #tokenize
Constructor Details
#initialize(array_of_filenames = nil) ⇒ CsvReader
Returns a new instance of CsvReader.
15 16 17 18 19 20 21 |
# File 'lib/gooby_csv_reader.rb', line 15 def initialize(array_of_filenames=nil) @files = Array.new @csv_points = Array.new if array_of_filenames array_of_filenames.each { |filename| add_file(filename) } end end |
Instance Attribute Details
#col_names ⇒ Object (readonly)
Returns the value of attribute col_names.
13 14 15 |
# File 'lib/gooby_csv_reader.rb', line 13 def col_names @col_names end |
#csv_points ⇒ Object (readonly)
Returns the value of attribute csv_points.
13 14 15 |
# File 'lib/gooby_csv_reader.rb', line 13 def csv_points @csv_points end |
#files ⇒ Object (readonly)
Returns the value of attribute files.
13 14 15 |
# File 'lib/gooby_csv_reader.rb', line 13 def files @files end |
Instance Method Details
#add_file(filename) ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/gooby_csv_reader.rb', line 23 def add_file(filename) if (filename) if (File.exist?(filename)) @files << filename end end end |
#display_formatted_record(record_index = 2) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/gooby_csv_reader.rb', line 50 def display_formatted_record(record_index=2) tokens = @csv_points[record_index].rawdata.split('|') puts "\nCsvReader.display_formatted_record hdr_cols=#{@col_names.size} data_cols=#{tokens.size}" size = 0 @col_names.each { |col_name| size = size + 1 if size <= tokens.size value = tokens[size - 1] puts "#{col_name.strip.ljust(20)} #{(size - 1).to_s.ljust(3)} #{value}" end } end |
#read ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/gooby_csv_reader.rb', line 31 def read @files.each { |filename| lines = read_lines(filename, true) lines.each { |line| if (line.match('^#')) if (line.match('^#cols: ')) col_names_header = line[7, line.size] @col_names = col_names_header.split('|') end else if (line.size > 50) @csv_points << Gooby::CsvPoint.new(line) end end } } @csv_points end |
#to_s ⇒ Object
63 64 65 66 67 |
# File 'lib/gooby_csv_reader.rb', line 63 def to_s s = "CsvReader - file count: #{files.size} total points: #{csv_points.size}" @files.each { |file| s << "\n file: #{file} "} s end |