Class: OOCSV::CSVEntry

Inherits:
Struct
  • Object
show all
Defined in:
lib/oocsv.rb

Overview

A struct representing a single line in a CSV file.

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ CSVEntry

Creates a new CSVEntry from a hash, rather than specific symbol => value sets.

Parameters:

  • opts (Hash<Symbol, Any>) (defaults to: {})

    The options hash. Key is the instance variable name (header in CSV), value is this entry’s value.



9
10
11
12
13
14
15
16
17
# File 'lib/oocsv.rb', line 9

def initialize(opts = {})
  opts.each do |k, v|
    self.class.send(:attr_accessor, k)
    instance_variable_set("@#{k}", v)
  end

  # noinspection RubyInstanceVariableNamingConvention
  @i_expect_that_nobody_will_use_this_name = opts
end

Instance Method Details

#to_sObject

Returns a string representation of the CSV. This is not the same as OOCSV#write.



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/oocsv.rb', line 20

def to_s
  values = {}
  @i_expect_that_nobody_will_use_this_name.keys.each do |key|
    val = instance_variable_get("@#{key}")
    values[key] = val
  end
  str = '#<struct Struct::CSVEntry'
  values.each do |k, v|
    str << " @#{k}=#{v}"
  end
  str << '>'
end