Sha256: 2030932da96a568c6dbbc02d682bb7a1030424155b587683b7b12b39fae3d163

Contents?: true

Size: 943 Bytes

Versions: 2

Compression:

Stored size: 943 Bytes

Contents

# frozen_string_literal: true

require "pathname"
require "yaml"
require "refinements/hashes"

module Runcom
  # Default gem configuration with support for custom settings.
  class Configuration
    using Refinements::Hashes

    attr_reader :path

    def initialize project_name:, file_name: "configuration.yml", defaults: {}
      @path = load_path project_name, file_name
      @defaults = defaults
      @settings = defaults.deep_merge load_settings
    end

    def merge custom_settings
      settings.deep_merge custom_settings
    end

    def to_h
      settings
    end

    private

    attr_reader :paths, :defaults, :settings

    def load_path project, file
      paths = XDG::Configuration.computed_dirs.map { |root| Pathname "#{root}/#{project}/#{file}" }
      paths.find(&:exist?)
    end

    def load_settings
      yaml = YAML.load_file path
      yaml.is_a?(Hash) ? yaml : {}
    rescue
      defaults
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
runcom-1.2.0 lib/runcom/configuration.rb
runcom-1.1.0 lib/runcom/configuration.rb