class Beaneater::Tubes

Represents collection of tube related commands.

Attributes

client[R]

@!attribute client

@return [Beaneater] returns the client instance

Public Class Methods

new(client) click to toggle source

Creates new tubes instance.

@param [Beaneater] client The beaneater client instance. @example

Beaneater::Tubes.new(@client)
# File lib/beaneater/tube/collection.rb, line 17
def initialize(client)
  @client = client
end

Public Instance Methods

[](tube_name)
Alias for: find
all() click to toggle source

List of all known beanstalk tubes.

@return [Array<Beaneater::Tube>] List of all beanstalk tubes. @example

@client.tubes.all
  # => [<Beaneater::Tube name="tube2">, <Beaneater::Tube name="tube3">]

@api public

# File lib/beaneater/tube/collection.rb, line 78
def all
  transmit('list-tubes')[:body].map do |tube_name|
    Tube.new(client, tube_name)
  end
end
each(&block) click to toggle source

Calls the given block once for each known beanstalk tube, passing that element as a parameter.

@return An Enumerator is returned if no block is given. @example

@pool.tubes.each {|t| puts t.name}

@api public

# File lib/beaneater/tube/collection.rb, line 91
def each(&block)
  all.each(&block)
end
find(tube_name) click to toggle source

Finds the specified beanstalk tube.

@param [String] tube_name Name of the beanstalkd tube @return [Beaneater::Tube] specified tube @example

@pool.tubes.find('tube2')
@pool.tubes['tube2']
  # => <Beaneater::Tube name="tube2">

@api public

# File lib/beaneater/tube/collection.rb, line 46
def find(tube_name)
  Tube.new(client, tube_name)
end
Also aliased as: []
ignore(*names) click to toggle source

Ignores specified beanstalkd tubes.

@param [*String] names Name of tubes to ignore @example

@client.tubes.ignore('foo', 'bar')

@api public

# File lib/beaneater/tube/collection.rb, line 162
def ignore(*names)
  names.each do |w|
    transmit "ignore #{w}"
    client.connection.remove_from_watched(w)
  end
end
last_used() click to toggle source
# File lib/beaneater/tube/collection.rb, line 21
def last_used
  client.connection.tube_used
end
last_used=(tube_name) click to toggle source
# File lib/beaneater/tube/collection.rb, line 25
def last_used=(tube_name)
  client.connection.tube_used = tube_name
end
reserve(timeout=nil, &block) click to toggle source

Reserves a ready job looking at all watched tubes.

@param [Integer] timeout Number of seconds before timing out. @param [Proc] block Callback to perform on the reserved job. @yield [job] Reserved beaneater job. @return [Beaneater::Job] Reserved beaneater job. @example

@client.tubes.reserve { |job| process(job) }
  # => <Beaneater::Job id=5 body="foo">

@api public

# File lib/beaneater/tube/collection.rb, line 62
def reserve(timeout=nil, &block)
  res = transmit(
    timeout ? "reserve-with-timeout #{timeout}" : 'reserve')
  job = Job.new(client, res)
  block.call(job) if block_given?
  job
end
transmit(command, **options) click to toggle source

Delegates transmit to the connection object.

@see Beaneater::Connection#transmit

# File lib/beaneater/tube/collection.rb, line 32
def transmit(command, **options)
  client.connection.transmit(command, **options)
end
use(tube) click to toggle source

Set specified tube as used.

@param [String] tube Tube to be used. @example

@conn.tubes.use("some-tube")
# File lib/beaneater/tube/collection.rb, line 175
def use(tube)
  return tube if last_used == tube
  transmit("use #{tube}")
  self.last_used = tube
rescue BadFormatError
  raise InvalidTubeName, "Tube cannot be named '#{tube}'"
end
used() click to toggle source

Currently used beanstalk tube.

@return [Beaneater::Tube] Currently used beanstalk tube. @example

@client.tubes.used
  # => <Beaneater::Tube name="tube2">

@api public

# File lib/beaneater/tube/collection.rb, line 119
def used
  last_used = transmit('list-tube-used')[:id]
  Tube.new(client, last_used)
end
watch(*names) click to toggle source

Add specified beanstalkd tubes as watched.

@param [*String] names Name of tubes to watch @raise [Beaneater::InvalidTubeName] Tube to watch was invalid. @example

@client.tubes.watch('foo', 'bar')

@api public

# File lib/beaneater/tube/collection.rb, line 132
def watch(*names)
  names.each do |t|
    transmit "watch #{t}"
    client.connection.add_to_watched(t)
  end
rescue BadFormatError => ex
  raise InvalidTubeName, "Tube in '#{ex.cmd}' is invalid!"
end
watch!(*names) click to toggle source

Add specified beanstalkd tubes as watched and ignores all other tubes.

@param [*String] names Name of tubes to watch @raise [Beaneater::InvalidTubeName] Tube to watch was invalid. @example

@client.tubes.watch!('foo', 'bar')

@api public

# File lib/beaneater/tube/collection.rb, line 149
def watch!(*names)
  old_tubes = watched.map(&:name) - names.map(&:to_s)
  watch(*names)
  ignore(*old_tubes)
end
watched() click to toggle source

List of watched beanstalk tubes.

@return [Array<Beaneater::Tube>] List of watched beanstalk tubes. @example

@client.tubes.watched
  # => [<Beaneater::Tube name="tube2">, <Beaneater::Tube name="tube3">]

@api public

# File lib/beaneater/tube/collection.rb, line 103
def watched
  last_watched = transmit('list-tubes-watched')[:body]
  client.connection.tubes_watched = last_watched.dup
  last_watched.map do |tube_name|
    Tube.new(client, tube_name)
  end
end