Sha256: a9ddc867157ef07f3c69be600cbcf913d017842d8c58a39854e639a43a6fce12

Contents?: true

Size: 1.49 KB

Versions: 1

Compression:

Stored size: 1.49 KB

Contents

require 'set'
require 'hashlib'

module Onering
  class Config
    class<<self
      DEFAULT_CONFIG={}
      DEFAULT_OPTIONS_FILE=["~/.onering/cli.yml", "/etc/onering/cli.yml"]

      def load(configfile=nil, config={})
        if configfile.nil?
          configfile = []
        else
        # recursively grab all .yml files if directory is specified
          configfile = (File.directory?(configfile) ? Dir.glob(File.join(configfile, "**", "*.yml")).sort : [configfile])
        end

      # list all existing config files from least specific to most
        @_configfiles = (configfile + DEFAULT_OPTIONS_FILE).compact.select{|i|
          (File.exists?(File.expand_path(i)) rescue false)
        }.reverse

      # merge all config files with more-specific values overriding less-specific ones
        @_config ||= DEFAULT_CONFIG
        @_configfiles_seen ||= Set.new()

      # only load files we haven't seen before
        (@_configfiles - @_configfiles_seen.to_a).each do |i|
          c = YAML.load(File.read(File.expand_path(i))) rescue {}
          @_config.deep_merge!(c)
          @_configfiles_seen << i
        end

      # settings specified in the library override everything
        @_config.deep_merge!(config.compact) unless config.empty?

        return @_config
      end

      def get(key, default=nil)
        @_config.get(key, default)
      end

      def set(key, value)
        @_config.set(key, value)
      end

      def unset(key)
        @_config.unset(key)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
onering-client-0.0.83 lib/onering/config.rb