Sha256: ad96874c2d9c750530e96f0e33df80613994d8217d78a2d2a9ed6e5bdb06b471

Contents?: true

Size: 1.1 KB

Versions: 3

Compression:

Stored size: 1.1 KB

Contents

# frozen_string_literal: true
module Basquiat
  module Adapters
    # An adapter to be used in testing
    class Test < Basquiat::Adapters::Base
      class << self
        def events
          @events ||= Hash.new { |hash, key| hash[key] = [] }
        end

        def clean
          @events.clear if @events
        end
      end

      attr_reader :options

      def base_options
        @event_names = []
        { host: '127.0.0.1', port: 123_456, durable: true }
      end

      def publish(event, message, _single_message = true)
        self.class.events[event] << Basquiat::Json.encode(message)
      end

      def events(key)
        self.class.events[key]
      end

      def subscribe_to(event_name, proc)
        @event_names << event_name
        procs[event_name] = proc
      end

      def listen(*)
        event = subscribed_event
        msg   = self.class.events[event].shift
        msg ? procs[event].call(BaseMessage.new(msg)) : nil
      end

      private

      def subscribed_event
        event = @event_names.first
        @event_names.rotate!
        event
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
basquiat-1.3.2 lib/basquiat/adapters/test_adapter.rb
basquiat-1.3.1 lib/basquiat/adapters/test_adapter.rb
basquiat-1.3.0 lib/basquiat/adapters/test_adapter.rb