From 18df40bf7abf8248a97eb25d7b03e151962622ec Mon Sep 17 00:00:00 2001 From: Alex Date: Sun, 6 Nov 2022 12:35:21 +0000 Subject: [PATCH] Add Logger support --- Gemfile | 2 - Gemfile.lock | 95 +------------------ Readme.md | 2 +- lib/pico_api/configuration.rb | 2 +- lib/pico_api/database.rb | 18 +++- .../templates/config/configuration.erb | 6 +- lib/pico_api/version.rb | 2 +- spec/lib/pico_api/database_spec.rb | 10 +- 8 files changed, 29 insertions(+), 108 deletions(-) diff --git a/Gemfile b/Gemfile index 4dbc709..b745cbe 100644 --- a/Gemfile +++ b/Gemfile @@ -2,8 +2,6 @@ source "https://rubygems.org" -gemspec - group :test do gem "rspec" gem "pry" diff --git a/Gemfile.lock b/Gemfile.lock index 6c8cf12..6c48010 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,98 +1,12 @@ -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) @@ -106,20 +20,13 @@ GEM 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) + rspec BUNDLED WITH 2.1.4 diff --git a/Readme.md b/Readme.md index d871b21..e944f86 100644 --- a/Readme.md +++ b/Readme.md @@ -43,7 +43,7 @@ Migration info: https://rom-rb.org/5.0/learn/sql/migrations/ - [x] Create Database config - [x] Hook a json-api library - [x] Handle Errors -- [ ] Create a Logger config +- [x] Create a Logger config - [ ] Create a testing environment - [x] Allow multiple db gateways in the config - [ ] Add irb/pry console script diff --git a/lib/pico_api/configuration.rb b/lib/pico_api/configuration.rb index c04f776..75ab9a2 100644 --- a/lib/pico_api/configuration.rb +++ b/lib/pico_api/configuration.rb @@ -21,7 +21,7 @@ def configure class Configuration attr_reader :db_config - attr_accessor :namespace, :lib_path, :errors_map + attr_accessor :namespace, :lib_path, :errors_map, :logger def initialize(db_config) @db_config = db_config diff --git a/lib/pico_api/database.rb b/lib/pico_api/database.rb index 34e306a..62859d7 100644 --- a/lib/pico_api/database.rb +++ b/lib/pico_api/database.rb @@ -10,9 +10,21 @@ class << self def setup! database_config = PicoApi.configuration.db_config.deep_symbolize_keys + logger = PicoApi.configuration.logger gateways = database_config.keys - configs = gateways.inject({}) do |memo, gateway| + configs = create_config(gateways, database_config) + cont = ROM.container(**configs) { |config| yield config if block_given? } + + gateways.each { |gateway| cont.gateways[gateway].use_logger(logger) } if logger + + @container = cont + end + + private + + def create_config(gateways, database_config) + 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) @@ -20,10 +32,6 @@ def setup! 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/configuration.erb b/lib/pico_api/generators/templates/config/configuration.erb index d576d68..b4c15e9 100644 --- a/lib/pico_api/generators/templates/config/configuration.erb +++ b/lib/pico_api/generators/templates/config/configuration.erb @@ -1,9 +1,11 @@ PicoApi.configure do |config| ## the base namespace of your app - config.namespace = "<%= @project_name_camelcased %>" + config.namespace = "<%= @project_name_camelcased %>" ## the root directory where all the business logic resides - config.lib_path = "lib/<%= @project_name_snakecased %>" + config.lib_path = "lib/<%= @project_name_snakecased %>" + + config.logger = Logger.new(STDOUT) if ENV['LOG_TO_STDOUT'] == 'true' ## Add your custom exception mappings -> { exception: status_code }. # PicoApi serializes runtime exceptions to json-api specification error objects diff --git a/lib/pico_api/version.rb b/lib/pico_api/version.rb index 73e5717..bf0fe5f 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.2' + VERSION = '0.0.3' end diff --git a/spec/lib/pico_api/database_spec.rb b/spec/lib/pico_api/database_spec.rb index 5fa65f7..6ce66ab 100644 --- a/spec/lib/pico_api/database_spec.rb +++ b/spec/lib/pico_api/database_spec.rb @@ -3,7 +3,8 @@ RSpec.describe PicoApi::Database do subject { described_class } - let(:configuration) { double(:configuration, db_config: db_config) } + let(:configuration) { double(:configuration, db_config: db_config, logger: logger) } + let(:logger) { instance_double(Logger) } let(:db_config) do { default: { @@ -36,7 +37,12 @@ expect(subject.container.gateways[:default]).to be_present end - it "includes the legacy gateway" do + it "includes the legacy gateway" do expect(subject.container.gateways[:legacy]).to be_present end + + it "includes a logger in all the gateways" do + expect(subject.container.gateways[:default].logger).to eq(logger) + expect(subject.container.gateways[:legacy].logger).to eq(logger) + end end