Class: Burner::Library::Collection::ObjectsToArrays

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

Overview

Convert an array of objects to an array of arrays. You can leverage the separator option to support key paths and nested objects. Pass in an array of Burner::Modeling::KeyIndexMapping instances or hashable configurations which specifies the key-to-index mappings to use.

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

An example using a configuration-first pipeline:

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

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

Burner::Pipeline.make(config).execute

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, separator: '') ⇒ ObjectsToArrays

If you wish to support nested objects you can pass in a string to use as a key path separator. For example: if you would like to recognize dot-notation for nested hashes then set separator to ‘.’. For more information, see the underlying library that supports this dot-notation concept:

https://github.com/bluemarblepayroll/objectable


61
62
63
64
65
66
67
68
# File 'lib/burner/library/collection/objects_to_arrays.rb', line 61

def initialize(mappings: [], name: '', register: DEFAULT_REGISTER, separator: '')
  super(name: name, register: register)

  @mappings = Modeling::KeyIndexMapping.array(mappings)
  @resolver = Objectable.resolver(separator: separator.to_s)

  freeze
end

Instance Attribute Details

#mappingsObject (readonly)

Returns the value of attribute mappings.



54
55
56
# File 'lib/burner/library/collection/objects_to_arrays.rb', line 54

def mappings
  @mappings
end

Instance Method Details

#perform(_output, payload) ⇒ Object



70
71
72
# File 'lib/burner/library/collection/objects_to_arrays.rb', line 70

def perform(_output, payload)
  payload[register] = array(payload[register]).map { |object| key_to_index_map(object) }
end