Sha256: 8cb02b63ad54d10ad31a155f19e7863d27d647a8546e0c4130d650b76519eb29

Contents?: true

Size: 877 Bytes

Versions: 3

Compression:

Stored size: 877 Bytes

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 Realize
  class Value
    # Basic transformer that can take an object and extract a value based off the transformer's
    # key.  If the value passed in is an array then it will select the first record.
    class Resolve
      acts_as_hashable

      attr_reader :key

      def initialize(key:)
        raise ArgumentError, 'key is required' if key.to_s.empty?

        @key = key

        freeze
      end

      def transform(resolver, value, _time, _record)
        record = first(value)

        resolver.get(record, key)
      end

      private

      def first(value)
        value.is_a?(Array) ? value.first : value
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
realize-1.6.0 lib/realize/value/resolve.rb
realize-1.5.0 lib/realize/value/resolve.rb
realize-1.4.0 lib/realize/value/resolve.rb