octoshark 0.0.2 → 0.0.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: edbe9d237528071174741f9625af2b2f752ebb3f
4
- data.tar.gz: a0f0081430d9987ceaa43f375526cab9acec3817
3
+ metadata.gz: 161e067d71a05ad670c0878c76c81cf2f12e46f6
4
+ data.tar.gz: 2ea1161f0943be6a2898098ce33a45d71c198d79
5
5
  SHA512:
6
- metadata.gz: 01b1279e3d8036eed43a2ef8f780fe5ec13281d424f1202ea2a3d3857cea5a9a826a508a10d35603082fef1b9304883d7a12a801d41e2663bce639cd721ffe37
7
- data.tar.gz: 5050e8c20e16acae21138f85cea4cc033500f27f6fba0c4c1c57d85ff20c08a10a233bbcd6517c3592752ed1895250e9c7113ff35ad84e07ae1bfb1feb188dd2
6
+ metadata.gz: e564f3d12510245908fa909d14712e1a3c7be6463b9bdd48c5040951762f08aeaa4052a7e17a434671396dae16a19c6a3c64c7164db2463884551b316895a5c2
7
+ data.tar.gz: 770a6674f4dfb97311027811c613450dabc42c95894f314477b174f121793357f529efb81257aa7972b94696963d45ad1760588c7fa62323bb771b63f63c67b2
data/.gitignore CHANGED
@@ -15,3 +15,4 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+ gemfiles/*.gemfile.lock
data/.travis.yml CHANGED
@@ -5,4 +5,7 @@ cache:
5
5
  notifications:
6
6
  email:
7
7
  - dalibor.nasevic@gmail.com
8
+ gemfile:
9
+ - gemfiles/rails3.gemfile
10
+ - gemfiles/rails4.gemfile
8
11
  script: bundle exec rspec spec
data/Appraisals ADDED
@@ -0,0 +1,7 @@
1
+ appraise "rails3" do
2
+ gem "activerecord", "~> 3.0.0"
3
+ end
4
+
5
+ appraise "rails4" do
6
+ gem "activerecord", "~> 4.0.0"
7
+ end
data/README.md CHANGED
@@ -31,7 +31,7 @@ $ gem install octoshark
31
31
  Specify the connections for Octoshark to manage. This is usually done in an app initializer.
32
32
 
33
33
  ```ruby
34
- Octoshark.setup({
34
+ Octoshark.configure({
35
35
  db1: { adapter: "sqlite3", database: "db/db1.sqlite" },
36
36
  db2: { adapter: "sqlite3", database: "db/db2.sqlite" }
37
37
  })
@@ -100,6 +100,12 @@ When we want to do something in the slave database with all ActiveRecord models,
100
100
  ```ruby
101
101
  class ActiveRecord::Base
102
102
  def self.connection
103
+ # Some rake tasks like `rake db:create` does not load initializers,
104
+ # and because we're overriding ActiveRecord::Base.connection,
105
+ # we need to make sure Octoshark is configured before using it.
106
+ Octoshark.configure(configs) unless Octoshark.configured?
107
+
108
+ # Return the current connection (from with_connection block) or default one
103
109
  Octoshark.current_or_default_connection
104
110
  end
105
111
  end
data/Rakefile CHANGED
@@ -1 +1,6 @@
1
1
  require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", "~> 3.0.0"
6
+
7
+ gemspec :path => "../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", "~> 4.0.0"
6
+
7
+ gemspec :path => "../"
data/lib/octoshark.rb CHANGED
@@ -17,13 +17,13 @@ module Octoshark
17
17
  :connection_pools, :find_connection_pool,
18
18
  :disconnect!, to: :switcher
19
19
 
20
- def setup(configs)
20
+ def configure(configs)
21
21
  @configs = configs
22
22
  @switcher = ConnectionSwitcher.new(configs)
23
23
  end
24
24
 
25
25
  def reset!
26
- return unless enabled?
26
+ return unless configured?
27
27
  disconnect!
28
28
  @confings = nil
29
29
  @switcher = nil
@@ -36,7 +36,7 @@ module Octoshark
36
36
  @switcher = ConnectionSwitcher.new(@configs)
37
37
  end
38
38
 
39
- def enabled?
39
+ def configured?
40
40
  !@switcher.nil?
41
41
  end
42
42
 
@@ -46,7 +46,7 @@ module Octoshark
46
46
 
47
47
  private
48
48
  def raise_not_configured_error
49
- raise NotConfiguredError, "Octoshark is not setup"
49
+ raise NotConfiguredError, "Octoshark is not configured, use Octoshark.configure"
50
50
  end
51
51
  end
52
52
  end
@@ -11,7 +11,7 @@ module Octoshark
11
11
  module ClassMethods
12
12
  def establish_connection_with_octoshark(*args)
13
13
  establish_connection_without_octoshark(*args)
14
- Octoshark.reload! if Octoshark.enabled?
14
+ Octoshark.reload! if Octoshark.configured?
15
15
  end
16
16
  end
17
17
  end
@@ -8,15 +8,13 @@ module Octoshark
8
8
  @connection_pools = { default: @default_pool }.with_indifferent_access
9
9
 
10
10
  configs.each_pair do |name, config|
11
- spec = ActiveRecord::ConnectionAdapters::ConnectionSpecification.new(
12
- config, "#{config[:adapter]}_connection"
13
- )
11
+ spec = spec_class.new(config, "#{config[:adapter]}_connection")
14
12
  @connection_pools[name] = ActiveRecord::ConnectionAdapters::ConnectionPool.new(spec)
15
13
  end
16
14
  end
17
15
 
18
16
  def current_connection
19
- Thread.current[OCTOSHARK] || raise(NoCurrentConnectionError, "No current connection")
17
+ Thread.current[OCTOSHARK] || raise(NoCurrentConnectionError, "No current connection, use Octoshark.with_connection")
20
18
  end
21
19
 
22
20
  def current_or_default_connection
@@ -39,7 +37,7 @@ module Octoshark
39
37
  result
40
38
  end
41
39
 
42
- def find_connection_pool(name, &block)
40
+ def find_connection_pool(name)
43
41
  @connection_pools[name] || raise(NoConnectionError, "No such database connection '#{name}'")
44
42
  end
45
43
 
@@ -48,5 +46,14 @@ module Octoshark
48
46
  connection_pool.disconnect!
49
47
  end
50
48
  end
49
+
50
+ private
51
+ def spec_class
52
+ if defined?(ActiveRecord::ConnectionAdapters::ConnectionSpecification)
53
+ spec_class = ActiveRecord::ConnectionAdapters::ConnectionSpecification
54
+ else
55
+ spec_class = ActiveRecord::Base::ConnectionSpecification
56
+ end
57
+ end
51
58
  end
52
59
  end
@@ -1,3 +1,3 @@
1
1
  module Octoshark
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/octoshark.gemspec CHANGED
@@ -24,4 +24,5 @@ Gem::Specification.new do |spec|
24
24
  spec.add_development_dependency "rake"
25
25
  spec.add_development_dependency "rspec", "~> 3.0.0"
26
26
  spec.add_development_dependency "sqlite3", "~> 1.3.0"
27
+ spec.add_development_dependency "appraisal"
27
28
  end
@@ -2,16 +2,12 @@ require 'spec_helper'
2
2
 
3
3
  describe Octoshark::ActiveRecordExtensions do
4
4
 
5
- it "reloads Octoshark when re-establishing new connection" do
6
- Octoshark.setup(configs)
7
- spec = ActiveRecord::Base.remove_connection
8
-
9
- expect { Octoshark.with_connection(:default) {} }.to raise_error(ActiveRecord::ConnectionNotEstablished)
10
- expect { Octoshark.with_connection(:db1) {} }.not_to raise_error
5
+ it "reloads Octoshark connection pools when establishing a new connection" do
6
+ Octoshark.configure(configs)
11
7
 
8
+ spec = ActiveRecord::Base.remove_connection
12
9
  ActiveRecord::Base.establish_connection(spec)
13
10
 
14
- expect { Octoshark.with_connection(:default) {} }.not_to raise_error
15
- expect { Octoshark.with_connection(:db1) {} }.not_to raise_error
11
+ expect(ActiveRecord::Base.connection_pool).to eq(Octoshark.find_connection_pool(:default))
16
12
  end
17
13
  end
@@ -2,9 +2,9 @@ require 'spec_helper'
2
2
 
3
3
  describe Octoshark do
4
4
 
5
- describe ".setup" do
5
+ describe ".configure" do
6
6
  it "creates connection switcher" do
7
- Octoshark.setup({})
7
+ Octoshark.configure({})
8
8
 
9
9
  expect(Octoshark.switcher).to_not be_nil
10
10
  end
@@ -12,14 +12,14 @@ describe Octoshark do
12
12
 
13
13
  describe ".reset!" do
14
14
  it "removes connection switcher" do
15
- Octoshark.setup({})
15
+ Octoshark.configure({})
16
16
  Octoshark.reset!
17
17
 
18
18
  expect { Octoshark.switcher }.to raise_error(Octoshark::NotConfiguredError)
19
19
  end
20
20
 
21
21
  it "cleans octoshark thread key" do
22
- Octoshark.setup({})
22
+ Octoshark.configure({})
23
23
  Octoshark.reset!
24
24
 
25
25
  expect(Thread.current[Octoshark::OCTOSHARK]).to be_nil
@@ -32,7 +32,7 @@ describe Octoshark do
32
32
 
33
33
  describe ".reload!" do
34
34
  it "replaces connection switcher" do
35
- Octoshark.setup({})
35
+ Octoshark.configure({})
36
36
  switcher = Octoshark.switcher
37
37
 
38
38
  Octoshark.reload!
@@ -46,26 +46,26 @@ describe Octoshark do
46
46
  end
47
47
  end
48
48
 
49
- describe ".enabled?" do
50
- it "is not enabled by default" do
51
- expect(Octoshark.enabled?).to be_falsey
49
+ describe ".configured?" do
50
+ it "is not configured by default" do
51
+ expect(Octoshark.configured?).to be_falsey
52
52
  end
53
53
 
54
- it "is enabled when it's setup" do
55
- Octoshark.setup({})
54
+ it "is configured is switcher is configured" do
55
+ Octoshark.configure({})
56
56
 
57
- expect(Octoshark.enabled?).to be_truthy
57
+ expect(Octoshark.configured?).to be_truthy
58
58
  end
59
59
  end
60
60
 
61
61
  describe ".switcher" do
62
62
  it "returns connection switcher" do
63
- Octoshark.setup({})
63
+ Octoshark.configure({})
64
64
 
65
65
  expect(Octoshark.switcher).to be_an_instance_of(Octoshark::ConnectionSwitcher)
66
66
  end
67
67
 
68
- it "raises 'NotConfiguredError' error if not setup" do
68
+ it "raises 'NotConfiguredError' error if not configured" do
69
69
  expect { Octoshark.switcher }.to raise_error(Octoshark::NotConfiguredError)
70
70
  end
71
71
  end
@@ -77,7 +77,7 @@ describe Octoshark do
77
77
  ].each do |method_name|
78
78
  describe ".#{method_name}" do
79
79
  it "delegates #{method_name} to connection switcher" do
80
- Octoshark.setup({})
80
+ Octoshark.configure({})
81
81
  expect(Octoshark.switcher).to receive(method_name)
82
82
 
83
83
  Octoshark.send(method_name)
data/spec/spec_helper.rb CHANGED
@@ -1,16 +1,26 @@
1
1
  require 'octoshark'
2
+ require 'fileutils'
2
3
 
4
+ ROOT = File.expand_path('../', File.dirname(__FILE__))
5
+ TMP = 'tmp'
3
6
 
4
7
  # Load support files
5
- ROOT = File.expand_path('../', File.dirname(__FILE__))
6
8
  Dir["#{ROOT}/spec/support/**/*.rb"].each { |f| require f }
