Sha256: 656c8daaa8a2e9b2db9cfbecbde9d53c43959157e4da1f4b8c23437b3f7443a2

Contents?: true

Size: 1.14 KB

Versions: 3

Compression:

Stored size: 1.14 KB

Contents

# frozen_string_literal: true
module Basquiat
  module Adapters
    class RabbitMq
      class Events
        attr_reader :keys

        def initialize
          @keys     = []
          @exact    = {}
          @patterns = {}
        end

        def []=(key, value)
          if key =~ /\*|#/
            set_pattern_key(key, value)
          else
            @exact[key] = value
          end
          @keys.push key
        end

        def [](key)
          @exact.fetch(key) { simple_pattern_match(key) }
        rescue KeyError
          raise KeyError, "No event handler found for #{key}"
        end

        private

        def set_pattern_key(key, value)
          key            = key.gsub('.', '\.')
          key            = if key =~ /\*/
                             /^#{key.gsub('*', '[^.]+')}$/
                           else
                             /^#{key.gsub(/#/, '.*')}$/
                           end
          @patterns[key] = value
        end

        def simple_pattern_match(key)
          match = @patterns.keys.detect(nil) { |pattern| key =~ pattern }
          @patterns.fetch match
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

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