Sha256: d6a58d0d16835df261a52c47ccaa9315709ab754b990dc6484a11db61067efb8
Contents?: true
Size: 1.22 KB
Versions: 16
Compression:
Stored size: 1.22 KB
Contents
# frozen_string_literal: true class TableSync::Config::CallbackRegistry CALLBACK_KINDS = %i[after_commit before_commit].freeze EVENTS = %i[create update destroy].freeze InvalidCallbackKindError = Class.new(ArgumentError) InvalidEventError = Class.new(ArgumentError) def initialize @callbacks = CALLBACK_KINDS.map { |event| [event, make_event_hash] }.to_h end def register_callback(callback, kind:, event:) validate_callback_kind!(kind) validate_event!(event) callbacks.fetch(kind)[event] << callback end def get_callbacks(kind:, event:) validate_callback_kind!(kind) validate_event!(event) callbacks.fetch(kind).fetch(event, []) end private attr_reader :callbacks def make_event_hash Hash.new { |hsh, key| hsh[key] = [] } end def validate_callback_kind!(kind) unless CALLBACK_KINDS.include?(kind) raise( InvalidCallbackKindError, "Invalid callback kind: #{kind.inspect}. Valid kinds are #{CALLBACK_KINDS.inspect}", ) end end def validate_event!(event) unless EVENTS.include?(event) raise( InvalidEventError, "Invalid event: #{event.inspect}. Valid events are #{EVENTS.inspect}", ) end end end
Version data entries
16 entries across 16 versions & 1 rubygems