after_commit_queue 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +4 -2
- data/lib/after_commit_queue.rb +5 -4
- data/lib/after_commit_queue/version.rb +1 -1
- data/test/after_commit_queue_test.rb +12 -0
- data/test/dummy/app/models/server.rb +10 -1
- data/test/dummy/db/development.sqlite3 +0 -0
- data/test/dummy/db/test.sqlite3 +0 -0
- data/test/dummy/log/development.log +22 -0
- data/test/dummy/log/test.log +16 -0
- metadata +4 -4
data/README.md
CHANGED
@@ -19,7 +19,7 @@ gem 'after_commit_queue'
|
|
19
19
|
|
20
20
|
### Usage
|
21
21
|
|
22
|
-
Include AfterCommitQueue module in your ActiveRecord model and you're ready to go.
|
22
|
+
Include AfterCommitQueue module in your ActiveRecord model and you're ready to go. When registering a hook with run_after_commit you can supply either a method symbol or a block. No parameter is supplied when using the block form.
|
23
23
|
|
24
24
|
```ruby
|
25
25
|
class Server < ActiveRecord::Base
|
@@ -41,7 +41,9 @@ class Server < ActiveRecord::Base
|
|
41
41
|
end
|
42
42
|
|
43
43
|
def schedule_stop
|
44
|
-
run_after_commit
|
44
|
+
run_after_commit do
|
45
|
+
stop_server
|
46
|
+
end
|
45
47
|
end
|
46
48
|
|
47
49
|
def start_server; @started = true end
|
data/lib/after_commit_queue.rb
CHANGED
@@ -10,15 +10,16 @@ module AfterCommitQueue
|
|
10
10
|
# Protected: Is called as after_commit callback
|
11
11
|
# runs methods from the queue and clears the queue afterwards
|
12
12
|
def _run_after_commit_queue
|
13
|
-
_after_commit_queue.each do |
|
14
|
-
|
13
|
+
_after_commit_queue.each do |action|
|
14
|
+
action.call
|
15
15
|
end
|
16
16
|
@after_commit_queue.clear
|
17
17
|
end
|
18
18
|
|
19
19
|
# Protected: Add method to after commit queue
|
20
|
-
def run_after_commit(method)
|
21
|
-
_after_commit_queue << method
|
20
|
+
def run_after_commit(method = nil, &block)
|
21
|
+
_after_commit_queue << Proc.new { self.send(method) } if method
|
22
|
+
_after_commit_queue << block if block
|
22
23
|
true
|
23
24
|
end
|
24
25
|
|
@@ -16,6 +16,18 @@ class AfterCommitQueueTest < ActiveSupport::TestCase
|
|
16
16
|
assert @server.started
|
17
17
|
end
|
18
18
|
|
19
|
+
test "run blocks after transaction is committed" do
|
20
|
+
@server.start!
|
21
|
+
assert !@server.meditating
|
22
|
+
|
23
|
+
@server.transaction do
|
24
|
+
@server.crash!
|
25
|
+
assert !@server.meditating
|
26
|
+
end
|
27
|
+
|
28
|
+
assert @server.meditating
|
29
|
+
end
|
30
|
+
|
19
31
|
test "clear queue after methods from are called" do
|
20
32
|
@server.start!
|
21
33
|
@server.started = false
|
@@ -1,13 +1,16 @@
|
|
1
1
|
class Server < ActiveRecord::Base
|
2
2
|
include AfterCommitQueue
|
3
3
|
|
4
|
-
attr_accessor :started, :stopped
|
4
|
+
attr_accessor :started, :stopped, :meditating
|
5
5
|
|
6
6
|
state_machine :state, :initial => :pending do
|
7
7
|
after_transition :pending => :running, :do => :schedule_start
|
8
|
+
after_transition :running => :crashed, :do => :schedule_guru_meditation
|
8
9
|
after_transition :running => :turned_off, :do => :schedule_stop
|
10
|
+
|
9
11
|
event(:start) { transition :pending => :running }
|
10
12
|
event(:stop) { transition :running => :turned_off }
|
13
|
+
event(:crash) { transition :running => :crashed }
|
11
14
|
end
|
12
15
|
|
13
16
|
def schedule_start
|
@@ -18,6 +21,12 @@ class Server < ActiveRecord::Base
|
|
18
21
|
run_after_commit(:stop_server)
|
19
22
|
end
|
20
23
|
|
24
|
+
def schedule_guru_meditation
|
25
|
+
run_after_commit do
|
26
|
+
@meditating = true
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
21
30
|
def start_server; @started = true end
|
22
31
|
def stop_server; @stopped = true end
|
23
32
|
end
|
Binary file
|
data/test/dummy/db/test.sqlite3
CHANGED
Binary file
|
@@ -73,3 +73,25 @@ Migrating to CreateServers (20120622135717)
|
|
73
73
|
[1m[35m (1.1ms)[0m CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
|
74
74
|
[1m[36m (0.1ms)[0m [1mSELECT version FROM "schema_migrations"[0m
|
75
75
|
[1m[35m (1.0ms)[0m INSERT INTO "schema_migrations" (version) VALUES ('20120622135717')
|
76
|
+
Connecting to database specified by database.yml
|
77
|
+
[1m[36m (0.6ms)[0m [1mselect sqlite_version(*)[0m
|
78
|
+
[1m[35m (1.5ms)[0m CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL)
|
79
|
+
[1m[36m (0.0ms)[0m [1mPRAGMA index_list("schema_migrations")[0m
|
80
|
+
[1m[35m (1.1ms)[0m CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
|
81
|
+
[1m[36m (0.1ms)[0m [1mSELECT "schema_migrations"."version" FROM "schema_migrations" [0m
|
82
|
+
Migrating to CreateServers (20120622135717)
|
83
|
+
[1m[35m (0.0ms)[0m begin transaction
|
84
|
+
[1m[36m (0.4ms)[0m [1mCREATE TABLE "servers" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "state" varchar(255), "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL) [0m
|
85
|
+
[1m[35m (0.1ms)[0m INSERT INTO "schema_migrations" ("version") VALUES ('20120622135717')
|
86
|
+
[1m[36m (1.0ms)[0m [1mcommit transaction[0m
|
87
|
+
[1m[35m (0.2ms)[0m select sqlite_version(*)
|
88
|
+
[1m[36m (0.1ms)[0m [1mSELECT "schema_migrations"."version" FROM "schema_migrations" [0m
|
89
|
+
[1m[35m (0.0ms)[0m PRAGMA index_list("servers")
|
90
|
+
[1m[36m (0.1ms)[0m [1mSELECT "schema_migrations"."version" FROM "schema_migrations" [0m
|
91
|
+
[1m[35m (0.2ms)[0m select sqlite_version(*)
|
92
|
+
[1m[36m (1.3ms)[0m [1mCREATE TABLE "servers" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "state" varchar(255), "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL) [0m
|
93
|
+
[1m[35m (1.2ms)[0m CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL)
|
94
|
+
[1m[36m (0.0ms)[0m [1mPRAGMA index_list("schema_migrations")[0m
|
95
|
+
[1m[35m (1.0ms)[0m CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
|
96
|
+
[1m[36m (0.1ms)[0m [1mSELECT version FROM "schema_migrations"[0m
|
97
|
+
[1m[35m (1.1ms)[0m INSERT INTO "schema_migrations" (version) VALUES ('20120622135717')
|
data/test/dummy/log/test.log
CHANGED
@@ -300,3 +300,19 @@ Connecting to database specified by database.yml
|
|
300
300
|
[1m[36m (0.0ms)[0m [1mbegin transaction[0m
|
301
301
|
[1m[35mSQL (0.3ms)[0m INSERT INTO "servers" ("created_at", "state", "updated_at") VALUES (?, ?, ?) [["created_at", Wed, 18 Jul 2012 21:16:29 UTC +00:00], ["state", "running"], ["updated_at", Wed, 18 Jul 2012 21:16:29 UTC +00:00]]
|
302
302
|
[1m[36m (0.9ms)[0m [1mcommit transaction[0m
|
303
|
+
Connecting to database specified by database.yml
|
304
|
+
[1m[36m (0.0ms)[0m [1mbegin transaction[0m
|
305
|
+
[1m[35mSQL (5.2ms)[0m INSERT INTO "servers" ("created_at", "state", "updated_at") VALUES (?, ?, ?) [["created_at", Wed, 01 Aug 2012 10:44:49 UTC +00:00], ["state", "running"], ["updated_at", Wed, 01 Aug 2012 10:44:49 UTC +00:00]]
|
306
|
+
[1m[36m (1.0ms)[0m [1mcommit transaction[0m
|
307
|
+
[1m[35m (0.0ms)[0m begin transaction
|
308
|
+
[1m[36m (0.3ms)[0m [1mUPDATE "servers" SET "state" = 'turned_off', "updated_at" = '2012-08-01 10:44:49.378234' WHERE "servers"."id" = 1[0m
|
309
|
+
[1m[35m (0.9ms)[0m commit transaction
|
310
|
+
[1m[36m (0.1ms)[0m [1mbegin transaction[0m
|
311
|
+
[1m[35mSQL (0.4ms)[0m INSERT INTO "servers" ("created_at", "state", "updated_at") VALUES (?, ?, ?) [["created_at", Wed, 01 Aug 2012 10:44:49 UTC +00:00], ["state", "running"], ["updated_at", Wed, 01 Aug 2012 10:44:49 UTC +00:00]]
|
312
|
+
[1m[36m (1.0ms)[0m [1mcommit transaction[0m
|
313
|
+
[1m[35m (0.0ms)[0m begin transaction
|
314
|
+
[1m[36m (0.2ms)[0m [1mUPDATE "servers" SET "state" = 'crashed', "updated_at" = '2012-08-01 10:44:49.384841' WHERE "servers"."id" = 2[0m
|
315
|
+
[1m[35m (0.7ms)[0m commit transaction
|
316
|
+
[1m[36m (0.1ms)[0m [1mbegin transaction[0m
|
317
|
+
[1m[35mSQL (0.3ms)[0m INSERT INTO "servers" ("created_at", "state", "updated_at") VALUES (?, ?, ?) [["created_at", Wed, 01 Aug 2012 10:44:49 UTC +00:00], ["state", "running"], ["updated_at", Wed, 01 Aug 2012 10:44:49 UTC +00:00]]
|
318
|
+
[1m[36m (1.0ms)[0m [1mcommit transaction[0m
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: after_commit_queue
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2012-
|
13
|
+
date: 2012-08-01 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rails
|
@@ -110,7 +110,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
110
110
|
version: '0'
|
111
111
|
segments:
|
112
112
|
- 0
|
113
|
-
hash:
|
113
|
+
hash: 3808905130846087768
|
114
114
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
115
115
|
none: false
|
116
116
|
requirements:
|
@@ -119,7 +119,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
119
119
|
version: '0'
|
120
120
|
segments:
|
121
121
|
- 0
|
122
|
-
hash:
|
122
|
+
hash: 3808905130846087768
|
123
123
|
requirements: []
|
124
124
|
rubyforge_project:
|
125
125
|
rubygems_version: 1.8.24
|