Class: Scruber::Helpers::DictionaryReader::Csv

Inherits:
Object
  • Object
show all
Defined in:
lib/scruber/helpers/dictionary_reader/csv.rb

Instance Method Summary collapse

Constructor Details

#initialize(file_path) ⇒ Csv

Returns a new instance of Csv.



5
6
7
# File 'lib/scruber/helpers/dictionary_reader/csv.rb', line 5

def initialize(file_path)
  @file_path = file_path
end

Instance Method Details

#read(options = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/scruber/helpers/dictionary_reader/csv.rb', line 9

def read(options={})
  col_sep = options.delete(:col_sep) || ','

  CSV.foreach(@file_path, col_sep: col_sep, headers: true, encoding: 'utf-8') do |csv_row|
    if options.blank?
      yield csv_row
    else
      if options.all?{|(k,v)| csv_row[k.to_s] == v }
        yield csv_row
      end
    end
  end
end