fresh_connection 0.1.2 → 0.1.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5020cb29611bf24a7ee6063ce70c1df0c58fc241
4
- data.tar.gz: 2c4e322e659d9cb8cc96c71a670c8cfff55bcbc7
3
+ metadata.gz: f83d1ba0780db658b9d3e9879476a4992b3f43ba
4
+ data.tar.gz: f25542e364f014b4fae17820715f91bb24ce4e1c
5
5
  SHA512:
6
- metadata.gz: a9e952072b5549d5b36fb2702f9ddee6a0299b9e8360c9efb668afd7b5684d5d214d79f17d82fc619185de642668a03cdd74d046a119afebe4f3c75c274d7afe
7
- data.tar.gz: 886a5c6650c1fc07cdce4191b0b3d00b808dc3219cb1169f62b0a2027d4c72b529b7779241e64085d0122d283b5bc4d85597067501429d8e5f3ffa1c05714ccb
6
+ metadata.gz: 7351305422d63add314a3a3a2aea341557d9338fae72af84c252e311cb7f5f3a0b2c669ec79d2230889e373acf094f8a3670006c2093a6c17027abd4af5acc34
7
+ data.tar.gz: 8a19c6258fcff2798143c6a807e538980f9dc3afa73b689f9b028dc2cdb126acdab0f6deb2b7af72e701bf24933664a725944c807a2f5fa2bcb78fcbd291687c
@@ -5,7 +5,7 @@ module ActiveRecord
5
5
  def exec_queries_with_slave_connection
6
6
  return @records if loaded?
7
7
 
8
- FreshConnection::SlaveConnection.manage_access(@klass.name, go_slave?) do
8
+ FreshConnection::SlaveConnection.manage_access(@klass, go_slave?) do
9
9
  exec_queries_without_slave_connection
10
10
  end
11
11
  end
@@ -1,15 +1,23 @@
1
1
  module FreshConnection
2
2
  class ConnectionManager
3
+ def initialize
4
+ @mutex = Mutex.new
5
+ end
6
+
3
7
  def slave_connection
4
- @slave_connections ||= {}
5
- @slave_connections[current_thread_id] ||= new_connection
8
+ @mutex.synchronize do
9
+ @slave_connections ||= {}
10
+ @slave_connections[current_thread_id] ||= new_connection
11
+ end
6
12
  end
7
13
 
8
14
  def put_aside!
9
- if @slave_connections.present?
10
- @slave_connections.each_value{|c| c && c.disconnect! rescue nil}
15
+ @mutex.synchronize do
16
+ if @slave_connections.present?
17
+ @slave_connections.each_value{|c| c && c.disconnect! rescue nil}
18
+ end
19
+ @slave_connections = {}
11
20
  end
12
- @slave_connections = {}
13
21
  end
14
22
 
15
23
  private
@@ -18,8 +18,8 @@ module FreshConnection
18
18
  connection_manager.put_aside!
19
19
  end
20
20
 
21
- def manage_access(model_name, go_slave, &block)
22
- if ignore_model?(model_name)
21
+ def manage_access(model_klass, go_slave, &block)
22
+ if ignore_model?(model_klass)
23
23
  force_master_access(&block)
24
24
  else
25
25
  target = go_slave ? :slave : :master
@@ -36,8 +36,17 @@ module FreshConnection
36
36
  Thread.current[TARGET] == :slave
37
37
  end
38
38
 
39
- def ignore_model?(model_name)
40
- (@ignore_models || []).include?(model_name)
39
+ def ignore_model?(model_klass)
40
+ (@ignore_models || []).one? do |ignore_model|
41
+ case ignore_model
42
+ when String
43
+ ignore_model == model_klass.name
44
+ when ActiveRecord::Base
45
+ model_klass.ancestors.include?(ignore_model)
46
+ else
47
+ false
48
+ end
49
+ end
41
50
  end
42
51
 
43
52
  def ignore_configure_connection?
@@ -1,4 +1,4 @@
1
1
  module FreshConnection
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
4
4
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fresh_connection
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tsukasa OISHI
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-08 00:00:00.000000000 Z
11
+ date: 2013-12-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord