Class: CsvSource

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

Overview

Helper class to process CSV files

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ CsvSource

Returns a new instance of CsvSource.


10
11
12
# File 'lib/csv_source.rb', line 10

def initialize(file)
  @file = file
end

Instance Attribute Details

#arraysObject (readonly)

Returns the value of attribute arrays


7
8
9
# File 'lib/csv_source.rb', line 7

def arrays
  @arrays
end

#fileObject (readonly)

Returns the value of attribute file


8
9
10
# File 'lib/csv_source.rb', line 8

def file
  @file
end

Instance Method Details

#readObject

returns an array of arrays containing file contents


15
16
17
18
19
# File 'lib/csv_source.rb', line 15

def read
  raise FileNotFoundException.new I18n.t :file_not_found unless File.exist? file 
 
  @arrays = CSV.read(file)
end

#to_sObject

returns a line delimited single string


22
23
24
25
# File 'lib/csv_source.rb', line 22

def to_s
  read unless arrays
  arrays.map(&:join).join("\n")
end