fresh_connection 0.1.0 → 0.1.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bb171789539cc2eaf31793f904e53f4c0a9f1114
4
- data.tar.gz: be8ef001eec63ca7a879d3950fb460b7f300bdb6
3
+ metadata.gz: 3707472745859247112e68f0b348876811dcea97
4
+ data.tar.gz: 032461a6457f8ff4b5f566ef54e4ace7160aeebd
5
5
  SHA512:
6
- metadata.gz: becd1aed70d40cecaa8155630609e7ec13d9fd62c4f4ce6fb92e4f6e54bdeb025915aa55dc9deb226da2a89f6c0bfb491353dea84b68595bf015599108c8e238
7
- data.tar.gz: 4aa63bc28708181c47df8dfd211e6fceb2df57dc2a45d1e053a0594db9d1b82b6d366c0186c77f17f1b8faa80a5fccd26f4d9aa8fe51a88b9a9b0451aee32fcf
6
+ metadata.gz: 504d3589083726b6403c1e828dbe5d4b692e6b0bd695c21422a23daad8f6427dc8151fb9f00dd1dd18c4929189dbbf621d736e7fc176b52fa9fe8f7d17e87649
7
+ data.tar.gz: ead47efa46d780b2a1e0586c2a5be2347c1f4e8bf7a47323d38cd8b90d9f33af344d874642792a8c59ba01b3444627f24bca04ad33825b7a1c67a708c359076f
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
- spec.add_dependency 'activerecord', '~> 3.2.0'
21
+ spec.add_dependency 'activerecord', '>= 3.2.0'
22
22
 
23
23
  spec.add_development_dependency "bundler", "~> 1.3"
24
24
  spec.add_development_dependency "rake"
@@ -13,11 +13,11 @@ module ActiveRecord
13
13
  private
14
14
 
15
15
  def change_connection
16
- master_con, @connection =
17
- @connection, FreshConnection::SlaveConnection.connection.raw_connection
16
+ master_connection, @connection =
17
+ @connection, FreshConnection::SlaveConnection.raw_connection
18
18
  yield
19
19
  ensure
20
- @connection = master_con
20
+ @connection = master_connection
21
21
  end
22
22
  end
23
23
  end
@@ -5,18 +5,20 @@ module ActiveRecord
5
5
  def exec_queries_with_slave_connection
6
6
  return @records if loaded?
7
7
 
8
- if FreshConnection::SlaveConnection.ignore_model?(@klass.name)
9
- FreshConnection::SlaveConnection.force_master_access { exec_queries_without_slave_connection }
10
- elsif go_slave?
11
- FreshConnection::SlaveConnection.slave_access { exec_queries_without_slave_connection }
12
- else
13
- FreshConnection::SlaveConnection.master_access { exec_queries_without_slave_connection }
8
+ FreshConnection::SlaveConnection.manage_access(@klass.name, go_slave?) do
9
+ exec_queries_without_slave_connection
14
10
  end
15
11
  end
16
12
  alias_method_chain :exec_queries, :slave_connection
17
13
 
18
- def go_slave?
19
- connection.open_transactions == 0 && (@readonly_value.nil? || @readonly_value)
14
+ if Rails.version.to_f > 3
15
+ def go_slave?
16
+ connection.open_transactions == 0 && (readonly_value.nil? || readonly_value)
17
+ end
18
+ else
19
+ def go_slave?
20
+ connection.open_transactions == 0 && (@readonly_value.nil? || @readonly_value)
21
+ end
20
22
  end
21
23
  end
22
24
  end
@@ -6,8 +6,8 @@ module FreshConnection
6
6
  class << self
7
7
  attr_writer :ignore_models, :ignore_configure_connection, :master_clear_connection
8
8
 
9
- def connection
10
- slave_connection
9
+ def raw_connection
10
+ slave_connection.raw_connection
11
11
  end
12
12
 
13
13
  def clear_all_connections!
@@ -17,26 +17,18 @@ module FreshConnection
17
17
  @slave_connections = {}
18
18
  end
19
19
 
20
- def slave_access
21
- access_in(:slave)
22
- yield
23
- ensure
24
- access_out
25
- end
26
-
27
- def master_access
28
- access_in(:master)
29
- yield
30
- ensure
31
- access_out
32
- end
33
-
34
- def force_master_access
35
- now_target = Thread.current[TARGET]
36
- Thread.current[TARGET] = :master
37
- yield
38
- ensure
39
- Thread.current[TARGET] = now_target
20
+ def manage_access(model_name, go_slave, &block)
21
+ if ignore_model?(model_name)
22
+ force_master_access(&block)
23
+ else
24
+ target = go_slave ? :slave : :master
25
+ begin
26
+ access_in(target)
27
+ block.call
28
+ ensure
29
+ access_out
30
+ end
31
+ end
40
32
  end
41
33
 
42
34
  def slave_access?
@@ -57,6 +49,14 @@ module FreshConnection
57
49
 
58
50
  private
59
51
 
52
+ def force_master_access
53
+ now_target = Thread.current[TARGET]
54
+ Thread.current[TARGET] = :master
55
+ yield
56
+ ensure
57
+ Thread.current[TARGET] = now_target
58
+ end
59
+
60
60
  def access_in(target)
61
61
  Thread.current[COUNT] = (Thread.current[COUNT] || 0) + 1
62
62
  Thread.current[TARGET] ||= target
@@ -70,7 +70,6 @@ module FreshConnection
70
70
  end
71
71
  end
72
72
 
73
-
74
73
  def slave_connection
75
74
  @slave_connections ||= {}
76
75
  @slave_connections[current_thread_id] ||= new_connection
@@ -1,4 +1,4 @@
1
1
  module FreshConnection
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
4
4
 
metadata CHANGED
@@ -1,27 +1,27 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fresh_connection
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
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-07 00:00:00.000000000 Z
11
+ date: 2013-12-08 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.2.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.2.0
27
27
  - !ruby/object:Gem::Dependency