Sha256: 3033f0cd546488ee06f5ff6a60fdfec03d5330097265ff498f104dbc38288d30

Contents?: true

Size: 1.1 KB

Versions: 1

Compression:

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

    [:push, :pop, :receive].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

1 entries across 1 versions & 1 rubygems

Version Path
queuel-0.1.0 lib/queuel/client.rb