Sha256: ff64c61a51a008b02021ba723d6c9e1378dd074e117828cea9e1567fda66ddb0

Contents?: true

Size: 1.27 KB

Versions: 1

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

require "dry-container"
require "logger"
require "http"
require "pastel"

module Pennyworth
  # Provides a global gem container for injection into other objects.
  module Container
    extend Dry::Container::Mixin

    SPEC_PATH = "#{__dir__}/../../pennyworth.gemspec".freeze

    register(:configuration) { Configuration::Loader.call }
    register(:specification) { Gem::Specification.load SPEC_PATH }
    register(:environment) { ENV }
    register(:kernel) { Kernel }
    register(:http) { HTTP }
    register(:colorizer) { Pastel.new enabled: $stdout.tty? }

    register :log_colors do
      {
        "DEBUG" => self[:colorizer].white.detach,
        "INFO" => self[:colorizer].green.detach,
        "WARN" => self[:colorizer].yellow.detach,
        "ERROR" => self[:colorizer].red.detach,
        "FATAL" => self[:colorizer].white.bold.on_red.detach,
        "ANY" => self[:colorizer].white.bold.detach
      }
    end

    register :logger do
      Logger.new $stdout,
                 level: Logger.const_get(ENV.fetch("LOG_LEVEL", "INFO")),
                 formatter: (
                   lambda do |severity, _at, _name, message|
                     self[:log_colors][severity].call "#{message}\n"
                   end
                 )
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pennyworth-12.1.0 lib/pennyworth/container.rb