Class: Burner::Library::Collection::ArraysToObjects

Inherits:
JobWithRegister show all
Defined in:
lib/burner/library/collection/arrays_to_objects.rb

Overview

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' }
]

Constant Summary

Constants inherited from JobWithRegister

JobWithRegister::BLANK

Instance Attribute Summary collapse

Attributes inherited from JobWithRegister

#register

Attributes inherited from Job

#name

Instance Method Summary collapse

Methods included from Util::Arrayable

#array

Constructor Details

#initialize(mappings: [], name: '', register: DEFAULT_REGISTER) ⇒ ArraysToObjects

Returns a new instance of ArraysToObjects.



58
59
60
61
62
63
64
# 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

Instance Attribute Details

#mappingsObject (readonly)

Returns the value of attribute mappings.



56
57
58
# File 'lib/burner/library/collection/arrays_to_objects.rb', line 56

def mappings
  @mappings
end

Instance Method Details

#perform(_output, payload) ⇒ Object



66
67
68
# 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