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.
-
#bool(item, index = 0) ⇒ Boolean
(also: #boolean)
Convenience method to access a value by key and convert it to a boolean.
-
#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.
-
#float(item, index = 0) ⇒ Float
Convenience method to access a value by key and convert it to a float.
-
#initialize(input_file) ⇒ CSV
constructor
A new instance of CSV.
-
#int(item, index = 0) ⇒ Integer
(also: #integer)
Convenience method to access a value by key and convert it to an integer.
-
#keys ⇒ Array<String>
All the values in the first column of the CSV file.
-
#string(item, index = 0) ⇒ String
(also: #str)
Convenience method to access a value by key and convert it to a string.
-
#symbol(item, index = 0) ⇒ Symbol
(also: #sym)
Convenience method to access a value by key and convert it to a symbol.
-
#write_archive(value) ⇒ Object
Write the archive file created by ##create_archive.
Constructor Details
#initialize(input_file) ⇒ CSV
Returns a new instance of CSV.
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/cosmos/utilities/csv.rb', line 22 def initialize(input_file) @filename = input_file @hash = {} @archive = nil @archive_file = "" Object::CSV.read(input_file).each do |line| next if line[0].strip()[0] == '#' # Ignore Ruby comment lines @hash[line[0]] = line[1..-1] end end |
Instance Attribute Details
#archive_file ⇒ String (readonly)
Returns The name of the archive file.
19 20 21 |
# File 'lib/cosmos/utilities/csv.rb', line 19 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 |
#bool(item, index = 0) ⇒ Boolean Also known as: boolean
Convenience method to access a value by key and convert it to a boolean. The csv value must be ‘TRUE’ or ‘FALSE’ (case doesn’t matter) and will be converted to Ruby true or false values.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/cosmos/utilities/csv.rb', line 53 def bool(item, index = 0) raise "#{item} not found" unless keys.include?(item) if Range === index @hash[item][index].map do |x| case x.upcase when 'TRUE' true when 'FALSE' false else raise "#{item} value of #{x} not boolean. Must be 'TRUE' 'or 'FALSE'." end end else case @hash[item][index].upcase when 'TRUE' true when 'FALSE' false else raise "#{item} value of #{@hash[item][index]} not boolean. Must be 'TRUE' 'or 'FALSE'." end end end |
#close_archive ⇒ Object
Closes the archive file created by ##create_archive.
184 185 186 187 188 |
# File 'lib/cosmos/utilities/csv.rb', line 184 def close_archive @archive.close File.chmod(0444, @archive_file) @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.
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/cosmos/utilities/csv.rb', line 146 def create_archive(path = nil) close_archive() if @archive path = System.paths['LOGS'] if path.nil? attempt = nil while true name = File.([File.basename(@filename, '.csv'), attempt], '.csv') @archive_file = File.join(path, name) if File.exist?(@archive_file) attempt ||= 0 attempt += 1 else break end end @archive = File.open(@archive_file, "w") @hash.each do |key, values| @archive.puts "#{key},#{values.join(',')}" end @archive.puts "\n" end |
#float(item, index = 0) ⇒ Float
Convenience method to access a value by key and convert it to a float
99 100 101 102 103 104 105 106 |
# File 'lib/cosmos/utilities/csv.rb', line 99 def float(item, index = 0) raise "#{item} not found" unless keys.include?(item) if Range === index @hash[item][index].map {|x| x.to_f } else @hash[item][index].to_f end end |
#int(item, index = 0) ⇒ Integer Also known as: integer
Convenience method to access a value by key and convert it to an integer
84 85 86 87 88 89 90 91 |
# File 'lib/cosmos/utilities/csv.rb', line 84 def int(item, index = 0) raise "#{item} not found" unless keys.include?(item) if Range === index @hash[item][index].map {|x| x.to_i } else @hash[item][index].to_i end 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 |
#string(item, index = 0) ⇒ String Also known as: str
Convenience method to access a value by key and convert it to a string
113 114 115 116 117 118 119 120 |
# File 'lib/cosmos/utilities/csv.rb', line 113 def string(item, index = 0) raise "#{item} not found" unless keys.include?(item) if Range === index @hash[item][index].map {|x| x.to_s } else @hash[item][index].to_s end end |
#symbol(item, index = 0) ⇒ Symbol Also known as: sym
Convenience method to access a value by key and convert it to a symbol
128 129 130 131 132 133 134 135 |
# File 'lib/cosmos/utilities/csv.rb', line 128 def symbol(item, index = 0) raise "#{item} not found" unless keys.include?(item) if Range === index @hash[item][index].map {|x| x.intern } else @hash[item][index].intern end end |
#write_archive(value) ⇒ Object
Write the archive file created by ##create_archive. This method will append a row to the archive file by joining writing the value.
174 175 176 177 178 179 180 181 |
# File 'lib/cosmos/utilities/csv.rb', line 174 def write_archive(value) create_archive() unless @archive if value.is_a? Array @archive.puts value.join(',') else @archive.puts value end end |