class Dry::Events::Filter

Event filter

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

@api private

Constants

NO_MATCH

Attributes

checks[R]

@!attribute [r] events

@return [Array] A list of lambdas checking payloads

Public Class Methods

new(filter) click to toggle source

Create a new filter

@param [Hash] filter Source filter

@api private

# File lib/dry/events/filter.rb, line 25
def initialize(filter)
  @checks = build_checks(filter)
end

Public Instance Methods

build_checks(filter, checks = EMPTY_ARRAY, keys = EMPTY_ARRAY) click to toggle source

Recursively build checks

@api private

# 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) click to toggle source

Test event payload against the checks

@param [Hash] payload Event payload

@api private

# File lib/dry/events/filter.rb, line 34
def call(payload = EMPTY_HASH)
  checks.all? { |check| check.(payload) }
end
compare(path, predicate, payload) click to toggle source

@api private

# 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) click to toggle source

@api private

# 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