Module: Etcher

Defined in:
lib/etcher.rb,
lib/etcher/types.rb,
lib/etcher/finder.rb,
lib/etcher/builder.rb,
lib/etcher/contract.rb,
lib/etcher/registry.rb,
lib/etcher/resolver.rb,
lib/etcher/loaders/hash.rb,
lib/etcher/loaders/json.rb,
lib/etcher/loaders/yaml.rb,
lib/etcher/transformers/root.rb,
lib/etcher/transformers/time.rb,
lib/etcher/loaders/environment.rb,
lib/etcher/transformers/format.rb,
lib/etcher/transformers/basename.rb

Overview

Finds internal constant if moniker matches, otherwise answers a failure.

Defined Under Namespace

Modules: Loaders, Transformers, Types Classes: Builder, Resolver

Constant Summary collapse

LOGGER =
Cogger.new id: :etcher
Finder =
lambda do |namespace, moniker|
  Etcher.const_get(namespace)
        .constants
        .find { |constant| constant.downcase == moniker }
        .then do |constant|
          return Dry::Monads::Success Etcher.const_get("#{namespace}::#{constant}") if constant

          Dry::Monads::Failure "Unable to select #{moniker.inspect} within #{namespace.downcase}."
        end
rescue NameError
  Dry::Monads::Failure "Invalid namespace: #{namespace.inspect}."
end
Contract =

A simple passthrough contract.

lambda do |result|
  def result.to_monad = Dry::Monads::Success self unless result.respond_to? :to_monad
  result
end
Registry =

Provides a registry of customization for loading and resolving a configuration.

Data.define :contract, :model, :loaders, :transformers do
  def self.find namespace, moniker, logger: LOGGER
    case Finder.call namespace, moniker
      in Success(constant) then constant
      in Failure(message) then logger.abort message
      else logger.abort "Unable to find constant in registry."
    end
  end

  def initialize contract: Contract, model: Hash, loaders: [], transformers: []
    super
  end

  def add_loader(loader, ...) = add(loader, :Loaders, ...)

  def remove_loader(index) = remove index, loaders

  def add_transformer(transformer, ...) = add(transformer, :Transformers, ...)

  def remove_transformer(index) = remove index, transformers

  private

  def add(item, namespace, ...)
    collection = __send__ namespace.downcase

    if item.is_a? Symbol
      self.class.find(namespace, item).then { |kind| collection.append kind.new(...) }
    else
      collection.append item
    end

    self
  end

  def remove index, collection
    collection.delete_at index
    self
  end
end

Class Method Summary collapse

Class Method Details

.call(registry = Registry.new) ⇒ Object



23
# File 'lib/etcher.rb', line 23

def self.call(registry = Registry.new, **) = Resolver.new(registry).call(**)

.loader(registry = Zeitwerk::Registry) ⇒ Object



17
18
19
# File 'lib/etcher.rb', line 17

def self.loader registry = Zeitwerk::Registry
  @loader ||= registry.loaders.find { |loader| loader.tag == File.basename(__FILE__, ".rb") }
end

.newObject



21
# File 'lib/etcher.rb', line 21

def self.new(...) = Builder.new(...)