Sha256: 7291f3c4a433a3c3a5ae04c96a95c3a0066eacf0fc83c1e3990763a88fb9780a

Contents?: true

Size: 1.15 KB

Versions: 3

Compression:

Stored size: 1.15 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 Realize
  class Filter
    # This transformer can take an array or a hash (put in array) and it understands how to
    # select only the records where a key's value looked up in the original record equates
    # to the transformer's value.
    class ByKeyRecordValue
      include Arrays
      acts_as_hashable

      attr_reader :key, :value

      # value is also passed into #transform so we need to alias it so its not shadowed by
      # the argument.
      alias desired_value value

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

        @key    = key
        @value  = value

        freeze
      end

      def transform(resolver, value, _time, original_record)
        records = array(value)

        records.select do |record|
          record_value = resolver.get(record, key)
          record_value == resolver.get(original_record, desired_value)
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
realize-1.6.0 lib/realize/filter/by_key_record_value.rb
realize-1.5.0 lib/realize/filter/by_key_record_value.rb
realize-1.4.0 lib/realize/filter/by_key_record_value.rb