Class: Realize::Filter::ByKeyRecordValue

Inherits:
Object
  • Object
show all
Includes:
Arrays
Defined in:
lib/realize/filter/by_key_record_value.rb

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Arrays

#array

Constructor Details

#initialize(key:, value:) ⇒ ByKeyRecordValue

Returns a new instance of ByKeyRecordValue.

Raises:

  • (ArgumentError)


25
26
27
28
29
30
31
32
# File 'lib/realize/filter/by_key_record_value.rb', line 25

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

  @key    = key
  @value  = value

  freeze
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



19
20
21
# File 'lib/realize/filter/by_key_record_value.rb', line 19

def key
  @key
end

#valueObject (readonly) Also known as: desired_value

Returns the value of attribute value.



19
20
21
# File 'lib/realize/filter/by_key_record_value.rb', line 19

def value
  @value
end

Instance Method Details

#transform(resolver, value, _time, original_record) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/realize/filter/by_key_record_value.rb', line 34

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