Class: WorkerTools::CsvInput::CsvInputForeach

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/worker_tools/csv_input.rb

Instance Method Summary collapse

Constructor Details

#initialize(rows_enum:, mapping_order:, cleanup_method:, headers_present:) ⇒ CsvInputForeach

Returns a new instance of CsvInputForeach.



164
165
166
167
168
169
# File 'lib/worker_tools/csv_input.rb', line 164

def initialize(rows_enum:, mapping_order:, cleanup_method:, headers_present:)
  @rows_enum = rows_enum
  @mapping_order = mapping_order
  @cleanup_method = cleanup_method
  @headers_present = headers_present
end

Instance Method Details

#eachObject



171
172
173
174
175
176
177
178
179
# File 'lib/worker_tools/csv_input.rb', line 171

def each
  return enum_for(:each) unless block_given?

  @rows_enum.with_index.each do |values, index|
    next if index.zero? && @headers_present

    yield values_to_row(values)
  end
end

#values_to_row(values) ⇒ Object



181
182
183
184
185
# File 'lib/worker_tools/csv_input.rb', line 181

def values_to_row(values)
  @mapping_order.each_with_object(HashWithIndifferentAccess.new) do |(k, v), h|
    h[k] = @cleanup_method.call(values[v])
  end
end