Class: UnifiedQueues::Single

Inherits:
Object
  • Object
show all
Defined in:
lib/unified-queues/single.rb,
lib/unified-queues/single/driver.rb,
lib/unified-queues/single/driver/depq.rb,
lib/unified-queues/single/driver/array.rb,
lib/unified-queues/single/driver/queue.rb,
lib/unified-queues/single/driver/algorithms.rb,
lib/unified-queues/single/driver/evented-queue.rb,
lib/unified-queues/single/driver/priority_queue.rb

Overview

Universal single queue interface.

Defined Under Namespace

Classes: Driver

Constant Summary collapse

DRIVERS =

Contains assignment of classnames to drivers.

Returns:

  • (Hash)
Hash[
    :Queue => "queue",
    :Array => "array",
    :Depq => "depq",
    :"Containers::Heap" => "algorithms",
    :CPriorityQueue => "priority_queue",
    :PoorPriorityQueue => "priority_queue",
    :RubyPriorityQueue => "priority_queue",
    :EventedQueue => "evented-queue" 
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cls, *args, &block) ⇒ Single

Constructor.

Parameters:

  • cls (Class|UnifiedQueues::Single::Driver)

    required class object or driver instance

  • *args (Array)

    array of arguments for the queue constructor

  • &block (Proc)

    block for the queue constructor



51
52
53
54
55
56
57
# File 'lib/unified-queues/single.rb', line 51

def initialize(cls, *args, &block)
    if cls.kind_of? UnifiedQueues::Single::Driver
        @driver = cls
    else
        self.assign_driver(cls, args, block)
    end
end

Instance Attribute Details

#driverUnifiedQueues::Single::Driver

Contains driver for specific class instance.

Returns:



40
41
42
# File 'lib/unified-queues/single.rb', line 40

def driver
  @driver
end

Instance Method Details

#assign_driver(cls, args, block) ⇒ UnifiedQueues::Single::Driver

Assigns driver to interface according to given class name.

Parameters:

  • cls (Class)

    required class

  • args (Array)

    array of arguments for the queue constructor

  • block (Proc)

    block for the queue constructor

Returns:



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/unified-queues/single.rb', line 68

def assign_driver(cls, args, block)
    _cls = cls
    if not _cls.kind_of? Class
        _cls = cls.class
    end
    
    driver = nil
    name = nil
    
    self.class::DRIVERS.each_pair do |_name, _driver|
        begin
            _module = Module::get_module(_name.to_s)
        rescue NameError
            next
        end
        
        if _cls <= _module
            driver = _driver
            name = _name
            break
        end
    end
    
    ###
    
    require "unified-queues/single/driver/" << driver
    
    path = name.to_s.split("::")
    classname = path.shift << 'Driver::' << path.join('::')
    _module = Module::get_module("UnifiedQueues::Single::Driver::" << classname)
    
    args = [cls] + args
    @driver = _module::new(*args, &block)
    return @driver
end

#clear!(&block) ⇒ Object Also known as: clear

Clears the queue.



143
144
145
# File 'lib/unified-queues/single.rb', line 143

def clear!(&block)
    @driver.clear!(&block)
end

#empty?(&block) ⇒ Boolean

Indicates queue is empty.

Parameters:

  • +true+ (Boolean)

    if it’s, false otherwise

Returns:

  • (Boolean)


135
136
137
# File 'lib/unified-queues/single.rb', line 135

def empty?(&block)
    @driver.empty?(&block)
end

#length(&block) ⇒ Integer Also known as: size

Returns length of the queue.

Returns:

  • (Integer)


154
155
156
# File 'lib/unified-queues/single.rb', line 154

def length(&block)
    @driver.length(&block)
end

#pop(blocking = false, &block) ⇒ Object Also known as: pop!

Pops value out of the queue.

Parameters:

  • blocking (Boolean|Integer) (defaults to: false)

    true or timeout if it should block, false otherwise

  • queue (Object)

    value



124
125
126
# File 'lib/unified-queues/single.rb', line 124

def pop(blocking = false, &block)
    @driver.pop(blocking, &block)
end

#push(value, key = value, &block) ⇒ Object Also known as: <<

Pushes the value into the queue.

Parameters:

  • value (Object)

    value for push

  • key (Object) (defaults to: value)

    key for priority queues



111
112
113
# File 'lib/unified-queues/single.rb', line 111

def push(value, key = value, &block)
    @driver.push(value, key, &block)
end