Skip to content

Commit 02110e1

Browse files
Merge pull request #1 from AlexAvlonitis/allow-multiple-db-gateways
Add multiple gateways support
2 parents 26d90a7 + eb966ad commit 02110e1

File tree

10 files changed

+221
-17
lines changed

10 files changed

+221
-17
lines changed

.rspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--require spec_helper

Gemfile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# frozen_string_literal: true
2+
3+
source "https://rubygems.org"
4+
5+
gemspec
6+
7+
group :test do
8+
gem "rspec"
9+
gem "pry"
10+
end

Gemfile.lock

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
PATH
2+
remote: .
3+
specs:
4+
pico_api (0.0.1)
5+
erb (~> 2.2.3)
6+
jsonapi-serializer (~> 2.2.0)
7+
rackup (~> 0.2.2)
8+
rake (~> 13.0.6)
9+
roda (~> 3.61.0)
10+
rom (~> 5.2.6)
11+
rom-sql (~> 3.5.0)
12+
zeitwerk (~> 2.6.1)
13+
14+
GEM
15+
remote: https://rubygems.org/
16+
specs:
17+
activesupport (7.0.4)
18+
concurrent-ruby (~> 1.0, >= 1.0.2)
19+
i18n (>= 1.6, < 2)
20+
minitest (>= 5.1)
21+
tzinfo (~> 2.0)
22+
cgi (0.3.3)
23+
coderay (1.1.3)
24+
concurrent-ruby (1.1.10)
25+
diff-lcs (1.5.0)
26+
dry-container (0.11.0)
27+
concurrent-ruby (~> 1.0)
28+
dry-core (0.9.1)
29+
concurrent-ruby (~> 1.0)
30+
zeitwerk (~> 2.6)
31+
dry-equalizer (0.3.0)
32+
dry-inflector (0.3.0)
33+
dry-initializer (3.1.1)
34+
dry-logic (1.3.0)
35+
concurrent-ruby (~> 1.0)
36+
dry-core (~> 0.9, >= 0.9)
37+
zeitwerk (~> 2.6)
38+
dry-struct (1.5.2)
39+
dry-core (~> 0.9, >= 0.9)
40+
dry-types (~> 1.6)
41+
ice_nine (~> 0.11)
42+
zeitwerk (~> 2.6)
43+
dry-types (1.6.1)
44+
concurrent-ruby (~> 1.0)
45+
dry-container (~> 0.3)
46+
dry-core (~> 0.9, >= 0.9)
47+
dry-inflector (~> 0.1, >= 0.1.2)
48+
dry-logic (~> 1.3, >= 1.3)
49+
zeitwerk (~> 2.6)
50+
erb (2.2.3)
51+
cgi
52+
i18n (1.12.0)
53+
concurrent-ruby (~> 1.0)
54+
ice_nine (0.11.2)
55+
jsonapi-serializer (2.2.0)
56+
activesupport (>= 4.2)
57+
method_source (1.0.0)
58+
minitest (5.16.3)
59+
pry (0.14.1)
60+
coderay (~> 1.1)
61+
method_source (~> 1.0)
62+
rack (3.0.0)
63+
rackup (0.2.2)
64+
rack (>= 3.0.0.beta1)
65+
webrick
66+
rake (13.0.6)
67+
roda (3.61.0)
68+
rack
69+
rom (5.2.6)
70+
rom-changeset (~> 5.2, >= 5.2.3)
71+
rom-core (~> 5.2, >= 5.2.5)
72+
rom-repository (~> 5.2, >= 5.2.2)
73+
rom-changeset (5.2.3)
74+
dry-core (~> 0.4)
75+
rom-core (~> 5.2)
76+
transproc (~> 1.0, >= 1.1.0)
77+
rom-core (5.2.6)
78+
concurrent-ruby (~> 1.1)
79+
dry-container (~> 0.7)
80+
dry-core (~> 0.4)
81+
dry-equalizer (~> 0.2)
82+
dry-inflector (~> 0.1)
83+
dry-initializer (~> 3.0, >= 3.0.1)
84+
dry-struct (~> 1.0)
85+
dry-types (~> 1.0)
86+
transproc (~> 1.0, >= 1.1.0)
87+
rom-repository (5.2.2)
88+
dry-core (~> 0.4)
89+
dry-initializer (~> 3.0, >= 3.0.1)
90+
rom-core (~> 5.2, >= 5.2.2)
91+
rom-sql (3.5.0)
92+
dry-core (~> 0.5, >= 0.5)
93+
dry-types (~> 1.0)
94+
rom (~> 5.2, >= 5.2.1)
95+
sequel (>= 4.49)
96+
rspec (3.12.0)
97+
rspec-core (~> 3.12.0)
98+
rspec-expectations (~> 3.12.0)
99+
rspec-mocks (~> 3.12.0)
100+
rspec-core (3.12.0)
101+
rspec-support (~> 3.12.0)
102+
rspec-expectations (3.12.0)
103+
diff-lcs (>= 1.2.0, < 2.0)
104+
rspec-support (~> 3.12.0)
105+
rspec-mocks (3.12.0)
106+
diff-lcs (>= 1.2.0, < 2.0)
107+
rspec-support (~> 3.12.0)
108+
rspec-support (3.12.0)
109+
sequel (5.62.0)
110+
transproc (1.1.1)
111+
tzinfo (2.0.5)
112+
concurrent-ruby (~> 1.0)
113+
webrick (1.7.0)
114+
zeitwerk (2.6.4)
115+
116+
PLATFORMS
117+
ruby
118+
119+
DEPENDENCIES
120+
pico_api!
121+
pry
122+
rspec (~> 3)
123+
124+
BUNDLED WITH
125+
2.1.4

Readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Migration info: https://rom-rb.org/5.0/learn/sql/migrations/
4545
- [x] Handle Errors
4646
- [ ] Create a Logger config
4747
- [ ] Create a testing environment
48-
- [ ] Allow multiple db gateways in the config
48+
- [x] Allow multiple db gateways in the config
4949
- [ ] Add irb/pry console script
5050

5151
## Contributing

lib/pico_api/database.rb

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,21 @@ class << self
99
attr_accessor :container
1010

1111
def setup!
12-
database_config = PicoApi.configuration.db_config['default']
13-
adapter = database_config['adapter'].to_sym
14-
options = database_config['options'].symbolize_keys
15-
connection_string = database_config['connection_string']
12+
database_config = PicoApi.configuration.db_config.deep_symbolize_keys
13+
gateways = database_config.keys
1614

17-
config = ROM::Configuration.new(adapter, connection_string, options)
18-
yield config if block_given?
15+
configs = gateways.inject({}) do |memo, gateway|
16+
adapter = database_config.dig(gateway, :adapter)&.to_sym
17+
options = database_config.dig(gateway, :options)
18+
connection_string = database_config.dig(gateway, :connection_string)
1919

20-
@container = ROM.container(config)
20+
memo[gateway] = [adapter, connection_string, **options]
21+
memo
22+
end
23+
24+
@container = ROM.container(**configs) do |config|
25+
yield config if block_given?
26+
end
2127
end
2228
end
2329
end

lib/pico_api/generators/templates/config/database.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,9 @@ default:
66
options:
77
encoding:
88

9+
# Additional gateways supported
10+
# legacy:
11+
# adapter:
12+
# connection_string:
13+
# options:
14+
# encoding:

lib/pico_api/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module PicoApi
4-
VERSION = '0.0.1'
4+
VERSION = '0.0.2'
55
end

pico_api.gemspec

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ Gem::Specification.new do |spec|
2222
spec.require_paths = ['lib']
2323
spec.executables << 'pico_api'
2424

25-
spec.add_runtime_dependency 'erb', '~> 2.2.3'
26-
spec.add_runtime_dependency 'jsonapi-serializer', '~> 2.2.0'
27-
spec.add_runtime_dependency 'rackup', '~> 0.2.2'
28-
spec.add_runtime_dependency 'rake', '~> 13.0.6'
29-
spec.add_runtime_dependency 'roda', '~> 3.61.0'
30-
spec.add_runtime_dependency 'rom', '~> 5.2.6'
31-
spec.add_runtime_dependency 'rom-sql', '~> 3.5.0'
32-
spec.add_runtime_dependency 'zeitwerk', '~> 2.6.1'
25+
spec.add_runtime_dependency 'erb', '~> 2'
26+
spec.add_runtime_dependency 'jsonapi-serializer', '~> 2'
27+
spec.add_runtime_dependency 'rackup', '~> 0.2'
28+
spec.add_runtime_dependency 'rake', '~> 13'
29+
spec.add_runtime_dependency 'roda', '~> 3'
30+
spec.add_runtime_dependency 'rom', '~> 5'
31+
spec.add_runtime_dependency 'rom-sql', '~> 3.5'
32+
spec.add_runtime_dependency 'zeitwerk', '~> 2.6'
3333
end

spec/lib/pico_api/database_spec.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.describe PicoApi::Database do
4+
subject { described_class }
5+
6+
let(:configuration) { double(:configuration, db_config: db_config) }
7+
let(:db_config) do
8+
{
9+
default: {
10+
adapter: "memory",
11+
connection_string: "memory://test.db",
12+
options: {
13+
encoding: 'utf8'
14+
}
15+
},
16+
legacy: {
17+
adapter: "memory",
18+
connection_string: "memory://legacy.db",
19+
options: {
20+
encoding: 'unicode'
21+
}
22+
}
23+
}
24+
end
25+
26+
before do
27+
allow(PicoApi).to receive(:configuration) { configuration }
28+
subject.setup!
29+
end
30+
31+
it "builds a correct configuration container" do
32+
expect(subject.container).to be_a(ROM::Container)
33+
end
34+
35+
it "includes the default gateway" do
36+
expect(subject.container.gateways[:default]).to be_present
37+
end
38+
39+
it "includes the legacy gateway" do
40+
expect(subject.container.gateways[:legacy]).to be_present
41+
end
42+
end

spec/spec_helper.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
require_relative "../lib/pico_api"
2+
require "pry"
3+
4+
RSpec.configure do |config|
5+
config.expect_with :rspec do |expectations|
6+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
7+
end
8+
9+
config.mock_with :rspec do |mocks|
10+
mocks.verify_partial_doubles = true
11+
end
12+
13+
config.shared_context_metadata_behavior = :apply_to_host_groups
14+
end

0 commit comments

Comments
 (0)