Sha256: 0028d06032193d3e50a61e44cf744b95441255781b59667e4c2770319eae807b

Contents?: true

Size: 931 Bytes

Versions: 7

Compression:

Stored size: 931 Bytes

Contents

# frozen_string_literal: true

module SlackRubyBot
  module Hooks
    class Set
      attr_accessor :handlers, :client

      def initialize(client = nil)
        self.handlers = {}
        self.client = client

        @pending_flush = client.blank?
      end

      def add(hook_name, handler)
        handlers[hook_name] ||= ::Set.new
        handlers[hook_name] << handler

        register_callback(hook_name)
      end

      def client=(client)
        @client = client

        flush_handlers if @pending_flush
      end

      protected

      def register_callback(hook_name)
        return unless client # We'll delay this until client is set

        client.on hook_name do |data|
          handlers[hook_name].each do |handler|
            handler.call(client, data)
          end
        end
      end

      def flush_handlers
        handlers.each_key { |hook| register_callback(hook) }
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
slackrb-0.17.1 lib/slack-ruby-bot/hooks/set.rb
slackrb-0.17.0 lib/slack-ruby-bot/hooks/set.rb
slack-ruby-bot-0.16.1 lib/slack-ruby-bot/hooks/set.rb
slack-ruby-bot-0.16.0 lib/slack-ruby-bot/hooks/set.rb
slack-ruby-bot-0.15.0 lib/slack-ruby-bot/hooks/set.rb
slack-ruby-bot-0.14.0 lib/slack-ruby-bot/hooks/set.rb
slack-ruby-bot-0.13.0 lib/slack-ruby-bot/hooks/set.rb