Module: ActsAsQueue::ClassMethods
- Defined in:
- lib/acts_as_queue.rb
Overview
acts_as_queue allows you to turn any ActiveRecord model into a queue, providing you with the necessary actions to manipulate your model. The class also allows you to define a queue size so that your queue can then become a bounded queue.
acts_as_queue example
class Post < ActiveRecord::Base
acts_as_queue
end
# Pushing new posts onto the queue.
Post.push :name => "Hello World"
Post.push :name => "Using Rails"
# Popping the item at the front of the queue.
Post.pop
Instance Method Summary collapse
-
#acts_as_queue(options = {}) ⇒ Object
Configuration options are:.
-
#count ⇒ Object
Returns the number of items in the queue.
-
#pop ⇒ Object
Pops the earliest item from the queue.
-
#push(attributes) ⇒ Object
Pushes an item onto the queue.
Instance Method Details
#acts_as_queue(options = {}) ⇒ Object
Configuration options are:
size - specifies the bounded queue size. Example: acts_as_queue :size => 20
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/acts_as_queue.rb', line 28 def acts_as_queue( = {}) configuration = { :size => "0" } configuration.update() if .is_a?(Hash) class_eval " \n def self.queue_size\n '\#{configuration[:size]}'.to_i\n end\n EOV\nend\n" |
#count ⇒ Object
Returns the number of items in the queue.
57 58 59 |
# File 'lib/acts_as_queue.rb', line 57 def count self.find(:all).count end |
#pop ⇒ Object
Pops the earliest item from the queue.
50 51 52 53 54 |
# File 'lib/acts_as_queue.rb', line 50 def pop attributes = self.first.attributes self.first.delete attributes.inject({}) { |attribute,(k,v)| attribute[k.to_sym] = v; attribute } end |
#push(attributes) ⇒ Object
Pushes an item onto the queue.
41 42 43 44 45 46 47 |
# File 'lib/acts_as_queue.rb', line 41 def push(attributes) if (self.queue_size > 0) && (self.count == self.queue_size) popped = self.pop end self.create(attributes) end |