ar-multidb 0.1.12 → 0.1.13

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: cedb676272cc87f3a21d66c15efd9058e0e13551
4
- data.tar.gz: b1dae22e38cfbe06ab71ccaf7abbe6dbfb459c9f
3
+ metadata.gz: ebc5c80b4af196924f76bb368ffec424b94d6090
4
+ data.tar.gz: 50c67359cdce94f25a6c64f251423281f79e53b7
5
5
  SHA512:
6
- metadata.gz: 5662d638ad37ebd926c8a7c369ed60de18f8debbc121cad19c760a9f7301e73b68d1914f0d8920aa9b2e2dcb7a2af01e79b89a1bf65041b5a636b6e239417502
7
- data.tar.gz: cb9a4f11337c379216d295d579e1938266377c887e7b4608a07fbcb1dcc78d298502459f8ad3cfb97e0acda5209261e9a869560e926654d213747e3946e0b65b
6
+ metadata.gz: fe6db0140c93565223db1739c68916ab4334da42ed8b451f75d024456f5aea5194a5885a714b9f29625002e348ed7d60e53f5dba3caaf1e41317194a3a4573aa
7
+ data.tar.gz: b304471c77b2cc4ef243f21db533fa09a921ad57c4cc183a97ae108fdd54724ab942bd24d1bbd42ca35c77c0fdcbd9d704c635647ff1260c50d1ebae7b40a817
data/.gitignore CHANGED
@@ -1,4 +1,5 @@
1
1
  /pkg
2
2
  *~
3
3
  \.DS_Store
4
- *.sqlite
4
+ *.sqlite
5
+ Gemfile.lock
@@ -102,6 +102,7 @@ module Multidb
102
102
 
103
103
  class << self
104
104
  delegate :use, :current_connection, :disconnect!, to: :balancer
105
+
105
106
  def use(name, &block)
106
107
  Multidb.balancer.use(name, &block)
107
108
  end
@@ -1,19 +1,29 @@
1
1
  module Multidb
2
-
3
- mattr_reader :configuration
4
-
5
2
  class << self
6
3
  delegate :use, :get, :disconnect!, to: :balancer
7
4
  end
8
5
 
6
+ def self.init(config)
7
+ activerecord_config = config.dup.with_indifferent_access
8
+ default_adapter, configuration_hash = activerecord_config, activerecord_config.delete(:multidb)
9
+
10
+ @balancer = Balancer.new(Configuration.new(default_adapter, configuration_hash || {}))
11
+ end
12
+
9
13
  def self.balancer
10
- @balancer ||= create_balancer
14
+ if @balancer
15
+ @balancer
16
+ else
17
+ raise NotInitializedError, "Balancer not initialized. You need to run Multidb.setup first"
18
+ end
11
19
  end
12
20
 
13
21
  def self.reset!
14
- @balancer, @configuration = nil, nil
22
+ @balancer = nil
15
23
  end
16
24
 
25
+ class NotInitializedError < StandardError; end;
26
+
17
27
  class Configuration
18
28
  def initialize(default_adapter, configuration_hash)
19
29
  @default_pool = ActiveRecord::Base.connection_pool
@@ -25,30 +35,4 @@ module Multidb
25
35
  attr_reader :default_adapter
26
36
  attr_reader :raw_configuration
27
37
  end
28
-
29
- private
30
-
31
- def self.create_balancer
32
- unless @configuration
33
- begin
34
- connection_pool = ActiveRecord::Base.connection_pool
35
- rescue ActiveRecord::ConnectionNotEstablished
36
- # Ignore
37
- else
38
- connection = connection_pool.connection
39
-
40
- # FIXME: This is hacky, but apparently the only way to get at
41
- # the internal configuration hash.
42
- activerecord_config = connection.instance_variable_get(:@config).dup.with_indifferent_access
43
-
44
- default_adapter, configuration_hash = activerecord_config, activerecord_config.delete(:multidb)
45
-
46
- @configuration = Configuration.new(default_adapter, configuration_hash || {})
47
- end
48
- end
49
- if @configuration
50
- Balancer.new(@configuration)
51
- end
52
- end
53
-
54
38
  end
@@ -1,24 +1,28 @@
1
+ require 'active_record/base'
2
+
1
3
  module Multidb
2
4
  module ModelExtensions
3
-
4
5
  extend ActiveSupport::Concern
5
6
 
6
7
  included do
7
8
  class << self
9
+ alias_method_chain :establish_connection, :multidb
8
10
  alias_method_chain :connection, :multidb
