Sha256: 36235d7be6b6a123c277c5bb811a9bee07b88c27f3dd71a451ef36107cec58d7

Contents?: true

Size: 1.25 KB

Versions: 1

Compression:

Stored size: 1.25 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
      # Take an array of objects and call #values on each object.
      # If include_keys is true (it is false by default), then call #keys on the first
      # object and inject that as a "header" object.
      # Expected Payload#value input: array of objects.
      # Payload#value output: An array of arrays.
      class Values < Job
        attr_reader :include_keys

        def initialize(name:, include_keys: false)
          super(name: name)

          @include_keys = include_keys || false

          freeze
        end

        def perform(_output, payload)
          keys   = include_keys ? [keys(payload.value&.first)] : []
          values = (payload.value || []).map { |object| values(object) }

          payload.value = keys + values

          nil
        end

        private

        def keys(object)
          object.respond_to?(:keys) ? object.keys : []
        end

        def values(object)
          object.respond_to?(:values) ? object.values : []
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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