Sha256: c5ec5f0215134803c3cf59795e9da53609fbae72a720a3fe38c84b77c0252d22

Contents?: true

Size: 1.25 KB

Versions: 11

Compression:

Stored size: 1.25 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
    end

    def to_h
      settings
    end

    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

11 entries across 11 versions & 1 rubygems

Version Path
runcom-7.1.0 lib/runcom/config.rb
runcom-7.0.0 lib/runcom/config.rb
runcom-6.6.0 lib/runcom/config.rb
runcom-6.5.0 lib/runcom/config.rb
runcom-6.4.0 lib/runcom/config.rb
runcom-6.3.0 lib/runcom/config.rb
runcom-6.2.0 lib/runcom/config.rb
runcom-6.1.1 lib/runcom/config.rb
runcom-6.1.0 lib/runcom/config.rb
runcom-6.0.1 lib/runcom/config.rb
runcom-6.0.0 lib/runcom/config.rb