acts_as_queue 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in acts_as_queue.gemspec
4
+ gemspec
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (C) 2010, Matthew Lang
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,27 @@
1
+ = acts_as_queue
2
+
3
+ acts_as_queue is a plugin for ActiveRecord that allows you to turn any of your ActiveRecord models into a queue.
4
+
5
+ == Using the default queue size
6
+
7
+ class Post < ActiveRecord::Base
8
+ acts_as_queue
9
+ end
10
+
11
+ # Pushing new posts onto the queue.
12
+ Post.push :name => "Hello World"
13
+ Post.push :name => "Using Rails"
14
+
15
+ # Popping the item at the front of the queue.
16
+ Post.pop
17
+
18
+ == Using a queue size
19
+
20
+ class Message < ActiveRecord::Base
21
+ acts_as_queue :size => 10
22
+ end
23
+
24
+ # Pushing 20 new messages onto the queue.
25
+ (1..20).each { |i| Message.push :description => "Message #{i}" }
26
+
27
+ Message.count # Just 10 messages on the queue
data/Rakefile ADDED
@@ -0,0 +1,15 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'bundler'
4
+
5
+ Bundler::GemHelper.install_tasks
6
+
7
+ desc 'Default: run acts_as_queue unit tests.'
8
+ task :default => :test
9
+
10
+ desc 'Test the acts_as_queue gem.'
11
+ Rake::TestTask.new(:test) do |t|
12
+ t.libs << 'lib'
13
+ t.pattern = 'test/**/*_test.rb'
14
+ t.verbose = true
15
+ end
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "acts_as_queue"
6
+ s.version = "0.1.2"
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = ["Matthew Lang"]
9
+ s.email = ["matthew@matthewlang.co.uk"]
10
+ s.homepage = "http://github.com/matthewl/acts_as_queue"
11
+ s.summary = %q{Allows you to turn your ActiveRecord models into queues.}
12
+ s.description = %q{Allows you to turn your ActiveRecord models into queues.}
13
+
14
+ s.add_development_dependency "shoulda", [">= 2.11.3"]
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'acts_as_queue'
@@ -0,0 +1,41 @@
1
+ module ActsAsQueue
2
+
3
+ def self.included(base)
4
+ base.extend(ClassMethods)
5
+ end
6
+
7
+ module ClassMethods
8
+
9
+ def acts_as_queue(options = {})
10
+ configuration = { :size => "0" }
11
+ configuration.update(options) if options.is_a?(Hash)
12
+
13
+ class_eval <<-EOV
14
+
15
+ def self.queue_size
16
+ '#{configuration[:size]}'.to_i
17
+ end
18
+ EOV
19
+ end
20
+
21
+ def push(attributes)
22
+ if (self.queue_size > 0) && (self.count == self.queue_size)
23
+ popped = self.pop
24
+ end
25
+
26
+ self.create(attributes)
27
+ end
28
+
29
+ def pop
30
+ attributes = self.first.attributes
31
+ self.first.delete
32
+ attributes.inject({}) { |attribute,(k,v)| attribute[k.to_sym] = v; attribute }
33
+ end
34
+
35
+ def count
36
+ self.find(:all).count
37
+ end
38
+ end
39
+ end
40
+
41
+ ActiveRecord::Base.class_eval { include ActsAsQueue }
@@ -0,0 +1,113 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+ require 'active_record'
4
+ require 'shoulda'
5
+
6
+ require "#{File.dirname(__FILE__)}/../init.rb"
7
+
8
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
9
+ ActiveRecord::Migration.verbose = false
10
+
11
+ def setup_db
12
+ ActiveRecord::Schema.define(:version => 1) do
13
+ create_table :posts do |t|
14
+ t.string :name
15
+ t.string :slug
16
+ t.timestamps
17
+ end
18
+
19
+ create_table :messages do |t|
20
+ t.string :description
21
+ t.timestamps
22
+ end
23
+ end
24
+ end
25
+
26
+ def teardown_db
27
+ ActiveRecord::Base.connection.tables.each do |table|
28
+ ActiveRecord::Base.connection.drop_table(table)
29
+ end
30
+ end
31
+
32
+ class Post < ActiveRecord::Base
33
+ acts_as_queue
34
+ end
35
+
36
+ class Message < ActiveRecord::Base
37
+ acts_as_queue :size => 3
38
+ end
39
+
40
+ class QueueTest < Test::Unit::TestCase
41
+
42
+ def setup
43
+ setup_db
44
+ end
45
+
46
+ def teardown
47
+ teardown_db
48
+ end
49
+
50
+ context "A default queue model" do
51
+
52
+ should "have a zero queue size" do
53
+ assert_equal 0, Post.queue_size
54
+ end
55
+
56
+ should "push items on the queue" do
57
+ Post.push(:name => "How to write a Ruby app in 5 minutes", :slug => "write-app-5-minutes")
58
+ assert_equal 1, Post.count
59
+ assert_equal "How to write a Ruby app in 5 minutes", Post.first.name
60
+ assert_equal "write-app-5-minutes", Post.first.slug
61
+
62
+ Post.push(:name => "Debug your gems like a pro!", :slug => "debug-gems-like-pro")
63
+ assert_equal 2, Post.count
64
+ assert_equal "Debug your gems like a pro!", Post.last.name
65
+ assert_equal "debug-gems-like-pro", Post.last.slug
66
+ end
67
+
68
+ should "allow items to be popped" do
69
+ (1..2).each { |i| Post.push :name => "Post #{i}" }
70
+ assert_equal 2, Post.count
71
+
72
+ Post.pop
73
+ assert_equal 1, Post.count
74
+ assert_equal "Post 2", Post.first.name
75
+ end
76
+
77
+ should "return popped items as a hash" do
78
+ (1..2).each { |i| Post.push :name => "Post #{i}" }
79
+ popped = Post.pop
80
+
81
+ assert_kind_of Hash, popped
82
+ assert_equal "Post 1", popped[:name]
83
+ end
84
+
85
+ end
86
+
87
+ context "A custom queue model" do
88
+
89
+ should "allow a custom queue size" do
90
+ assert_equal 3, Message.queue_size
91
+ end
92
+
93
+ should "push items on the queue" do
94
+ (1..3).each { |i| Message.push :description => "Message #{i}" }
95
+ assert_equal 3, Message.count
96
+ end
97
+
98
+ should "automatically pop items if the queue size has been reached" do
99
+ (1..3).each { |i| Message.push :description => "Message #{i}" }
100
+ assert_equal 3, Message.count
101
+
102
+ Message.push :description => "Message 4"
103
+ assert_equal 3, Message.count
104
+ assert_equal "Message 4", Message.last.description
105
+ assert_equal "Message 2", Message.first.description
106
+
107
+ Message.push :description => "Message 5"
108
+ assert_equal 3, Message.count
109
+ assert_equal "Message 5", Message.last.description
110
+ assert_equal "Message 3", Message.first.description
111
+ end
112
+ end
113
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts_as_queue
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 31
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 1
10
- version: 0.1.1
9
+ - 2
10
+ version: 0.1.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Matthew Lang
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-10-26 00:00:00 +01:00
18
+ date: 2010-10-27 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -43,8 +43,16 @@ extensions: []
43
43
 
44
44
  extra_rdoc_files: []
45
45
 
46
- files: []
47
-
46
+ files:
47
+ - .gitignore
48
+ - Gemfile
49
+ - MIT-LICENSE
50
+ - README
51
+ - Rakefile
52
+ - acts_as_queue.gemspec
53
+ - init.rb
54
+ - lib/acts_as_queue.rb
55
+ - test/acts_as_queue_test.rb
48
56
  has_rdoc: true
49
57
  homepage: http://github.com/matthewl/acts_as_queue
50
58
  licenses: []
@@ -79,5 +87,5 @@ rubygems_version: 1.3.7
79
87
  signing_key:
80
88
  specification_version: 3
81
89
  summary: Allows you to turn your ActiveRecord models into queues.
82
- test_files: []
83
-
90
+ test_files:
91
+ - test/acts_as_queue_test.rb