Skip to content

Commit

Permalink
Merge pull request #1 from AlexAvlonitis/allow-multiple-db-gateways
Browse files Browse the repository at this point in the history
Add multiple gateways support
  • Loading branch information
AlexAvlonitis authored Nov 5, 2022
2 parents 26d90a7 + eb966ad commit 02110e1
Show file tree
Hide file tree
Showing 10 changed files with 221 additions and 17 deletions.
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--require spec_helper
10 changes: 10 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

source "https://rubygems.org"

gemspec

group :test do
gem "rspec"
gem "pry"
end
125 changes: 125 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
PATH
remote: .
specs:
pico_api (0.0.1)
erb (~> 2.2.3)
jsonapi-serializer (~> 2.2.0)
rackup (~> 0.2.2)
rake (~> 13.0.6)
roda (~> 3.61.0)
rom (~> 5.2.6)
rom-sql (~> 3.5.0)
zeitwerk (~> 2.6.1)

GEM
remote: https://rubygems.org/
specs:
activesupport (7.0.4)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 1.6, < 2)
minitest (>= 5.1)
tzinfo (~> 2.0)
cgi (0.3.3)
coderay (1.1.3)
concurrent-ruby (1.1.10)
diff-lcs (1.5.0)
dry-container (0.11.0)
concurrent-ruby (~> 1.0)
dry-core (0.9.1)
concurrent-ruby (~> 1.0)
zeitwerk (~> 2.6)
dry-equalizer (0.3.0)
dry-inflector (0.3.0)
dry-initializer (3.1.1)
dry-logic (1.3.0)
concurrent-ruby (~> 1.0)
dry-core (~> 0.9, >= 0.9)
zeitwerk (~> 2.6)
dry-struct (1.5.2)
dry-core (~> 0.9, >= 0.9)
dry-types (~> 1.6)
ice_nine (~> 0.11)
zeitwerk (~> 2.6)
dry-types (1.6.1)
concurrent-ruby (~> 1.0)
dry-container (~> 0.3)
dry-core (~> 0.9, >= 0.9)
dry-inflector (~> 0.1, >= 0.1.2)
dry-logic (~> 1.3, >= 1.3)
zeitwerk (~> 2.6)
erb (2.2.3)
cgi
i18n (1.12.0)
concurrent-ruby (~> 1.0)
ice_nine (0.11.2)
jsonapi-serializer (2.2.0)
activesupport (>= 4.2)
method_source (1.0.0)
minitest (5.16.3)
pry (0.14.1)
coderay (~> 1.1)
method_source (~> 1.0)
rack (3.0.0)
rackup (0.2.2)
rack (>= 3.0.0.beta1)
webrick
rake (13.0.6)
roda (3.61.0)
rack
rom (5.2.6)
rom-changeset (~> 5.2, >= 5.2.3)
rom-core (~> 5.2, >= 5.2.5)
rom-repository (~> 5.2, >= 5.2.2)
rom-changeset (5.2.3)
dry-core (~> 0.4)
rom-core (~> 5.2)
transproc (~> 1.0, >= 1.1.0)
rom-core (5.2.6)
concurrent-ruby (~> 1.1)
dry-container (~> 0.7)
dry-core (~> 0.4)
dry-equalizer (~> 0.2)
dry-inflector (~> 0.1)
dry-initializer (~> 3.0, >= 3.0.1)
dry-struct (~> 1.0)
dry-types (~> 1.0)
transproc (~> 1.0, >= 1.1.0)
rom-repository (5.2.2)
dry-core (~> 0.4)
dry-initializer (~> 3.0, >= 3.0.1)
rom-core (~> 5.2, >= 5.2.2)
rom-sql (3.5.0)
dry-core (~> 0.5, >= 0.5)
dry-types (~> 1.0)
rom (~> 5.2, >= 5.2.1)
sequel (>= 4.49)
rspec (3.12.0)
rspec-core (~> 3.12.0)
rspec-expectations (~> 3.12.0)
rspec-mocks (~> 3.12.0)
rspec-core (3.12.0)
rspec-support (~> 3.12.0)
rspec-expectations (3.12.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.12.0)
rspec-mocks (3.12.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.12.0)
rspec-support (3.12.0)
sequel (5.62.0)
transproc (1.1.1)
tzinfo (2.0.5)
concurrent-ruby (~> 1.0)
webrick (1.7.0)
zeitwerk (2.6.4)

