Class: Metacrunch::File::CSVSource

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

Constant Summary collapse

DEFAULT_OPTIONS =
{
  headers: true,
  col_sep: ",",
  row_sep: "\n",
  quote_char: '"',
  file_encoding: "utf-8"
}

Instance Method Summary collapse

Constructor Details

#initialize(csv_filename, options = {}) ⇒ CSVSource

Returns a new instance of CSVSource.



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

def initialize(csv_filename, options = {})
  @filename = csv_filename
  @options = DEFAULT_OPTIONS.merge(options)
end

Instance Method Details

#each(&block) ⇒ Object



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

def each(&block)
  return enum_for(__method__) unless block_given?

  SmarterCSV.process(@filename, {
    headers_in_file: @options[:headers],
    col_sep: @options[:col_sep],
    row_sep: @options[:row_sep],
    quote_char: @options[:quote_char],
    file_encoding: @options[:file_encoding]
  }) do |line|
    yield line
  end
end