Class: Realize::Filter::ByKeyValue

Inherits:
Object
  • Object
show all
Includes:
Arrays
Defined in:
lib/realize/filter/by_key_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 statically equates to the transformer’s value.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Arrays

#array

Constructor Details

#initialize(key:, value:) ⇒ ByKeyValue

Returns a new instance of ByKeyValue.

Raises:

  • (ArgumentError)


25
26
27
28
29
30
31
32
# File 'lib/realize/filter/by_key_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_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_value.rb', line 19

def value
  @value
end

Instance Method Details

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



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

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

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

    record_value == desired_value
  end
end