Sha256: 8697122717a0e2494c079ea53ffca349234e52aa7027a55f87c8bf423e5d566f

Contents?: true

Size: 1.21 KB

Versions: 12

Compression:

Stored size: 1.21 KB

Contents

# frozen_string_literal: true

require "yaml"
require "refinements/hashes"

module Runcom
  # A developer friendly wrapper of XDG config.
  class Config
    extend Forwardable
    using Refinements::Hashes

    DEFAULT_CONTEXT = Context.new xdg: XDG::Config

    delegate %i[relative namespace file_name current all inspect] => :common

    def initialize path, defaults: {}, context: DEFAULT_CONTEXT
      @common = Paths::Common.new path, context: @context = context
      @settings = defaults.deep_merge process_settings
      freeze
    end

    def merge other
      self.class.new common.relative, defaults: settings.deep_merge(other.to_h), context:
    end

    # :reek:FeatureEnvy
    def ==(other) = other.is_a?(Config) && (hash == other.hash)

    alias eql? ==

    def hash = [common.relative, to_h, self.class].hash

    def to_h = settings

    private

    attr_reader :common, :context, :settings

    def process_settings
      load_settings
    rescue Psych::SyntaxError
      raise Error, "Invalid configuration: #{common.current}."
    rescue StandardError
      context.defaults
      {}
    end

    def load_settings
      yaml = YAML.load_file common.current
      yaml.is_a?(Hash) ? yaml : {}
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
runcom-8.7.0 lib/runcom/config.rb
runcom-8.6.0 lib/runcom/config.rb
runcom-8.5.0 lib/runcom/config.rb
runcom-8.4.0 lib/runcom/config.rb
runcom-8.3.3 lib/runcom/config.rb
runcom-8.3.2 lib/runcom/config.rb
runcom-8.3.1 lib/runcom/config.rb
runcom-8.3.0 lib/runcom/config.rb
runcom-8.2.0 lib/runcom/config.rb
runcom-8.1.0 lib/runcom/config.rb
runcom-8.0.1 lib/runcom/config.rb
runcom-8.0.0 lib/runcom/config.rb