Class: Burner::Library::Collection::FlatFileParse

Inherits:
JobWithDynamicKeys show all
Defined in:
lib/burner/library/collection/flat_file_parse.rb

Overview

Convert an array of arrays to an array of objects. The difference between this job and ArraysToObjects is that this one does not require mappings and instead will use the first entry as the array of keys to positionally map to. If key mappings are specified then the keys register will only contain the keys that are mapped. The register will still contain mapped and unmapped keys.

For example, if a register had this:

[['id', 'first', 'last'], [1, 'frank', 'rizzo']]

Then executing this job would result in this:

[{ 'id' => 1, 'first' => frank, 'last' => rizzo }]

As a side-effect, the keys_register would result in this, if no key mappings were specified:

['id', 'first', 'last']

If key mappings are specified: [

{
   from: 'first',
   to: 'first_name'
}
{
   from: 'iban',
   to: 'iban_number'
}

] Then the register will now be:

[{ 'id' => 1, 'first_name' => frank, 'last' => rizzo }]

And the keys_register will now contain:

['first_name']

Since ‘last’ did not have a key mapping entry it does not exist in the keys register. In addition, even though ‘iban’ does have a key mapping it does not exist in the register’s payload, so it also does not exist in the keys register.

Expected Payload input: array of arrays. Payload output: An array of hashes. Payload output; An array of key names.

Constant Summary

Constants inherited from JobWithRegister

JobWithRegister::BLANK

Instance Attribute Summary

Attributes inherited from JobWithDynamicKeys

#key_mappings, #keys_register, #resolver

Attributes inherited from JobWithRegister

#register

Attributes inherited from Job

#name

Instance Method Summary collapse

Methods inherited from JobWithDynamicKeys

#initialize

Methods inherited from JobWithRegister

#initialize

Methods inherited from Job

#initialize

Methods included from Util::Arrayable

#array

Constructor Details

This class inherits a constructor from Burner::JobWithDynamicKeys

Instance Method Details

#perform(output, payload) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/burner/library/collection/flat_file_parse.rb', line 57

def perform(output, payload)
  objects     = array(payload[register])
  keys        = array(objects.shift)
  count       = objects.length
  mapped_keys = update_keys_using_mappings(keys, output)

  payload[register]      = objects.map { |object| transform(object, mapped_keys, output) }
  payload[keys_register] = get_mapped_keys(mapped_keys)

  output.detail("Mapping #{count} array(s)")

  output.detail("Mapping keys register array to: #{payload[keys_register].join(', ')}")
end