diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..c99d2e7 --- /dev/null +++ b/.rspec @@ -0,0 +1 @@ +--require spec_helper diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..4dbc709 --- /dev/null +++ b/Gemfile @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +source "https://rubygems.org" + +gemspec + +group :test do + gem "rspec" + gem "pry" +end diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..6c8cf12 --- /dev/null +++ b/Gemfile.lock @@ -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 diff --git a/Readme.md b/Readme.md index 0cb63a3..d871b21 100644 --- a/Readme.md +++ b/Readme.md @@ -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 diff --git a/lib/pico_api/database.rb b/lib/pico_api/database.rb index 613074e..34e306a 100644 --- a/lib/pico_api/database.rb +++ b/lib/pico_api/database.rb @@ -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 diff --git a/lib/pico_api/generators/templates/config/database.yml b/lib/pico_api/generators/templates/config/database.yml index afd4dc2..03d7c58 100644 --- a/lib/pico_api/generators/templates/config/database.yml +++ b/lib/pico_api/generators/templates/config/database.yml @@ -6,3 +6,9 @@ default: options: encoding: +# Additional gateways supported +# legacy: +# adapter: +# connection_string: +# options: +# encoding: diff --git a/lib/pico_api/version.rb b/lib/pico_api/version.rb index 91f8cc6..73e5717 100644 --- a/lib/pico_api/version.rb +++ b/lib/pico_api/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module PicoApi - VERSION = '0.0.1' + VERSION = '0.0.2' end diff --git a/pico_api.gemspec b/pico_api.gemspec index 6579cd8..98d7d98 100644 --- a/pico_api.gemspec +++ b/pico_api.gemspec @@ -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 diff --git a/spec/lib/pico_api/database_spec.rb b/spec/lib/pico_api/database_spec.rb new file mode 100644 index 0000000..5fa65f7 --- /dev/null +++ b/spec/lib/pico_api/database_spec.rb @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..8004311 --- /dev/null +++ b/spec/spec_helper.rb @@ -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