replica 1.0.0 → 1.0.1

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.
Files changed (5) hide show
  1. data/Rakefile +2 -2
  2. data/VERSION +1 -1
  3. data/lib/replica.rb +8 -0
  4. data/test/replica_test.rb +81 -58
  5. metadata +2 -2
data/Rakefile CHANGED
@@ -21,7 +21,7 @@ end
21
21
  require 'rake/testtask'
22
22
  Rake::TestTask.new(:test) do |test|
23
23
  test.libs << 'lib' << 'test'
24
- test.pattern = 'test/**/test_*.rb'
24
+ test.pattern = 'test/**/*_test.rb'
25
25
  test.verbose = true
26
26
  end
27
27
 
@@ -29,7 +29,7 @@ begin
29
29
  require 'rcov/rcovtask'
30
30
  Rcov::RcovTask.new do |test|
31
31
  test.libs << 'test'
32
- test.pattern = 'test/**/test_*.rb'
32
+ test.pattern = 'test/**/*_test.rb'
33
33
  test.verbose = true
34
34
  end
35
35
  rescue LoadError
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.0
1
+ 1.0.1
data/lib/replica.rb CHANGED
@@ -24,6 +24,14 @@ module ActiveRecord # :nodoc:
24
24
  with_replica(nil, &block)
25
25
  end
26
26
 
27
+ def with_slave_if(condition, &block)
28
+ condition ? with_slave(&block) : with_master(&block)
29
+ end
30
+
31
+ def with_slave_unless(condition, &block)
32
+ with_slave_if(!condition, &block)
33
+ end
34
+
27
35
  # Name of the connection pool. Used by ConnectionHandler to retrieve the current connection pool.
28
36
  def connection_pool_name # :nodoc:
29
37
  replica = current_replica_name
data/test/replica_test.rb CHANGED
@@ -1,91 +1,114 @@
1
1
  require 'helper'
2
2
 
3
3
  class ReplicaTest < ActiveRecord::TestCase
4
-
4
+
5
5
  context "without replica configuration" do
6
-
6
+
7
7
  setup do
8
8
  ActiveRecord::Base.configurations.delete('test_slave')
9
9
  ActiveRecord::Base.connection_handler.connection_pools.clear
10
10
  ActiveRecord::Base.establish_connection('test')
11
11
  end
12
-
12
+
13
13
  should "default to the master database" do
14
14
  Account.create!
15
-
15
+
16
16
  ActiveRecord::Base.with_slave { assert_using_master_db(Account) }
17
17
  Account.with_slave { assert_using_master_db(Account) }
18
18
  Ticket.with_slave { assert_using_master_db(Account) }
19
19
  end
20
-
20
+
21
21
  should "successfully execute queries" do
22
22
  Account.create!
23
23
  assert_using_master_db(Account)
24
-
24
+
25
25
  assert_equal Account.count, ActiveRecord::Base.with_slave { Account.count }
26
26
  assert_equal Account.count, Account.with_slave { Account.count }
27
27
  end
28
-
28
+
29
29
  end
30
-
30
+
31
31
  context "with replica configuration" do
32
-
32
+
33
33
  should "successfully execute queries" do
34
34
  assert_using_master_db(Account)
35
35
  Account.create!
36
-
36
+
37
37
  assert_not_equal Account.count, ActiveRecord::Base.with_slave { Account.count }
38
38
  assert_not_equal Account.count, Account.with_slave { Account.count }
39
39
  assert_equal Account.count, Ticket.with_slave { Account.count }
40
40
  end
41
-
41
+
42
42
  should "support model specific with_slave blocks" do
43
- assert_using_master_db(Account)
44
- assert_using_master_db(Ticket)
45
-
46
- Account.with_slave do
47
- assert_using_slave_db(Account)
48
- assert_using_master_db(Ticket)
49
- end
50
-
51
- assert_using_master_db(Account)
52
- assert_using_master_db(Ticket)
53
- end
54
-
55
- should "support global with_slave blocks" do
56
- assert_using_master_db(Account)
57
- assert_using_master_db(Ticket)
58
-
59
- ActiveRecord::Base.with_slave do
60
- assert_using_slave_db(Account)
61
- assert_using_slave_db(Ticket)
62
- end
63
-
64
- assert_using_master_db(Account)
65
- assert_using_master_db(Ticket)
66
- end
67
-
68
- should_eventually "support nested with_* blocks" do
69
-
70
- assert_using_master_db(Account)
71
- assert_using_master_db(Ticket)
72
-
73
- ActiveRecord::Base.with_slave do
74
- assert_using_slave_db(Account)
75
- assert_using_slave_db(Ticket)
76
-
77
- Account.with_master do
78
- assert_using_master_db(Account)
79
- assert_using_slave_db(Ticket)
80
- end
81
-
82
- assert_using_slave_db(Account)
83
- assert_using_slave_db(Ticket)
84
- end
85
-
86
- assert_using_master_db(Account)
87
- assert_using_master_db(Ticket)
88
- end
89
-
43
+ assert_using_master_db(Account)
44
+ assert_using_master_db(Ticket)
45
+
46
+ Account.with_slave do
47
+ assert_using_slave_db(Account)
48
+ assert_using_master_db(Ticket)
49
+ end
50
+
51
+ assert_using_master_db(Account)
52
+ assert_using_master_db(Ticket)
53
+ end
54
+
55
+ should "support global with_slave blocks" do
56
+ assert_using_master_db(Account)
57
+ assert_using_master_db(Ticket)
58
+
59
+ ActiveRecord::Base.with_slave do
60
+ assert_using_slave_db(Account)
61
+ assert_using_slave_db(Ticket)
62
+ end
63
+
64
+ assert_using_master_db(Account)
65
+ assert_using_master_db(Ticket)
66
+ end
67
+
68
+ should "support conditional methods" do
69
+ assert_using_master_db(Account)
70
+
71
+ Account.with_slave_if(true) do
72
+ assert_using_slave_db(Account)
73
+ end
74
+
75
+ assert_using_master_db(Account)
76
+
77
+ Account.with_slave_if(false) do
78
+ assert_using_master_db(Account)
79
+ end
80
+
81
+ Account.with_slave_unless(true) do
82
+ assert_using_master_db(Account)
83
+ end
84
+
85
+ Account.with_slave_unless(false) do
86
+ assert_using_slave_db(Account)
87
+ end
88
+
89
+ end
90
+
91
+ should_eventually "support nested with_* blocks" do
92
+
93
+ assert_using_master_db(Account)
94
+ assert_using_master_db(Ticket)
95
+
96
+ ActiveRecord::Base.with_slave do
97
+ assert_using_slave_db(Account)
98
+ assert_using_slave_db(Ticket)
99
+
100
+ Account.with_master do
101
+ assert_using_master_db(Account)
102
+ assert_using_slave_db(Ticket)
103
+ end
104
+
105
+ assert_using_slave_db(Account)
106
+ assert_using_slave_db(Ticket)
107
+ end
108
+
109
+ assert_using_master_db(Account)
110
+ assert_using_master_db(Ticket)
111
+ end
112
+
90
113
  end
91
114
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: replica
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eric Chapweske
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2009-12-04 00:00:00 -08:00
13
+ date: 2010-01-27 00:00:00 -08:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency