Class: Redd::Clients::Base::Stream::PRAWBoundedQueueSet

Inherits:
Set
  • Object
show all
Defined in:
lib/redd/clients/base/stream.rb

Overview

A class similar to PRAW’s implementation of a BoundedSet.

Instance Method Summary collapse

Constructor Details

#initialize(max, *args, &block) ⇒ PRAWBoundedQueueSet

Returns a new instance of PRAWBoundedQueueSet.



10
11
12
13
14
# File 'lib/redd/clients/base/stream.rb', line 10

def initialize(max, *args, &block)
  @max = max
  @queue = []
  super(*args, &block)
end

Instance Method Details

#dequeueObject Also known as: deq

Remove the last element of the queue.

Returns:

  • The removed element.



41
42
43
44
45
# File 'lib/redd/clients/base/stream.rb', line 41

def dequeue
  element = @queue.shift
  delete(element)
  element
end

#enqueue(element) ⇒ PRAWBoundedQueueSet Also known as: enq

Add an element to the front if it isn’t already in the queue.

Parameters:

  • element

Returns:



19
20
21
22
23
# File 'lib/redd/clients/base/stream.rb', line 19

def enqueue(element)
  @queue.push(element) if add?(element)
  dequeue! if size > @max
  self
end

#enqueue?(element) ⇒ Boolean Also known as: enq?

Add an element to the front if it isn’t already in the queue.

Parameters:

  • element

Returns:

  • (Boolean)

    Whether the element was added to the queue.



29
30
31
32
33
34
35
36
# File 'lib/redd/clients/base/stream.rb', line 29

def enqueue?(element)
  added = add?(element)
  if added
    @queue.push(element)
    dequeue if size > @max
  end
  added
end