Sha256: ae72be1f61b876c8103e5f90c18b75cc910cc4c83e4cf61d7a3a986f4c63e8be

Contents?: true

Size: 1.26 KB

Versions: 1

Compression:

Stored size: 1.26 KB

Contents

# frozen_string_literal: true
require 'singleton'

module Apress
  module Api
    module Callbacks
      class Config
        include Singleton

        class << self
          delegate :add_service, :services, to: :instance
          delegate :allowed_client?, :add_client, to: :instance
          delegate :add_handler, :handlers, to: :instance
          delegate :valid_event?, to: :instance
        end

        def add_service(event:, service:)
          events[event] << service
        end

        def add_handler(service:, event:, handler:)
          (handlers_config[service][event] ||= []) << handler
        end

        def add_client(access_id)
          clients << access_id
        end

        def allowed_client?(client)
          clients.include?(client.access_id)
        end

        def services(event)
          events[event]
        end

        def handlers(service:, event:)
          handlers_config.fetch(service).fetch(event)
        end

        private

        def handlers_config
          @handlers_config ||= ::Hash.new { |hash, key| hash[key] = {} }
        end

        def events
          @events ||= ::Hash.new { |hash, key| hash[key] = [] }
        end

        def clients
          @clients ||= Set.new
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
apress-api-1.24.2 lib/apress/api/callbacks/config.rb