switch_point 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c52a7c39b406241e2df69b1d0dfae3a6c45d15d3
4
- data.tar.gz: 606dd78b88ad59873c9d7e187c110163899f1ffb
3
+ metadata.gz: caaba1e17cb79206eb52931dadba051e12aa10a4
4
+ data.tar.gz: a5ad7388ad8989ddd5c6a9ad9dd3a33474d4b6a3
5
5
  SHA512:
6
- metadata.gz: 1e9097c7f4e5696728fc57a0f190ccd6a4c65d665f1fbb7cfbc89e67cd77658f288d8a4f16a7360e3e34c103a1fdbb050b132eafc13e077901f1af25f1d8cf75
7
- data.tar.gz: 02bcf5bc10f0c2c4a3b7ee04f3d33b1a94edcfc0f1b91ef1ff2251963052983c3d24cdb67f8a80b6e73daae802d1448352eb573a364d41559d228752efe89337
6
+ metadata.gz: 18ccaf6142ea40e8e9974cf7fb85f7b8d0bf8fe0737128f5745b66c896b39a8af5d32e5c3b238a5c1a0486a09686f5d69aa409f774f74c394182dc0e330d075d
7
+ data.tar.gz: 3d4eb2f58f0e548348250567bae0e01cc873f3a4a703dd9727565cadeaad83fad295cb016e0f4d5fddd2d8fe26cec1b42de84f6d75e4f4faaf974d2280244a77
@@ -1,3 +1,7 @@
1
+ ## 0.2.1 (2014-05-29)
2
+ - Add Proxy#switch_name to switch proxy configuration
3
+ - Fix weird nil error when Config#define_switch_point isn't called yet
4
+
1
5
  ## 0.2.0 (2014-05-29)
2
6
  - Always send destructive operations to writable connection
3
7
  - Fix bug on pooled connections
@@ -2,12 +2,15 @@ module SwitchPoint
2
2
  class Config
3
3
  def define_switch_point(name, config)
4
4
  assert_valid_config!(config)
5
+ switch_points[name] = config
6
+ end
7
+
8
+ def switch_points
5
9
  @switch_points ||= {}
6
- @switch_points[name] = config
7
10
  end
8
11
 
9
12
  def database_name(name, mode)
10
- @switch_points[name][mode]
13
+ switch_points[name][mode]
11
14
  end
12
15
 
13
16
  def model_name(name, mode)
@@ -15,11 +18,11 @@ module SwitchPoint
15
18
  end
16
19
 
17
20
  def fetch(name)
18
- @switch_points.fetch(name)
21
+ switch_points.fetch(name)
19
22
  end
20
23
 
21
24
  def keys
22
- @switch_points.keys
25
+ switch_points.keys
23
26
  end
24
27
 
25
28
  private
@@ -31,15 +31,15 @@ module SwitchPoint
31
31
  @switch_point_name = name
32
32
  end
33
33
 
34
+ def switch_point_proxy
35
+ ProxyRepository.checkout(@switch_point_name)
36
+ end
37
+
34
38
  private
35
39
 
36
40
  def assert_existing_switch_point!(name)
37
41
  SwitchPoint.config.fetch(name)
38
42
  end
39
-
40
- def switch_point_proxy
41
- ProxyRepository.checkout(@switch_point_name)
42
- end
43
43
  end
44
44
  end
45
45
  end
@@ -1,10 +1,12 @@
1
1
  module SwitchPoint
2
2
  class Proxy
3
+ attr_reader :initial_name
4
+
3
5
  def initialize(name)
4
- @models = {}
6
+ @initial_name = name
7
+ @current_name = name
5
8
  [:readonly, :writable].each do |mode|
6
9
  model = define_model(SwitchPoint.config.model_name(name, mode))
7
- @models[mode] = model
8
10
  model.establish_connection(SwitchPoint.config.database_name(name, mode))
9
11
  memorize_switch_point(name, mode, model.connection)
10
12
  end
@@ -46,8 +48,27 @@ module SwitchPoint
46
48
  @mode = saved_mode
47
49
  end
48
50
 
51
+ def switch_name(new_name, &block)
52
+ if block
53
+ begin
54
+ old_name = @current_name
55
+ @current_name = new_name
56
+ block.call
57
+ ensure
58
+ @current_name = old_name
59
+ end
60
+ else
61
+ @current_name = new_name
62
+ end
63
+ end
64
+
65
+ def reset_name!
66
+ @current_name = @initial_name
67
+ end
68
+
49
69
  def connection
50
- @models[@mode].connection
70
+ ProxyRepository.checkout(@current_name) # Ensure the target proxy is created
71
+ Proxy.const_get(SwitchPoint.config.model_name(@current_name, @mode)).connection
51
72
  end
52
73
  end
53
74
  end
@@ -1,3 +1,3 @@
1
1
  module SwitchPoint
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
@@ -142,4 +142,27 @@ RSpec.describe SwitchPoint::Model do
142
142
  end
143
143
  end
144
144
  end
145
+
146
+ describe '.switch_name' do
147
+ after do
148
+ Book.switch_point_proxy.reset_name!
149
+ end
150
+
151
+ it 'switches proxy configuration' do
152
+ Book.switch_point_proxy.switch_name(:comment)
153
+ expect(Book).to connect_to('comment_readonly.sqlite3')
154
+ expect(Publisher).to connect_to('comment_readonly.sqlite3')
155
+ end
156
+
157
+ context 'with block' do
158
+ it 'switches proxy configuration locally' do
159
+ Book.switch_point_proxy.switch_name(:comment) do
160
+ expect(Book).to connect_to('comment_readonly.sqlite3')
161
+ expect(Publisher).to connect_to('comment_readonly.sqlite3')
162
+ end
163
+ expect(Book).to connect_to('main_readonly.sqlite3')
164
+ expect(Publisher).to connect_to('main_readonly.sqlite3')
165
+ end
166
+ end
167
+ end
145
168
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: switch_point
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kohei Suzuki