Sha256: f9de410c8cbd661190419e991410a3df7fe3b5664b58e340e7574adfd420ee5a

Contents?: true

Size: 1.23 KB

Versions: 5

Compression:

Stored size: 1.23 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 = 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: context
    end

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

    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 => error
      raise Errors::Syntax, error.message
    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

5 entries across 5 versions & 1 rubygems

Version Path
runcom-7.3.0 lib/runcom/config.rb
runcom-7.2.0 lib/runcom/config.rb
runcom-7.1.3 lib/runcom/config.rb
runcom-7.1.2 lib/runcom/config.rb
runcom-7.1.1 lib/runcom/config.rb