Class: ArcFurnace::CSVSource

Inherits:
Source show all
Includes:
CSVToHashWithDuplicateHeaders
Defined in:
lib/arc-furnace/csv_source.rb

Instance Attribute Summary collapse

Attributes inherited from Node

#error_handler, #node_id, #params

Instance Method Summary collapse

Methods included from CSVToHashWithDuplicateHeaders

#csv_to_hash_with_duplicates

Methods inherited from Source

#close, #finalize, #prepare, #row

Constructor Details

#initialize(filename:, encoding: 'UTF-8') ⇒ CSVSource

Returns a new instance of CSVSource.



11
12
13
14
15
# File 'lib/arc-furnace/csv_source.rb', line 11

def initialize(filename: , encoding: 'UTF-8')
  @file = File.open(filename, encoding: encoding)
  @csv = CSV.new(file, encoding: encoding, headers: true).each
  advance
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



9
10
11
# File 'lib/arc-furnace/csv_source.rb', line 9

def file
  @file
end

#valueObject (readonly)

Returns the value of attribute value.



9
10
11
# File 'lib/arc-furnace/csv_source.rb', line 9

def value
  @value
end

Instance Method Details

#advanceObject



22
23
24
25
26
27
28
29
30
31
# File 'lib/arc-furnace/csv_source.rb', line 22

def advance
  @value =
    begin
      csv_to_hash_with_duplicates(csv.next) if csv
    rescue StopIteration
      file.close
      @csv = nil
      nil
    end
end

#empty?Boolean

Is this source empty?

Returns:

  • (Boolean)


18
19
20
# File 'lib/arc-furnace/csv_source.rb', line 18

def empty?
  !value
end