Sha256: e12645fe6c6edd51174bb7cb07407b485b4b823ea59bc3516aa98485fa3a75a6

Contents?: true

Size: 1.32 KB

Versions: 7

Compression:

Stored size: 1.32 KB

Contents

# frozen_string_literal: true

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

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

    DEFAULT_FILE_NAME = "configuration.yml"

    def initialize name, file_name: DEFAULT_FILE_NAME, defaults: {}
      @name = name
      @file_name = file_name
      @defaults = defaults
      @settings = defaults.deep_merge process_settings
    end

    def path
      paths.find(&:exist?)
    end

    def paths
      XDG::Config.new(home: Runcom::Paths::Friendly).all.map do |root|
        Pathname "#{root}/#{name}/#{file_name}"
      end
    end

    def merge other
      self.class.new name, file_name: file_name, defaults: settings.deep_merge(other.to_h)
    end

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

    alias eql? ==

    def hash
      [name, file_name, to_h, self.class].hash
    end

    def to_h
      settings
    end

    private

    attr_reader :name, :file_name, :defaults, :settings

    def process_settings
      load_settings
    rescue Psych::SyntaxError => error
      raise Errors::Syntax, error.message
    rescue StandardError
      defaults
    end

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

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
runcom-5.0.2 lib/runcom/config.rb
runcom-5.0.1 lib/runcom/config.rb
runcom-5.0.0 lib/runcom/config.rb
runcom-4.2.1 lib/runcom/config.rb
runcom-4.2.0 lib/runcom/config.rb
runcom-4.1.0 lib/runcom/config.rb
runcom-4.0.0 lib/runcom/config.rb