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



23
24
25
# File 'lib/dry/events/filter.rb', line 23

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.



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

def checks
  @checks
end

#eventsArray (readonly)

Returns A list of lambdas checking payloads.

Returns:

  • (Array)

    A list of lambdas checking payloads



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

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



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

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



32
33
34
# File 'lib/dry/events/filter.rb', line 32

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.



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

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.



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

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