Class: TableSync::Config::CallbackRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/table_sync/config/callback_registry.rb

Constant Summary collapse

CALLBACK_KINDS =
%i[after_commit before_commit].freeze
EVENTS =
%i[create update destroy].freeze
InvalidCallbackKindError =
Class.new(ArgumentError)
InvalidEventError =
Class.new(ArgumentError)

Instance Method Summary collapse

Constructor Details

#initializeCallbackRegistry

Returns a new instance of CallbackRegistry.



10
11
12
# File 'lib/table_sync/config/callback_registry.rb', line 10

def initialize
  @callbacks = CALLBACK_KINDS.map { |event| [event, make_event_hash] }.to_h
end

Instance Method Details

#get_callbacks(kind:, event:) ⇒ Object



21
22
23
24
25
26
# File 'lib/table_sync/config/callback_registry.rb', line 21

def get_callbacks(kind:, event:)
  validate_callback_kind!(kind)
  validate_event!(event)

  callbacks.fetch(kind).fetch(event, [])
end

#register_callback(callback, kind:, event:) ⇒ Object



14
15
16
17
18
19
# File 'lib/table_sync/config/callback_registry.rb', line 14

def register_callback(callback, kind:, event:)
  validate_callback_kind!(kind)
  validate_event!(event)

  callbacks.fetch(kind)[event] << callback
end