Sha256: 3018e340341b989bb8d94770958a7e56b3834096e8982fee64ee4c08f20c80ba

Contents?: true

Size: 1.01 KB

Versions: 2

Compression:

Stored size: 1.01 KB

Contents

# frozen_string_literal: true

#
# Copyright (c) 2020-present, Blue Marble Payroll, LLC
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
#

module Burner
  class Jobs
    module Collection
      # Convert an array of arrays to an array of objects.
      # Expected Payload#value input: array of arrays.
      # Payload#value output: An array of hashes.
      class ArraysToObjects < Job
        attr_reader :mappings

        def initialize(name:, mappings: [])
          super(name: name)

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

          freeze
        end

        def perform(_output, payload)
          payload.value = (payload.value || []).map { |array| index_to_key_map(array) }

          nil
        end

        private

        def index_to_key_map(array)
          mappings.each_with_object({}) do |mapping, memo|
            memo[mapping.key] = array[mapping.index]
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
burner-1.0.0.pre.alpha.5 lib/burner/jobs/collection/arrays_to_objects.rb
burner-1.0.0.pre.alpha.4 lib/burner/jobs/collection/arrays_to_objects.rb