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

Version Path
table_sync-2.3.0 lib/table_sync/config/callback_registry.rb
table_sync-2.2.0 lib/table_sync/config/callback_registry.rb
table_sync-2.1.0 lib/table_sync/config/callback_registry.rb
table_sync-2.0.0 lib/table_sync/config/callback_registry.rb
table_sync-1.13.1 lib/table_sync/config/callback_registry.rb
table_sync-1.13.0 lib/table_sync/config/callback_registry.rb
table_sync-1.12.1 lib/table_sync/config/callback_registry.rb
table_sync-1.12.0 lib/table_sync/config/callback_registry.rb
table_sync-1.11.0 lib/table_sync/config/callback_registry.rb
table_sync-1.10.0 lib/table_sync/config/callback_registry.rb
table_sync-1.9.0 lib/table_sync/config/callback_registry.rb
table_sync-1.8.0 lib/table_sync/config/callback_registry.rb
table_sync-1.7.0 lib/table_sync/config/callback_registry.rb
table_sync-1.6.0 lib/table_sync/config/callback_registry.rb
table_sync-1.5.0 lib/table_sync/config/callback_registry.rb
table_sync-1.4.0 lib/table_sync/config/callback_registry.rb