slavery 1.3.0 → 1.4.0
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.
- checksums.yaml +4 -4
- data/README.md +15 -1
- data/lib/slavery.rb +13 -10
- data/lib/slavery/version.rb +1 -1
- data/spec/slavery_spec.rb +17 -2
- metadata +13 -13
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bb453d9f6d7b01c9edb5580517cda63135951dec
|
4
|
+
data.tar.gz: 26bdf5fad5cb4c3abf80589c5a4264e123b60b9f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 47905eaef67f0ea6533968a7e303f281f05e3a346e0c1d2012d5ec43a9fc8a4818750bc8a6ea81f31640d34e5b3b3d3ad9b8014c1aafee799762c729b9cc8f69
|
7
|
+
data.tar.gz: 920950c4e4996624b39078c0cb4cc9da0ffeeede17a17e68569fc199822ccf266f167fae02cc65a23be409235e4848796306185716fc9c502ebba6c0f9c4ff01
|
data/README.md
CHANGED
@@ -6,7 +6,7 @@ Probably you just start off with one single database. As your app grows, you wou
|
|
6
6
|
|
7
7
|
* Conservative - Safe by default. Installing Slavery won't change your app's current behavior.
|
8
8
|
* Future proof - No dirty hacks, simply works as a proxy for `ActiveRecord::Base.connection`.
|
9
|
-
* Simple -
|
9
|
+
* Simple - Only 100+ LOC, you can read the entire source and completely stay in control.
|
10
10
|
|
11
11
|
Slavery works with ActiveRecord 3 or later.
|
12
12
|
|
@@ -128,3 +128,17 @@ ActiveRecord::Base.configurations = {
|
|
128
128
|
}
|
129
129
|
ActiveRecord::Base.establish_connection(:development)
|
130
130
|
```
|
131
|
+
|
132
|
+
## Custom slave key in database.yml
|
133
|
+
|
134
|
+
This is useful for deploying on EngineYard where the configuration key in database.yml is simple "slave". Put the following line in `config/initializers/slavery.rb`.
|
135
|
+
|
136
|
+
```ruby
|
137
|
+
Slavery.spec_key = "slave" #instead of production_slave
|
138
|
+
```
|
139
|
+
|
140
|
+
Alternatively you can pass it a lambda for dynamically setting this.
|
141
|
+
|
142
|
+
```ruby
|
143
|
+
Slavery.spec_key = lambda{ "#{Slavery.env}_slave" }
|
144
|
+
```
|
data/lib/slavery.rb
CHANGED
@@ -16,9 +16,17 @@ module Slavery
|
|
16
16
|
|
17
17
|
class Error < StandardError; end
|
18
18
|
|
19
|
-
mattr_accessor :disabled
|
19
|
+
mattr_accessor :disabled, :env, :spec_key
|
20
20
|
|
21
21
|
class << self
|
22
|
+
def spec_key
|
23
|
+
case @@spec_key
|
24
|
+
when String then @@spec_key
|
25
|
+
when Proc then @@spec_key = @@spec_key.call
|
26
|
+
when NilClass then @@spec_key = "#{Slavery.env}_slave"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
22
30
|
def on_slave(&block)
|
23
31
|
run true, &block
|
24
32
|
end
|
@@ -36,12 +44,7 @@ module Slavery
|
|
36
44
|
end
|
37
45
|
|
38
46
|
def env
|
39
|
-
|
40
|
-
@env
|
41
|
-
end
|
42
|
-
|
43
|
-
def env=(string)
|
44
|
-
@env = ActiveSupport::StringInquirer.new(string)
|
47
|
+
@@env ||= defined?(Rails) ? Rails.env.to_s : 'development'
|
45
48
|
end
|
46
49
|
end
|
47
50
|
|
@@ -89,9 +92,9 @@ module Slavery
|
|
89
92
|
"SlaveConnectionHolder"
|
90
93
|
end
|
91
94
|
|
92
|
-
spec = [
|
93
|
-
ActiveRecord::Base.configurations[
|
94
|
-
end or raise Error.new("#{Slavery.
|
95
|
+
spec = [Slavery.spec_key, Slavery.env].find do |spec_key|
|
96
|
+
ActiveRecord::Base.configurations[spec_key]
|
97
|
+
end or raise Error.new("#{Slavery.spec_key} or #{Slavery.env} must exist!")
|
95
98
|
|
96
99
|
establish_connection spec
|
97
100
|
}
|
data/lib/slavery/version.rb
CHANGED
data/spec/slavery_spec.rb
CHANGED
@@ -53,6 +53,21 @@ describe Slavery do
|
|
53
53
|
Slavery.on_slave { User.slaveryable?.should == false }
|
54
54
|
end
|
55
55
|
|
56
|
+
it 'sets the Slavery database spec name by configuration' do
|
57
|
+
Slavery.spec_key = "custom_slave"
|
58
|
+
Slavery.spec_key.should eq 'custom_slave'
|
59
|
+
|
60
|
+
Slavery.spec_key = lambda{
|
61
|
+
"kewl_slave"
|
62
|
+
}
|
63
|
+
Slavery.spec_key.should eq "kewl_slave"
|
64
|
+
|
65
|
+
Slavery.spec_key = lambda{
|
66
|
+
"#{Slavery.env}_slave"
|
67
|
+
}
|
68
|
+
Slavery.spec_key.should eq "test_slave"
|
69
|
+
end
|
70
|
+
|
56
71
|
it 'works with scopes' do
|
57
72
|
User.count.should == 2
|
58
73
|
User.on_slave.count.should == 1
|
@@ -78,14 +93,14 @@ describe Slavery do
|
|
78
93
|
end
|
79
94
|
|
80
95
|
it 'connects to master if slave configuration not specified' do
|
81
|
-
ActiveRecord::Base.configurations[
|
96
|
+
ActiveRecord::Base.configurations[Slavery.spec_key] = nil
|
82
97
|
|
83
98
|
Slavery.on_slave { User.count }.should == 2
|
84
99
|
end
|
85
100
|
|
86
101
|
it 'raises error when no configuration found' do
|
87
102
|
ActiveRecord::Base.configurations['test'] = nil
|
88
|
-
ActiveRecord::Base.configurations[
|
103
|
+
ActiveRecord::Base.configurations[Slavery.spec_key] = nil
|
89
104
|
|
90
105
|
expect { Slavery.on_slave { User.count } }.to raise_error(Slavery::Error)
|
91
106
|
end
|
metadata
CHANGED
@@ -1,55 +1,55 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: slavery
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kenn Ejima
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2014-03-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: 3.0.0
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: 3.0.0
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rspec
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: sqlite3
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- -
|
52
|
+
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
55
|
description: Simple, conservative slave reads for ActiveRecord
|
@@ -59,8 +59,8 @@ executables: []
|
|
59
59
|
extensions: []
|
60
60
|
extra_rdoc_files: []
|
61
61
|
files:
|
62
|
-
- .gitignore
|
63
|
-
- .rspec
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
64
|
- Gemfile
|
65
65
|
- LICENSE.txt
|
66
66
|
- README.md
|
@@ -81,17 +81,17 @@ require_paths:
|
|
81
81
|
- lib
|
82
82
|
required_ruby_version: !ruby/object:Gem::Requirement
|
83
83
|
requirements:
|
84
|
-
- -
|
84
|
+
- - ">="
|
85
85
|
- !ruby/object:Gem::Version
|
86
86
|
version: '0'
|
87
87
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
88
|
requirements:
|
89
|
-
- -
|
89
|
+
- - ">="
|
90
90
|
- !ruby/object:Gem::Version
|
91
91
|
version: '0'
|
92
92
|
requirements: []
|
93
93
|
rubyforge_project:
|
94
|
-
rubygems_version: 2.0.
|
94
|
+
rubygems_version: 2.2.0.rc.1
|
95
95
|
signing_key:
|
96
96
|
specification_version: 4
|
97
97
|
summary: Simple, conservative slave reads for ActiveRecord
|