Class: Cosmos::CSV
Overview
Reads in a comma separated values (CSV) configuration file and allows access via the Hash bracket syntax. It allows the user to write back data to the configuration file to store state.
Instance Attribute Summary collapse
-
#archive_file ⇒ String
readonly
The name of the archive file.
Instance Method Summary collapse
-
#[](index) ⇒ Array<String>
The values in columns 2-n corresponding to the given key in column 1.
-
#close_archive ⇒ Object
Closes the archive file created by ##create_archive.
-
#create_archive(path = nil) ⇒ Object
Creates a copy of the CSV file passed into the constructor.
-
#initialize(input_file) ⇒ CSV
constructor
A new instance of CSV.
-
#keys ⇒ Array<String>
All the values in the first column of the CSV file.
-
#write_archive(values) ⇒ Object
Write the archive file created by ##create_archive.
Constructor Details
#initialize(input_file) ⇒ CSV
Returns a new instance of CSV.
23 24 25 26 27 28 29 30 31 |
# File 'lib/cosmos/utilities/csv.rb', line 23 def initialize(input_file) @filename = input_file @hash = {} @archive = nil @archive_file = "" Object::CSV.read(input_file).each do |line| @hash[line[0]] = line[1..-1].compact end end |
Instance Attribute Details
#archive_file ⇒ String (readonly)
Returns The name of the archive file.
20 21 22 |
# File 'lib/cosmos/utilities/csv.rb', line 20 def archive_file @archive_file end |
Instance Method Details
#[](index) ⇒ Array<String>
Returns The values in columns 2-n corresponding to the given key in column 1. The values are always returned as Strings so the user must convert if necessary.
42 43 44 |
# File 'lib/cosmos/utilities/csv.rb', line 42 def [](index) @hash[index] end |
#close_archive ⇒ Object
Closes the archive file created by ##create_archive. Once this method is called, #write_archive will throw an exception.
77 78 79 80 |
# File 'lib/cosmos/utilities/csv.rb', line 77 def close_archive @archive.close @archive = nil end |
#create_archive(path = nil) ⇒ Object
Creates a copy of the CSV file passed into the constructor. The file will be prefixed by the current date and time to create a unique filename. By default this file is created in the COSMOS/logs directory. Subsequent calls to #write_archive will write to this file until #close_archive is called.
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/cosmos/utilities/csv.rb', line 54 def create_archive(path = nil) raise "Archive file \"#{@archive.path}\" already open." unless @archive.nil? path = System.paths['LOGS'] if path.nil? @archive_file = File.join(path, File.([File.basename(@filename)], '')) @archive = File.open(@archive_file,"w") @hash.each do |key, values| @archive.puts "#{key},#{values.join(',')}" end @archive.puts "\n" end |
#keys ⇒ Array<String>
Returns All the values in the first column of the CSV file. These values are used as keys to access the data in columns 2-n.
35 36 37 |
# File 'lib/cosmos/utilities/csv.rb', line 35 def keys @hash.keys end |
#write_archive(values) ⇒ Object
Write the archive file created by ##create_archive. This method will append a CSV row to the archive file by joining all the values in the array into a CSV entry.
70 71 72 73 |
# File 'lib/cosmos/utilities/csv.rb', line 70 def write_archive(values) raise "Archive file not opened! Call create_archive." if @archive.nil? @archive.puts values.join(',') end |