Skip to content

Commit

Permalink
Merge pull request #2 from AlexAvlonitis/add-logger
Browse files Browse the repository at this point in the history
Add Logger support
  • Loading branch information
AlexAvlonitis authored Nov 6, 2022
2 parents 4288b4b + 18df40b commit e17fe94
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 108 deletions.
2 changes: 0 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

source "https://rubygems.org"

gemspec

group :test do
gem "rspec"
gem "pry"
Expand Down
95 changes: 1 addition & 94 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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
2 changes: 1 addition & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Add routing info in `config/application.rb` file
- [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
Expand Down
2 changes: 1 addition & 1 deletion lib/pico_api/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 13 additions & 5 deletions lib/pico_api/database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,28 @@ 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)

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: 4 additions & 2 deletions lib/pico_api/generators/templates/config/configuration.erb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
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.2'
VERSION = '0.0.3'
end
10 changes: 8 additions & 2 deletions spec/lib/pico_api/database_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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

0 comments on commit e17fe94

Please sign in to comment.