Class: Fog::Rackspace::Queues::Mock::MockQueue
- Inherits:
-
Object
- Object
- Fog::Rackspace::Queues::Mock::MockQueue
- Defined in:
- lib/fog/rackspace/queues.rb
Overview
An in-memory Queue implementation.
Instance Attribute Summary collapse
-
#claims ⇒ Object
Returns the value of attribute claims.
-
#messages ⇒ Object
Returns the value of attribute messages.
-
#metadata ⇒ Object
Returns the value of attribute metadata.
-
#name ⇒ Object
Returns the value of attribute name.
Instance Method Summary collapse
-
#add_claim(ttl, grace) ⇒ Object
Create a new MockClaim.
-
#add_message(client_id, data, ttl) ⇒ MockMessage
Append a new message to the queue.
-
#ageoff ⇒ Object
Remove any messages or claims whose ttls have expired.
-
#claim!(claim_id) ⇒ MockClaim
Access an existing MockClaim by id.
-
#claimed ⇒ Integer
The number of messages currently held by a claim.
-
#free ⇒ Integer
The number of messages not held by any claim.
-
#initialize(name) ⇒ MockQueue
constructor
A new instance of MockQueue.
-
#newest ⇒ MockMessage|UndefinedObject
The most recently published message on this queue, or ‘nil`.
-
#oldest ⇒ MockMessage|UndefinedObject
The oldest published message on this queue, or ‘nil`.
-
#total ⇒ Integer
The total number of messages currently on the queue.
Constructor Details
#initialize(name) ⇒ MockQueue
Returns a new instance of MockQueue.
99 100 101 102 103 |
# File 'lib/fog/rackspace/queues.rb', line 99 def initialize(name) @name, @metadata = name, {} @messages, @claims = [], {} @id_counter = Fog::Mock.random_hex(24).to_i(16) end |
Instance Attribute Details
#claims ⇒ Object
Returns the value of attribute claims.
97 98 99 |
# File 'lib/fog/rackspace/queues.rb', line 97 def claims @claims end |
#messages ⇒ Object
Returns the value of attribute messages.
97 98 99 |
# File 'lib/fog/rackspace/queues.rb', line 97 def @messages end |
#metadata ⇒ Object
Returns the value of attribute metadata.
97 98 99 |
# File 'lib/fog/rackspace/queues.rb', line 97 def @metadata end |
#name ⇒ Object
Returns the value of attribute name.
97 98 99 |
# File 'lib/fog/rackspace/queues.rb', line 97 def name @name end |
Instance Method Details
#add_claim(ttl, grace) ⇒ Object
Create a new MockClaim.
158 159 160 161 162 |
# File 'lib/fog/rackspace/queues.rb', line 158 def add_claim(ttl, grace) claim = MockClaim.new(self, ttl, grace) claims[claim.id] = claim claim end |
#add_message(client_id, data, ttl) ⇒ MockMessage
Append a new message to the queue.
146 147 148 149 150 151 152 |
# File 'lib/fog/rackspace/queues.rb', line 146 def (client_id, data, ttl) id = @id_counter.to_s(16) @id_counter += 1 = MockMessage.new(id, self, client_id, data, ttl) @messages << end |
#ageoff ⇒ Object
Remove any messages or claims whose ttls have expired.
174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/fog/rackspace/queues.rb', line 174 def ageoff .reject! { |m| m.expired? } claims.keys.dup.each do |id| claim = claims[id] if claim.expired? || claim..empty? claim..each { |m| m.claim = nil } claims.delete(id) end end end |
#claim!(claim_id) ⇒ MockClaim
Access an existing MockClaim by id.
169 170 171 |
# File 'lib/fog/rackspace/queues.rb', line 169 def claim!(claim_id) claims[claim_id] or raise NotFound.new end |
#claimed ⇒ Integer
The number of messages currently held by a claim.
115 116 117 |
# File 'lib/fog/rackspace/queues.rb', line 115 def claimed @messages.count { |msg| msg.claimed? } end |
#free ⇒ Integer
The number of messages not held by any claim.
122 123 124 |
# File 'lib/fog/rackspace/queues.rb', line 122 def free @messages.count { |msg| ! msg.claimed? } end |
#newest ⇒ MockMessage|UndefinedObject
The most recently published message on this queue, or ‘nil`.
136 137 138 |
# File 'lib/fog/rackspace/queues.rb', line 136 def newest @messages.last end |
#oldest ⇒ MockMessage|UndefinedObject
The oldest published message on this queue, or ‘nil`.
129 130 131 |
# File 'lib/fog/rackspace/queues.rb', line 129 def oldest @messages.first end |
#total ⇒ Integer
The total number of messages currently on the queue.
108 109 110 |
# File 'lib/fog/rackspace/queues.rb', line 108 def total @messages.size end |