Class: WorkerTools::CsvInput::CsvInputForeach
- Inherits:
-
Object
- Object
- WorkerTools::CsvInput::CsvInputForeach
- Includes:
- Enumerable
- Defined in:
- lib/worker_tools/csv_input.rb
Instance Method Summary collapse
- #each ⇒ Object
-
#initialize(rows_enum:, input_columns:, mapping_order:, cleanup_method:, headers_present:) ⇒ CsvInputForeach
constructor
A new instance of CsvInputForeach.
- #values_to_row(values) ⇒ Object
- #values_to_row_according_to_mapping(values) ⇒ Object
- #values_to_row_according_to_position(values) ⇒ Object
Constructor Details
#initialize(rows_enum:, input_columns:, mapping_order:, cleanup_method:, headers_present:) ⇒ CsvInputForeach
Returns a new instance of CsvInputForeach.
165 166 167 168 169 170 171 |
# File 'lib/worker_tools/csv_input.rb', line 165 def initialize(rows_enum:, input_columns:, mapping_order:, cleanup_method:, headers_present:) @rows_enum = rows_enum @input_columns = input_columns @mapping_order = mapping_order @cleanup_method = cleanup_method @headers_present = headers_present end |
Instance Method Details
#each ⇒ Object
173 174 175 176 177 178 179 180 181 |
# File 'lib/worker_tools/csv_input.rb', line 173 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
183 184 185 186 187 |
# File 'lib/worker_tools/csv_input.rb', line 183 def values_to_row(values) return values_to_row_according_to_mapping(values) if @mapping_order values_to_row_according_to_position(values) end |
#values_to_row_according_to_mapping(values) ⇒ Object
189 190 191 192 193 |
# File 'lib/worker_tools/csv_input.rb', line 189 def values_to_row_according_to_mapping(values) @mapping_order.each_with_object(HashWithIndifferentAccess.new) do |(k, v), h| h[k] = @cleanup_method.call(values[v]) end end |
#values_to_row_according_to_position(values) ⇒ Object
195 196 197 |
# File 'lib/worker_tools/csv_input.rb', line 195 def values_to_row_according_to_position(values) @input_columns.map.with_index { |c, i| [c, @cleanup_method.call(values[i])] }.to_h.with_indifferent_access end |