Skip to content

Commit e4a0f5b

Browse files
committed
cleanup code and add more test coverage
- move Multidb class methods to the multidb.rb instead of multidb/configuration.rb - cleanup specs (use expect syntax vs should) - add more test coverage - reset the threadlocal when resetting multidb - caused issues in random testing order otherwise - add and configure rubocop-rspec
1 parent 1f47457 commit e4a0f5b

16 files changed

Lines changed: 859 additions & 189 deletions

.rubocop.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
require:
2+
- rubocop-rspec
3+
14
AllCops:
25
TargetRubyVersion: 2.5
36
SuggestExtensions: false
@@ -24,6 +27,9 @@ Layout/SpaceBeforeBrackets: # new in 1.7
2427

2528
Lint/AmbiguousAssignment: # new in 1.7
2629
Enabled: true
30+
Lint/AmbiguousBlockAssociation:
31+
IgnoredMethods:
32+
- change
2733
Lint/AmbiguousOperatorPrecedence: # new in 1.21
2834
Enabled: true
2935
Lint/AmbiguousRange: # new in 1.19
@@ -89,6 +95,22 @@ Naming/BlockForwarding: # new in 1.24
8995
Metrics:
9096
Enabled: false
9197

98+
RSpec/ExampleLength:
99+
CountAsOne: [array, heredoc]
100+
Max: 9
101+
RSpec/ExpectChange:
102+
EnforcedStyle: block
103+
RSpec/ImplicitSubject:
104+
Enabled: false
105+
RSpec/NamedSubject:
106+
Enabled: false
107+
RSpec/MultipleMemoizedHelpers:
108+
Enabled: false
109+
RSpec/MultipleExpectations:
110+
Enabled: false
111+
RSpec/NestedGroups:
112+
Max: 4
113+
92114
Security/CompoundHash: # new in 1.28
93115
Enabled: true
94116
Security/IoMethods: # new in 1.22
@@ -102,6 +124,8 @@ Style/ArrayCoercion:
102124
Enabled: true
103125
Style/BisectedAttrAccessor:
104126
Enabled: true
127+
Style/BlockDelimiters:
128+
Enabled: false
105129
Style/CaseLikeIf:
106130
Enabled: true
107131
Style/CollectionCompact: # new in 1.2

.simplecov

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
SimpleCov.configure do
44
enable_coverage :branch
55
add_filter '/spec/'
6+
add_filter 'lib/multidb/version.rb'
7+
add_filter 'lib/ar-multidb.rb'
68

79
add_group 'Binaries', '/bin/'
810
add_group 'Libraries', '/lib/'

ar-multidb.gemspec

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Gem::Specification.new do |s|
77
s.name = 'ar-multidb'
88
s.version = Multidb::VERSION
99
s.authors = ['Alexander Staubo', 'Edward Rudd']
10-
s.email = ['alex@bengler.no', 'urkle@outoforder.cc']
10+
s.email = %w[alex@bengler.no urkle@outoforder.cc]
1111
s.homepage = ''
1212
s.summary = s.description = 'Multidb is an ActiveRecord extension for switching between multiple database connections, such as primary/replica setups.'
1313
s.license = 'MIT'
@@ -25,7 +25,8 @@ Gem::Specification.new do |s|
2525

2626
s.add_development_dependency 'rake', '~> 13.0'
2727
s.add_development_dependency 'rspec', '~> 3.8'
28-
s.add_development_dependency 'rubocop', '~> 1.28'
28+
s.add_development_dependency 'rubocop', '~> 1.28.0'
29+
s.add_development_dependency 'rubocop-rspec', '~> 2.10.0'
2930
s.add_development_dependency 'simplecov', '~> 0.21.2'
3031
s.add_development_dependency 'simplecov-lcov', '~> 0.8.0'
3132
s.add_development_dependency 'sqlite3', '~> 1.3'

lib/multidb.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,29 @@
1212
require_relative 'multidb/candidate'
1313
require_relative 'multidb/balancer'
1414
require_relative 'multidb/version'
15+
16+
module Multidb
17+
# Error raised when the configuration has not been initialized
18+
class NotInitializedError < StandardError; end
19+
20+
class << self
21+
delegate :use, :get, :disconnect!, to: :balancer
22+
end
23+
24+
def self.init(config)
25+
activerecord_config = config.dup.with_indifferent_access
26+
default_adapter = activerecord_config
27+
configuration_hash = activerecord_config.delete(:multidb)
28+
29+
@balancer = Balancer.new(Configuration.new(default_adapter, configuration_hash || {}))
30+
end
31+
32+
def self.balancer
33+
@balancer || raise(NotInitializedError, 'Balancer not initialized. You need to run Multidb.init first')
34+
end
35+
36+
def self.reset!
37+
@balancer = nil
38+
Thread.current[:multidb] = nil
39+
end
40+
end

lib/multidb/balancer.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def initialize(configuration)
2626
end
2727

2828
def append(databases)
29-
databases.each_pair do |name, config|
29+
databases.with_indifferent_access.each_pair do |name, config|
3030
configs = config.is_a?(Array) ? config : [config]
3131
configs.each do |cfg|
3232
if cfg['alias']
@@ -51,7 +51,7 @@ def get(name, &_block)
5151

5252
raise ArgumentError, "No such database connection '#{name}'" if candidates.empty?
5353

54-
candidate = candidates.respond_to?(:sample) ? candidates.sample : candidates[rand(candidates.length)]
54+
candidate = candidates.sample
5555

5656
block_given? ? yield(candidate) : candidate
5757
end

lib/multidb/configuration.rb

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,6 @@
11
# frozen_string_literal: true
22

33
module Multidb
4-
class << self
5-
delegate :use, :get, :disconnect!, to: :balancer
6-
end
7-
8-
def self.init(config)
9-
activerecord_config = config.dup.with_indifferent_access
10-
default_adapter = activerecord_config
11-
configuration_hash = activerecord_config.delete(:multidb)
12-
13-
@balancer = Balancer.new(Configuration.new(default_adapter, configuration_hash || {}))
14-
end
15-
16-
def self.balancer
17-
@balancer || raise(NotInitializedError, 'Balancer not initialized. You need to run Multidb.init first')
18-
end
19-
20-
def self.reset!
21-
@balancer = nil
22-
end
23-
24-
class NotInitializedError < StandardError; end
25-
264
class Configuration
275
def initialize(default_adapter, configuration_hash)
286
@default_handler = ActiveRecord::Base.connection_handler

spec/balancer_spec.rb

Lines changed: 0 additions & 157 deletions
This file was deleted.

0 commit comments

Comments
 (0)