PLATFORMS
ruby

DEPENDENCIES
pico_api!
pry
rspec (~> 3)

BUNDLED WITH
2.1.4
2 changes: 1 addition & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Migration info: https://rom-rb.org/5.0/learn/sql/migrations/
- [x] Handle Errors
- [ ] Create a Logger config
- [ ] Create a testing environment
- [ ] Allow multiple db gateways in the config
- [x] Allow multiple db gateways in the config
- [ ] Add irb/pry console script

## Contributing
Expand Down
20 changes: 13 additions & 7 deletions lib/pico_api/database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,21 @@ class << self
attr_accessor :container

def setup!
database_config = PicoApi.configuration.db_config['default']
adapter = database_config['adapter'].to_sym
options = database_config['options'].symbolize_keys
connection_string = database_config['connection_string']
database_config = PicoApi.configuration.db_config.deep_symbolize_keys
gateways = database_config.keys

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

@container = ROM.container(config)
memo[gateway] = [adapter, connection_string, **options]
memo
end

@container = ROM.container(**configs) do |config|
yield config if block_given?
end
end
end
end
Expand Down
6 changes: 6 additions & 0 deletions lib/pico_api/generators/templates/config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,9 @@ default:
options:
encoding:

# Additional gateways supported
# legacy:
# adapter:
# connection_string:
# options:
# encoding:
2 changes: 1 addition & 1 deletion lib/pico_api/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module PicoApi
VERSION = '0.0.1'
VERSION = '0.0.2'
end
16 changes: 8 additions & 8 deletions pico_api.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ Gem::Specification.new do |spec|
spec.require_paths = ['lib']
spec.executables << 'pico_api'

spec.add_runtime_dependency 'erb', '~> 2.2.3'
spec.add_runtime_dependency 'jsonapi-serializer', '~> 2.2.0'
spec.add_runtime_dependency 'rackup', '~> 0.2.2'
spec.add_runtime_dependency 'rake', '~> 13.0.6'
spec.add_runtime_dependency 'roda', '~> 3.61.0'
spec.add_runtime_dependency 'rom', '~> 5.2.6'
spec.add_runtime_dependency 'rom-sql', '~> 3.5.0'
spec.add_runtime_dependency 'zeitwerk', '~> 2.6.1'
spec.add_runtime_dependency 'erb', '~> 2'
spec.add_runtime_dependency 'jsonapi-serializer', '~> 2'
spec.add_runtime_dependency 'rackup', '~> 0.2'
spec.add_runtime_dependency 'rake', '~> 13'
spec.add_runtime_dependency 'roda', '~> 3'
spec.add_runtime_dependency 'rom', '~> 5'
spec.add_runtime_dependency 'rom-sql', '~> 3.5'
spec.add_runtime_dependency 'zeitwerk', '~> 2.6'
end
42 changes: 42 additions & 0 deletions spec/lib/pico_api/database_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# frozen_string_literal: true

RSpec.describe PicoApi::Database do
subject { described_class }

let(:configuration) { double(:configuration, db_config: db_config) }
let(:db_config) do
{
default: {
adapter: "memory",
connection_string: "memory://test.db",
options: {
encoding: 'utf8'
}
},
legacy: {
adapter: "memory",
connection_string: "memory://legacy.db",
options: {
encoding: 'unicode'
}
}
}
end

before do
allow(PicoApi).to receive(:configuration) { configuration }
subject.setup!
end

it "builds a correct configuration container" do
expect(subject.container).to be_a(ROM::Container)
end

it "includes the default gateway" do
expect(subject.container.gateways[:default]).to be_present
end

it "includes the legacy gateway" do
expect(subject.container.gateways[:legacy]).to be_present
end
end
14 changes: 14 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require_relative "../lib/pico_api"
require "pry"

RSpec.configure do |config|
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end

config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end

config.shared_context_metadata_behavior = :apply_to_host_groups
end

0 comments on commit 02110e1

Please sign in to comment.