Sha256: b1d0e29d6d956a6b41c0894390cc8dc3e0a439ccf4dc53c049d48b3806ed7392

Contents?: true

Size: 1.14 KB

Versions: 1

Compression:

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

        # event.for.the.win, event.for.everyone, event.for.*
        private

        def set_pattern_key(key, value)
          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.2.0 lib/basquiat/adapters/rabbitmq/events.rb