Sha256: 2afcc6ed820ed86ba8811b970a27cfc829b0ab108eeee0e44441eeea974a24a1

Contents?: true

Size: 1.13 KB

Versions: 1

Compression:

Stored size: 1.13 KB

Contents

require 'set'

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

1 entries across 1 versions & 1 rubygems

Version Path
basquiat-1.3.0.pre.1 lib/basquiat/adapters/rabbitmq/events.rb