Class: EfoNelfo::Reader::CSV
- Inherits:
-
Object
- Object
- EfoNelfo::Reader::CSV
- Defined in:
- lib/efo_nelfo/reader/csv.rb
Constant Summary collapse
- ENCODING =
Encoding::ISO_8859_1
- CSV_OPTIONS =
{ col_sep: ';', headers: false, row_sep: "\r\n", quote_char: "\x00", force_quotes: false, skip_blanks: true }
Instance Attribute Summary collapse
-
#csv ⇒ Object
readonly
Returns the value of attribute csv.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
Instance Method Summary collapse
-
#first ⇒ Object
Returns the first row of the csv file.
- #head ⇒ Object
-
#initialize(options) ⇒ CSV
constructor
A new instance of CSV.
-
#parse ⇒ Object
Parses the data and returns an EfoNelfo object of some kind.
Constructor Details
#initialize(options) ⇒ CSV
Returns a new instance of CSV.
20 21 22 23 24 25 26 27 28 |
# File 'lib/efo_nelfo/reader/csv.rb', line 20 def initialize() if [:filename] @data = File.read([:filename], encoding: ENCODING) else @data = [:data].force_encoding ENCODING end @csv = ::CSV.new @data, CSV_OPTIONS end |
Instance Attribute Details
#csv ⇒ Object (readonly)
Returns the value of attribute csv.
18 19 20 |
# File 'lib/efo_nelfo/reader/csv.rb', line 18 def csv @csv end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
18 19 20 |
# File 'lib/efo_nelfo/reader/csv.rb', line 18 def data @data end |
Instance Method Details
#first ⇒ Object
Returns the first row of the csv file
44 45 46 47 48 |
# File 'lib/efo_nelfo/reader/csv.rb', line 44 def first return @first if @first csv.rewind @first = csv.first end |
#head ⇒ Object
50 51 52 53 54 55 |
# File 'lib/efo_nelfo/reader/csv.rb', line 50 def head return @head if @head klass = EfoNelfo::PostType.for first[0], first[2] raise EfoNelfo::UnsupportedPostType.new("Don't know how to handle v#{first[2]} of #{first[0]}") if klass.nil? @head = klass.new first end |
#parse ⇒ Object
Parses the data and returns an EfoNelfo object of some kind
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/efo_nelfo/reader/csv.rb', line 31 def parse csv.each do |row| # Find the correct posttype module for given posttype and version klass = EfoNelfo::PostType.for row[0], head.version next if klass.nil? head.add klass.new(row) end head end |