Sha256: ec588e25b5c8a0051aac83f609c072f9c68dd7a92220e5dd01b93ad9ae1b16ef

Contents?: true

Size: 903 Bytes

Versions: 2

Compression:

Stored size: 903 Bytes

Contents

module Woodhouse

  class QueueCriteria
    attr_reader :criteria

    def initialize(opts = {})
      if opts.kind_of?(self.class)
        opts = opts.criteria
      end
      unless opts.nil?
        @criteria = stringify_values(opts).freeze
      end
    end

    def ==(other)
      @criteria == other.criteria
    end

    def describe
      @criteria.inspect
    end

    def amqp_headers
      # TODO: needs to be smarter
      @criteria ? @criteria.merge('x-match' => 'all') : {}
    end

    def queue_key
      @criteria ? @criteria.map{|k,v|
        "#{k.downcase}_#{v.downcase}"
      }.join("_") : ""
    end

    def matches?(args)
      return true if @criteria.nil?
      @criteria.all? do |key, val|
        args[key] == val
      end
    end

    private

    def stringify_values(hash)
      hash.inject({}) {|h,(k,v)|
        h[k.to_s] = v.to_s
        h
      }
    end

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
woodhouse-0.1.2 lib/woodhouse/queue_criteria.rb
woodhouse-0.1.1 lib/woodhouse/queue_criteria.rb