Class: UnifiedQueues::Multi

Inherits:
Object
  • Object
show all
Defined in:
lib/unified-queues/multi.rb,
lib/unified-queues/multi/driver.rb,
lib/unified-queues/multi/driver/em-jack.rb,
lib/unified-queues/multi/driver/unified-queues.rb

Overview

Universal multi queue interface.

Defined Under Namespace

Classes: Driver

Constant Summary collapse

DRIVERS =

Contains assignment of classnames to drivers.

Returns:

  • (Hash)
Hash[
    :"UnifiedQueues::Single" => "unified-queues.rb",
    :"EMJack::Connection" => "em-jack.rb"
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Constructor.

Parameters:

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

    required class object or driver instance

  • *args (Array)

    array of arguments for the queue constructor

  • &block (Proc)

    block for the queue constructor



45
46
47
48
49
50
51
# File 'lib/unified-queues/multi.rb', line 45

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

Instance Attribute Details

#driverUnifiedQueues::Multi::Driver

Contains driver for specific class instance.

Returns:



34
35
36
# File 'lib/unified-queues/multi.rb', line 34

def driver
  @driver
end

Instance Method Details

#assign_driver(cls, args, block) ⇒ UnifiedQueues::Multi::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:



62
63
64
65
66
67
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
# File 'lib/unified-queues/multi.rb', line 62

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/multi/driver/" << driver
    
    path = name.to_s.split("::")
    classname = path.shift << 'Driver::' << path.join('::')
    _module = Module::get_module("UnifiedQueues::Multi::Driver::" << classname)
    
    args = [cls] + args
    @driver = _module::new(*args, &block)
    return @driver
end

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

Closes the session.



202
203
204
# File 'lib/unified-queues/multi.rb', line 202

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

#list(&block) ⇒ Array

Lists names of all available queues.

Returns:

  • (Array)


176
177
178
# File 'lib/unified-queues/multi.rb', line 176

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

#list_subscribed(&block) ⇒ Array

Lists all subscribed queues.

Returns:

  • (Array)


194
195
196
# File 'lib/unified-queues/multi.rb', line 194

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

#list_used(&block) ⇒ Array

Lists all used queues.

Returns:

  • (Array)


185
186
187
# File 'lib/unified-queues/multi.rb', line 185

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

#pop(blocking = false, &block) ⇒ Object|nil

Pops value from the queue. In contrast to default Queue library, blocks or returns nil if empty.

Parameters:

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

    true or timeout if it should block, false otherwise

Returns:

  • (Object|nil)


117
118
119
# File 'lib/unified-queues/multi.rb', line 117

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

#push(value, key = value, &block) ⇒ Object

Pushes value to the currently used queue.

Parameters:

  • value (Object)
  • key (Object) (defaults to: value)

    key for priority purposes



105
106
107
# File 'lib/unified-queues/multi.rb', line 105

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

#subscribe(name, &block) ⇒ Object

Subscribes to the queue. So marks it as target for #pop. Note, than only single queue usally can be subscribed at one time.

Parameters:

  • name (Object)

    name of the required queue



140
141
142
# File 'lib/unified-queues/multi.rb', line 140

def subscribe(name, &block)
    @driver.subscribe(name, &block)
end

#subscribed(&block) ⇒ Queue

Currently subscribed queue.

Returns:

  • (Queue)


167
168
169
# File 'lib/unified-queues/multi.rb', line 167

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

#unsubscribe(name, &block) ⇒ Object

Unsubscribes from the queue.

Parameters:

  • name (Object)

    name of the required queue



149
150
151
# File 'lib/unified-queues/multi.rb', line 149

def unsubscribe(name, &block)
    @driver.unsubscribe(name, &block)
end

#use(name, &block) ⇒ Object

Sets queue with given name as used. So marks it as target for #push.

Parameters:

  • name (Object)

    name of the required queue



128
129
130
# File 'lib/unified-queues/multi.rb', line 128

def use(name, &block)
    @driver.use(name, &block)
end

#used(&block) ⇒ Queue

Currently used queue.

Returns:

  • (Queue)


158
159
160
# File 'lib/unified-queues/multi.rb', line 158

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