class Burner::Library::Collection::ArraysToObjects

Convert an array of arrays to an array of objects. Pass in an array of Burner::Modeling::KeyIndexMapping instances or hashable configurations which specifies the index-to-key mappings to use.

Expected Payload input: array of arrays. Payload output: An array of hashes.

An example using a configuration-first pipeline:

config = {
  jobs: [
    {
      name: 'set',
      type: 'b/value/static',
      value: [
        [1, 'funky']
      ]
    },
    {
      name: 'map',
      type: 'b/collection/arrays_to_objects',
      mappings: [
        { index: 0, key: 'id' },
        { index: 1, key: 'name' }
      ]
    },
    {
      name: 'output',
      type: 'b/echo',
      message: 'value is currently: {__value}'
    },

  ],
  steps: %w[set map output]
}

Burner::Pipeline.make(config).execute

Given the above example, the expected output would be:

[
  { 'id' => 1, 'name' => 'funky' }
]

Attributes

mappings[R]

Public Class Methods

new(mappings: [], name: '', register: DEFAULT_REGISTER) click to toggle source
Calls superclass method Burner::JobWithRegister::new
# File lib/burner/library/collection/arrays_to_objects.rb, line 58
def initialize(mappings: [], name: '', register: DEFAULT_REGISTER)
  super(name: name, register: register)

  @mappings = Modeling::KeyIndexMapping.array(mappings)

  freeze
end

Public Instance Methods

perform(_output, payload) click to toggle source
# File lib/burner/library/collection/arrays_to_objects.rb, line 66
def perform(_output, payload)
  payload[register] = array(payload[register]).map { |array| index_to_key_map(array) }
end

Private Instance Methods

index_to_key_map(array) click to toggle source
# File lib/burner/library/collection/arrays_to_objects.rb, line 72
def index_to_key_map(array)
  mappings.each_with_object({}) do |mapping, memo|
    memo[mapping.key] = array[mapping.index]
  end
end