Class: Dry::Events::Filter Private

Inherits:
Object
  • Object
show all
Defined in:
lib/dry/events/filter.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Event filter

A filter cherry-picks probes payload of events. Events not matching the predicates don’t fire callbacks.

Constant Summary collapse

NO_MATCH =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Object.new.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filter) ⇒ Filter

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create a new filter

Parameters:

  • filter (Hash)

    Source filter



25
26
27
# File 'lib/dry/events/filter.rb', line 25

def initialize(filter)
  @checks = build_checks(filter)
end

Instance Attribute Details

#checksObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



18
19
20
# File 'lib/dry/events/filter.rb', line 18

def checks
  @checks
end

#eventsArray (readonly)

Returns A list of lambdas checking payloads.

Returns:

  • (Array)

    A list of lambdas checking payloads



18
# File 'lib/dry/events/filter.rb', line 18

attr_reader :checks

Instance Method Details

#build_checks(filter, checks = EMPTY_ARRAY, keys = EMPTY_ARRAY) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Recursively build checks



41
42
43
44
45
46
47
48
49
# File 'lib/dry/events/filter.rb', line 41

def build_checks(filter, checks = EMPTY_ARRAY, keys = EMPTY_ARRAY)
  if filter.is_a?(Hash)
    filter.reduce(checks) do |cs, (key, value)|
      build_checks(value, cs, [*keys, key])
    end
  else
    [*checks, method(:compare).curry.(keys, predicate(filter))]
  end
end

#call(payload = EMPTY_HASH) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Test event payload against the checks

Parameters:

  • payload (Hash) (defaults to: EMPTY_HASH)

    Event payload



34
35
36
# File 'lib/dry/events/filter.rb', line 34

def call(payload = EMPTY_HASH)
  checks.all? { |check| check.(payload) }
end

#compare(path, predicate, payload) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/dry/events/filter.rb', line 52

def compare(path, predicate, payload)
  value = path.reduce(payload) do |acc, key|
    if acc.is_a?(Hash) && acc.key?(key)
      acc[key]
    else
      break NO_MATCH
    end
  end

  predicate.(value)
end

#predicate(value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



65
66
67
68
69
70
71
# File 'lib/dry/events/filter.rb', line 65

def predicate(value)
  case value
  when Proc then value
  when Array then value.method(:include?)
  else value.method(:==)
  end
end