class Box::Office::Showing

Attributes

booth[R]
capacity[RW]
name[R]
showings[RW]
track_fulfilled[RW]

Public Class Methods

new(name: config.default_name, track_fulfilled: config.track_fulfilled, showings: config.showings, capacity: config.capacity) click to toggle source
# File lib/box/office/showing.rb, line 13
def initialize(name: config.default_name, track_fulfilled: config.track_fulfilled,
               showings: config.showings, capacity: config.capacity)
  @name              = name
  @track_fulfilled   = track_fulfilled
  @showings          = showings
  @capacity          = capacity

  @booth             = Booth.new(showing: self)

  setup_reserved_queues
end

Public Instance Methods

<<(*msg)
Alias for: push
fulfill(queue, msg) click to toggle source
# File lib/box/office/showing.rb, line 31
def fulfill(queue, msg)
  send(config.fulfilled) << msg if track_fulfilled?
  queue >> msg
end
pop(capacity: self.capacity, block_unlock: true)
Alias for: reserve
push(*msg) click to toggle source
# File lib/box/office/showing.rb, line 36
def push(*msg)
  with_connection { |conn| conn.lpush(send("#{config.standby}_key"), *msg) }
end
Also aliased as: <<
reserve(capacity: self.capacity, block_unlock: true) { |opening, members(limit: capacity)| ... } click to toggle source
# File lib/box/office/showing.rb, line 41
def reserve(capacity: self.capacity, block_unlock: true)
  with_connection do |conn|
    booth.first_available_opening(conn) do |opening|
      Janitor.lock(opening.name) if config.lock_reserved?
      conn.rpoplpush(send("#{config.standby}_key"), opening.name) while openings?(opening, capacity)

      if block_given?
        yield opening, opening.members(limit: capacity)
        Janitor.unlock(opening.name) if config.lock_reserved? && block_unlock
      end
    rescue
      Janitor.unlock(opening.name)
      raise
    end
  end
end
Also aliased as: pop

Private Instance Methods

openings?(queue, capacity) click to toggle source
# File lib/box/office/showing.rb, line 73
def openings?(queue, capacity)
  queue.length < capacity && !send(config.standby).empty?
end
setup_reserved_queues() click to toggle source
# File lib/box/office/showing.rb, line 77
def setup_reserved_queues
  Box::Office.config.reserved.tap do |que|
    define_singleton_method que do |idx = 1|
      raise OutOfRange, "#{idx} is out of range of available showings" unless (1..showings).to_a.include? idx

      Queue.new send("#{que}_key", idx)
    end
  end
end