7
9
 
8
10
 
9
11
  RSpec.configure do |config|
10
12
  config.include Helpers
11
13
 
14
+ config.before :suite do
15
+ FileUtils.mkdir_p(TMP)
16
+ end
17
+
12
18
  config.before :each do
13
19
  ActiveRecord::Base.establish_connection({adapter: 'sqlite3', database: 'tmp/default.sqlite'})
14
20
  Octoshark.reset!
15
21
  end
22
+
23
+ config.after :suite do
24
+ FileUtils.rm_rf(TMP)
25
+ end
16
26
  end
@@ -15,7 +15,7 @@ module Helpers
15
15
  end
16
16
 
17
17
  def check_connections_clean_up
18
- Octoshark.setup({})
18
+ Octoshark.configure({})
19
19
  switcher = Octoshark.switcher
20
20
 
21
21
  Octoshark.with_connection(:default) { |connection| connection.execute("SELECT 1") }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: octoshark
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dalibor Nasevic
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-20 00:00:00.000000000 Z
11
+ date: 2014-07-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: 1.3.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: appraisal
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
83
97
  description: Octoshark is a library for switching between multiple ActiveRecord connections
84
98
  email:
85
99
  - dalibor.nasevic@gmail.com
@@ -90,10 +104,13 @@ files:
90
104
  - ".gitignore"
91
105
  - ".rspec"
92
106
  - ".travis.yml"
107
+ - Appraisals
93
108
  - Gemfile
94
109
  - LICENSE.txt
95
110
  - README.md
96
111
  - Rakefile
112
+ - gemfiles/rails3.gemfile
113
+ - gemfiles/rails4.gemfile
97
114
  - lib/octoshark.rb
98
115
  - lib/octoshark/active_record_extensions.rb
99
116
  - lib/octoshark/connection_switcher.rb