Class: OpenC3::CSV

Inherits:
Object show all
Defined in:
lib/openc3/utilities/csv.rb

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

Instance Method Summary collapse

Constructor Details

#initialize(input_file) ⇒ CSV

Returns a new instance of CSV.

Parameters:

  • input_file (String)

    CSV filename to read



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/openc3/utilities/csv.rb', line 34

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_fileString (readonly)

Returns The name of the archive file.

Returns:

  • (String)

    The name of the archive file



31
32
33
# File 'lib/openc3/utilities/csv.rb', line 31

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.

Returns:

  • (Array<String>)

    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.



55
56
57
# File 'lib/openc3/utilities/csv.rb', line 55

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.

Parameters:

  • item (String)

    Key to access the value

  • index (Integer) (defaults to: 0)

    Which value to return

Returns:

  • (Boolean)

    Single value converted to a boolean (true or false)



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/openc3/utilities/csv.rb', line 66

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

#float(item, index = 0) ⇒ Float

Convenience method to access a value by key and convert it to a float

Parameters:

  • item (String)

    Key to access the value

  • index (Integer) (defaults to: 0)

    Which value to return

Returns:

  • (Float)

    Single value converted to a float



114
115
116
117
118
119
120
121
122
# File 'lib/openc3/utilities/csv.rb', line 114

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

Parameters:

  • item (String)

    Key to access the value

  • index (Integer) (defaults to: 0)

    Which value to return

Returns:

  • (Integer)

    Single value converted to an integer



98
99
100
101
102
103
104
105
106
# File 'lib/openc3/utilities/csv.rb', line 98

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

#keysArray<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.

Returns:

  • (Array<String>)

    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.



48
49
50
# File 'lib/openc3/utilities/csv.rb', line 48

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

Parameters:

  • item (String)

    Key to access the value

  • index (Integer) (defaults to: 0)

    Which value to return

Returns:

  • (String)

    Single value converted to a string



129
130
131
132
133
134
135
136
137
# File 'lib/openc3/utilities/csv.rb', line 129

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

Parameters:

  • item (String)

    Key to access the value

  • index (Integer) (defaults to: 0)

    Which value to return

Returns:

  • (Symbol)

    Single value converted to a symbol



145
146
147
148
149
150
151
152
153
# File 'lib/openc3/utilities/csv.rb', line 145

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