Class: Gooby::CsvReader

Inherits:
GoobyObject show all
Defined in:
lib/gooby_csv_reader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_namesObject (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_pointsObject (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

#filesObject (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

#readObject



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_sObject



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