9
11
  end
10
12
  end
11
13
 
12
14
  module ClassMethods
15
+ def establish_connection_with_multidb(spec = nil)
16
+ establish_connection_without_multidb(spec)
17
+ Multidb.init(connection_pool.spec.config)
18
+ end
19
+
13
20
  def connection_with_multidb
14
- if (balancer = Multidb.balancer)
15
- balancer.current_connection
16
- else
17
- connection_without_multidb
18
- end
21
+ Multidb.balancer.current_connection
22
+ rescue Multidb::NotInitializedError
23
+ connection_without_multidb
19
24
  end
20
25
  end
21
-
22
26
  end
23
27
  end
24
28
 
@@ -1,3 +1,3 @@
1
1
  module Multidb
2
- VERSION = '0.1.12'
2
+ VERSION = '0.1.13'
3
3
  end
@@ -3,8 +3,8 @@ require_relative 'spec_helper'
3
3
  describe 'Multidb.balancer' do
4
4
 
5
5
  context 'with no configuration' do
6
- it 'returns nothing' do
7
- Multidb.balancer.should eq nil
6
+ it 'raises exception' do
7
+ -> { Multidb.balancer }.should raise_error(Multidb::NotInitializedError)
8
8
  end
9
9
  end
10
10
 
@@ -29,20 +29,20 @@ describe 'Multidb.balancer' do
29
29
  end
30
30
 
31
31
  describe '#use' do
32
- context 'with no configuration' do
33
- it 'raises exception' do
34
- -> {
35
- Multidb.use(:something) do
36
- end
37
- }.should raise_error(ArgumentError)
38
- end
39
- end
40
-
41
32
  context 'with configuration' do
42
33
  before do
43
34
  ActiveRecord::Base.establish_connection(configuration_with_slaves)
44
35
  end
45
36
 
37
+ context 'undefined connection' do
38
+ it 'raises exception' do
39
+ -> {
40
+ Multidb.use(:something) do
41
+ end
42
+ }.should raise_error(ArgumentError)
43
+ end
44
+ end
45
+
46
46
  it 'returns default connection on :default' do
47
47
  conn = ActiveRecord::Base.connection
48
48
  Multidb.use(:default) do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ar-multidb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.12
4
+ version: 0.1.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Staubo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-31 00:00:00.000000000 Z
11
+ date: 2014-05-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -77,7 +77,6 @@ files:
77
77
  - .gitignore
78
78
  - .travis.yml
79
79
  - Gemfile
80
- - Gemfile.lock
81
80
  - LICENSE
82
81
  - README.markdown
83
82
  - Rakefile
@@ -1,54 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- ar-multidb (0.1.11)
5
- activerecord (>= 3.0)
6
- activesupport (>= 3.0)
7
-
8
- GEM
9
- remote: http://rubygems.org/
10
- specs:
11
- activemodel (4.0.2)
12
- activesupport (= 4.0.2)
13
- builder (~> 3.1.0)
14
- activerecord (4.0.2)
15
- activemodel (= 4.0.2)
16
- activerecord-deprecated_finders (~> 1.0.2)
17
- activesupport (= 4.0.2)
18
- arel (~> 4.0.0)
19
- activerecord-deprecated_finders (1.0.3)
20
- activesupport (4.0.2)
21
- i18n (~> 0.6, >= 0.6.4)
22
- minitest (~> 4.2)
23
- multi_json (~> 1.3)
24
- thread_safe (~> 0.1)
25
- tzinfo (~> 0.3.37)
26
- arel (4.0.1)
27
- atomic (1.1.14)
28
- builder (3.1.4)
29
- diff-lcs (1.2.5)
30
- i18n (0.6.9)
31
- minitest (4.7.5)
32
- multi_json (1.8.4)
33
- rake (10.1.1)
34
- rspec (2.14.1)
35
- rspec-core (~> 2.14.0)
36
- rspec-expectations (~> 2.14.0)
37
- rspec-mocks (~> 2.14.0)
38
- rspec-core (2.14.7)
39
- rspec-expectations (2.14.5)
40
- diff-lcs (>= 1.1.3, < 2.0)
41
- rspec-mocks (2.14.5)
42
- sqlite3 (1.3.8)
43
- thread_safe (0.1.3)
44
- atomic
45
- tzinfo (0.3.38)
46
-
47
- PLATFORMS
48
- ruby
49
-
50
- DEPENDENCIES
51
- ar-multidb!
52
- rake
53
- rspec
54
- sqlite3