Sha256: 28933152dfb5c0cde59c09f213309b573444779f44c72510ab60bde74bf4ee66

Contents?: true

Size: 1.11 KB

Versions: 7

Compression:

Stored size: 1.11 KB

Contents

module Queuel
  NoQueueGivenError = Class.new StandardError
  class Client
    def initialize(engine, credentials, init_queue = nil)
      self.engine = engine
      self.credentials = credentials
      self.given_queue = init_queue
    end

    [:peek, :push, :pop, :receive, :size].each do |operation|
      define_method(operation) do |*args, &block|
        with_queue { queue_connection.public_send(operation, *args, &block) }
      end
    end

    def with(change_queue = nil)
      self.clone.tap { |client| client.given_queue = change_queue }
    end

    def queue
      bare = (given_queue || Queuel.default_queue)
      bare.to_s unless bare.nil?
    end

    protected
    attr_accessor :given_queue

    private

    def with_queue
      if queue.nil? || queue.to_s.strip.empty?
        raise NoQueueGivenError, "Must select a queue with #with or set a default_queue"
      else
        yield
      end
    end

    def queue_connection
      engine_client.queue queue
    end

    def engine_client
      @engine_client ||= engine.new credentials
    end

    attr_accessor :credentials
    attr_accessor :engine
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
queuel-0.4.8 lib/queuel/client.rb
queuel-0.4.6 lib/queuel/client.rb
queuel-0.4.5 lib/queuel/client.rb
queuel-0.4.4 lib/queuel/client.rb
queuel-0.4.3 lib/queuel/client.rb
queuel-0.4.2 lib/queuel/client.rb
queuel-0.4.1 lib/queuel/client.rb