after_commit_queue 0.0.2 → 0.0.3

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/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(:stop_server)
44
+ run_after_commit do
45
+ stop_server
46
+ end
45
47
  end
46
48
 
47
49
  def start_server; @started = true end
@@ -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 |method|
14
- send(method)
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
 
@@ -1,3 +1,3 @@
1
1
  module AfterCommitQueue
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -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
Binary file
@@ -73,3 +73,25 @@ Migrating to CreateServers (20120622135717)
73
73
   (1.1ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
74
74
   (0.1ms) SELECT version FROM "schema_migrations"
75
75
   (1.0ms) INSERT INTO "schema_migrations" (version) VALUES ('20120622135717')
76
+ Connecting to database specified by database.yml
77
+  (0.6ms) select sqlite_version(*)
78
+  (1.5ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL)
79
+  (0.0ms) PRAGMA index_list("schema_migrations")
80
+  (1.1ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
81
+  (0.1ms) SELECT "schema_migrations"."version" FROM "schema_migrations" 
82
+ Migrating to CreateServers (20120622135717)
83
+  (0.0ms) begin transaction
84
+  (0.4ms) CREATE TABLE "servers" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "state" varchar(255), "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL) 
85
+  (0.1ms) INSERT INTO "schema_migrations" ("version") VALUES ('20120622135717')
86
+  (1.0ms) commit transaction
87
+  (0.2ms) select sqlite_version(*)
88
+  (0.1ms) SELECT "schema_migrations"."version" FROM "schema_migrations" 
89
+  (0.0ms) PRAGMA index_list("servers")
90
+  (0.1ms) SELECT "schema_migrations"."version" FROM "schema_migrations" 
91
+  (0.2ms) select sqlite_version(*)
92
+  (1.3ms) CREATE TABLE "servers" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "state" varchar(255), "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL) 
93
+  (1.2ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL)
94
+  (0.0ms) PRAGMA index_list("schema_migrations")
95
+  (1.0ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
96
+  (0.1ms) SELECT version FROM "schema_migrations"
97
+  (1.1ms) INSERT INTO "schema_migrations" (version) VALUES ('20120622135717')
@@ -300,3 +300,19 @@ Connecting to database specified by database.yml
300
300
   (0.0ms) begin transaction
301
301
  SQL (0.3ms) 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
   (0.9ms) commit transaction
303
+ Connecting to database specified by database.yml
304
+  (0.0ms) begin transaction
305
+ SQL (5.2ms) 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
+  (1.0ms) commit transaction
307
+  (0.0ms) begin transaction
308
+  (0.3ms) UPDATE "servers" SET "state" = 'turned_off', "updated_at" = '2012-08-01 10:44:49.378234' WHERE "servers"."id" = 1
309
+  (0.9ms) commit transaction
310
+  (0.1ms) begin transaction
311
+ SQL (0.4ms) 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
+  (1.0ms) commit transaction
313
+  (0.0ms) begin transaction
314
+  (0.2ms) UPDATE "servers" SET "state" = 'crashed', "updated_at" = '2012-08-01 10:44:49.384841' WHERE "servers"."id" = 2
315
+  (0.7ms) commit transaction
316
+  (0.1ms) begin transaction
317
+ SQL (0.3ms) 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
+  (1.0ms) commit transaction
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.2
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-07-18 00:00:00.000000000 Z
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: -2163612595833260696
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: -2163612595833260696
122
+ hash: 3808905130846087768
123
123
  requirements: []
124
124
  rubyforge_project:
125
125
  rubygems_version: 1.8.24