Sha256: c06a7c7d707a093728454737c94567f64ce2edbe8d0cb6bb4b56823068cecb82

Contents?: true

Size: 907 Bytes

Versions: 4

Compression:

Stored size: 907 Bytes

Contents

module Adrian
  class Queue
    class ItemTooOldError < RuntimeError
      attr_reader :item, :queue

      def initialize(item, queue)
        super()
        @item  = item
        @queue = queue
      end
    end

    def initialize(options = {})
      @options = options
    end

    def pop
      item = pop_item
      item.queue = self if item
      verify_age!(item)
    end

    def push(item)
      push_item(item)
    end

    def verify_age!(item)
      if item && max_age && item.age > max_age
        raise ItemTooOldError.new(item, self)
      end

      item
    end

    def max_age
      @max_age ||= @options[:max_age]
    end

    def pop_item
      raise "#{self.class.name}#pop_item is not defined"
    end

    def push_item(item)
      raise "#{self.class.name}#push_item is not defined"
    end

    def length
      raise "#{self.class.name}#length is not defined"
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
adrian-2.0.2 lib/adrian/queue.rb
adrian-2.0.1 lib/adrian/queue.rb
adrian-2.0.0 lib/adrian/queue.rb
adrian-1.5.0 lib/adrian/queue.rb