Class: SimpleCsvReader::Reader

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, headers, **options) ⇒ Reader

Returns a new instance of Reader.



8
9
10
11
12
# File 'lib/simple_csv_reader/reader.rb', line 8

def initialize(path, headers, **options)
  @path = path
  @headers = headers
  @options = options
end

Class Method Details

.default_convertersObject



27
28
29
# File 'lib/simple_csv_reader/reader.rb', line 27

def self.default_converters
  instance_variable_defined?(:@default_converters) ? @default_converters : :numeric
end

.default_converters=(converters) ⇒ Object



31
32
33
# File 'lib/simple_csv_reader/reader.rb', line 31

def self.default_converters=(converters)
  @default_converters = converters
end

.default_nil_valueObject



35
36
37
# File 'lib/simple_csv_reader/reader.rb', line 35

def self.default_nil_value
  instance_variable_defined?(:@default_nil_value) ? @default_nil_value : nil
end

.default_nil_value=(value) ⇒ Object



39
40
41
# File 'lib/simple_csv_reader/reader.rb', line 39

def self.default_nil_value=(value)
  @default_nil_value = value
end

Instance Method Details

#each(&block) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/simple_csv_reader/reader.rb', line 14

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

  # NOTE: The first line is a header
  csv.each.with_index(2) do |row, row_number|
    hash = row.to_h
    next if hash.compact.empty? # NOTE: blank row

    filtered_hash = @options[:include_unspecified_headers] ? hash : hash.slice(*@headers.keys)
    yield(filtered_hash, row_number: row_number)
  end
end