Class: CDC::Core::Router

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

Overview

Routes CDC work items to the appropriate handler.

Router keeps the dispatch vocabulary in core while leaving execution strategy to the caller. It can route single change events, transaction envelopes, and arrays of events.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(processor:, transaction_processor: nil, observer: NullObserver::INSTANCE) ⇒ Router

Build a router.

Parameters:

  • processor (#process)

    handler for change events

  • transaction_processor (#process, nil) (defaults to: nil)

    handler for transaction envelopes

  • observer (Observer, nil) (defaults to: NullObserver::INSTANCE)

    instrumentation observer



21
22
23
24
25
# File 'lib/cdc/core/router.rb', line 21

def initialize(processor:, transaction_processor: nil, observer: NullObserver::INSTANCE)
  @processor = processor
  @transaction_processor = transaction_processor
  @observer = observer || NullObserver::INSTANCE
end

Instance Attribute Details

#observer#process, ... (readonly)

Returns:

  • (#process)

    handler for individual change events

  • (#process, nil)

    handler for transaction envelopes

  • (Observer)

    observer notified of dispatch events



14
15
16
# File 'lib/cdc/core/router.rb', line 14

def observer
  @observer
end

#processor#process, ... (readonly)

Returns:

  • (#process)

    handler for individual change events

  • (#process, nil)

    handler for transaction envelopes

  • (Observer)

    observer notified of dispatch events



14
15
16
# File 'lib/cdc/core/router.rb', line 14

def processor
  @processor
end

#transaction_processor#process, ... (readonly)

Returns:

  • (#process)

    handler for individual change events

  • (#process, nil)

    handler for transaction envelopes

  • (Observer)

    observer notified of dispatch events



14
15
16
# File 'lib/cdc/core/router.rb', line 14

def transaction_processor
  @transaction_processor
end

Instance Method Details

#process(item) ⇒ Object

Process a CDC work item.

Parameters:

Returns:

  • (Object)

Raises:



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/cdc/core/router.rb', line 32

def process(item)
  observer.dispatch_started(item)
  case item
  when ChangeEvent
    result = processor.process(item)
    observe_result(result)
    result
  when TransactionEnvelope
    route_transaction(item)
  when Array
    route_many(item)
  else
    raise UnsupportedWorkItemError, "unsupported CDC work item: #{item.class}"
  end
end