Sha256: 8a7efbcdfcd2e7713b9893d2459cf2a7ed6842f45afd19facafbe0d9ff6c25e6

Contents?: true

Size: 1.14 KB

Versions: 8

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

8 entries across 8 versions & 1 rubygems

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