Sha256: 0ddc0cea0e1484ebe0cf0bfe9d26f347334657471bf7c3ccef89f1e876a06565

Contents?: true

Size: 1.13 KB

Versions: 8

Compression:

Stored size: 1.13 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
        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

      def connected?
        true
      end

      private

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

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
basquiat-1.6.0 lib/basquiat/adapters/test_adapter.rb
basquiat-1.5.2 lib/basquiat/adapters/test_adapter.rb
basquiat-1.5.1 lib/basquiat/adapters/test_adapter.rb
basquiat-1.5.0 lib/basquiat/adapters/test_adapter.rb
basquiat-1.4.0 lib/basquiat/adapters/test_adapter.rb
basquiat-1.3.6 lib/basquiat/adapters/test_adapter.rb
basquiat-1.3.5 lib/basquiat/adapters/test_adapter.rb
basquiat-1.3.4 lib/basquiat/adapters/test_adapter.rb