Class: CDC::Core::Filter

Inherits:
Object
  • Object
show all
Defined in:
lib/cdc/core/filter.rb

Overview

Predicate object used to decide whether a pipeline should process an event.

Filters are composable with #& and #|. A filter only matches when its predicate returns true exactly, keeping accidental truthy values from silently passing events through a pipeline.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|event| ... } ⇒ Filter

Build a custom filter.

Yield Parameters:

Yield Returns:

  • (Boolean)

    true to match the event

Raises:

  • (ArgumentError)

    when no predicate block is provided



45
46
47
48
49
# File 'lib/cdc/core/filter.rb', line 45

def initialize(&predicate)
  raise ArgumentError, 'predicate block required' unless predicate

  @predicate = predicate
end

Class Method Details

.allFilter

Match every event.

Returns:



14
# File 'lib/cdc/core/filter.rb', line 14

def self.all = new { |_event| true }

.operation(operation) ⇒ Filter

Match events by operation.

Parameters:

  • operation (#to_sym)

    CDC operation

Returns:



38
# File 'lib/cdc/core/filter.rb', line 38

def self.operation(operation) = new { |event| event.operation == Operation.normalize(operation) }

.qualified_table(name) ⇒ Filter

Match events from a fully qualified schema.table name.

Parameters:

  • name (#to_s)

    qualified table name

Returns:



32
# File 'lib/cdc/core/filter.rb', line 32

def self.qualified_table(name) = new { |event| event.qualified_table_name == name.to_s }

.schema(name) ⇒ Filter

Match events from a schema.

Parameters:

  • name (#to_s)

    schema name

Returns:



20
# File 'lib/cdc/core/filter.rb', line 20

def self.schema(name) = new { |event| event.schema == name.to_s }

.table(name) ⇒ Filter

Match events from a table regardless of schema.

Parameters:

  • name (#to_s)

    table name

Returns:



26
# File 'lib/cdc/core/filter.rb', line 26

def self.table(name) = new { |event| event.table == name.to_s }

Instance Method Details

#&(other) ⇒ Filter

Compose this filter with another filter using logical AND.

Parameters:

  • other (Filter)

    other filter

Returns:



64
65
66
# File 'lib/cdc/core/filter.rb', line 64

def &(other)
  self.class.new { |event| match?(event) && other.match?(event) }
end

#match?(event) ⇒ Boolean Also known as: =~

Whether this filter matches an event.

Parameters:

Returns:

  • (Boolean)


55
56
57
# File 'lib/cdc/core/filter.rb', line 55

def match?(event)
  @predicate.call(event) == true
end

#|(other) ⇒ Filter

Compose this filter with another filter using logical OR.

Parameters:

  • other (Filter)

    other filter

Returns:



72
73
74
# File 'lib/cdc/core/filter.rb', line 72

def |(other)
  self.class.new { |event| match?(event) || other.match?(event) }
end