Sha256: d245ac0e02f0b40df599c9630f209d35be6b08aab2776a078d5fbbc057b287eb

Contents?: true

Size: 1.12 KB

Versions: 9

Compression:

Stored size: 1.12 KB

Contents

# frozen_string_literal: true

require "pathname"
require "refinements/hashes"
require "refinements/structs"
require "runcom"
require "yaml"

module Rubysmith
  module Configuration
    # Represents the fully assembled Command Line Interface (CLI) configuration.
    class Loader
      using Refinements::Hashes
      using Refinements::Structs

      DEFAULTS = YAML.load_file(Pathname(__dir__).join("defaults.yml")).freeze
      CLIENT = Runcom::Config.new "rubysmith/configuration.yml", defaults: DEFAULTS

      def self.call(...) = new(...).call

      def self.with_defaults = new(client: DEFAULTS, enhancers: {})

      def initialize content: Content.new, client: CLIENT, enhancers: Enhancers::Container
        @content = content
        @client = client
        @enhancers = enhancers
      end

      def call
        enhancers.each
                 .reduce(preload_content) { |preload, (_key, enhancer)| enhancer.call preload }
                 .freeze
      end

      protected

      attr_reader :content, :client, :enhancers

      private

      def preload_content = content.merge(**client.to_h.flatten_keys)
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
rubysmith-4.9.0 lib/rubysmith/configuration/loader.rb
rubysmith-4.8.0 lib/rubysmith/configuration/loader.rb
rubysmith-4.7.0 lib/rubysmith/configuration/loader.rb
rubysmith-4.6.1 lib/rubysmith/configuration/loader.rb
rubysmith-4.6.0 lib/rubysmith/configuration/loader.rb
rubysmith-4.5.0 lib/rubysmith/configuration/loader.rb
rubysmith-4.4.0 lib/rubysmith/configuration/loader.rb
rubysmith-4.3.0 lib/rubysmith/configuration/loader.rb
rubysmith-4.2.0 lib/rubysmith/configuration/loader.rb