class CConfig::Config

Config is the main class of this library. It allows you to fetch the current configuration (after merging the values from all sources) as a hash. This has will have some specials methods: `::CConfig::HashUtils::Extensions#enabled?`, `::CConfig::HashUtils::Extensions#disabled?` and `::CConfig::HashUtils::Extensions#default_of`.

Public Class Methods

new(default:, local:, prefix:) click to toggle source

Instantiate an object with `default` as the path to the default configuration, `local` as the alternate file, and `prefix` as the prefix for environment variables. The `prefix` will take “cconfig” as the default.

Note: the `local` value will be discarded in favor of the `#{prefix}_LOCAL_CONFIG_PATH` environment variable if it was set.

# File lib/cconfig/cconfig.rb, line 38
def initialize(default:, local:, prefix:)
  @default = default
  @prefix  = prefix || "cconfig"
  @local   = ENV["#{@prefix.upcase}_LOCAL_CONFIG_PATH"] || local
end

Public Instance Methods

fetch() click to toggle source

Returns a hash with the app configuration contained in it.

# File lib/cconfig/cconfig.rb, line 45
def fetch
  cfg   = File.file?(@default) ? YAML.load_file(@default) : {}
  local = fetch_local

  hsh = strict_merge_with_env(default: cfg, local: local, prefix: @prefix)
  hsh.extend(::CConfig::HashUtils::Extensions)
  hsh.defaults = cfg
  hsh
end
to_s() click to toggle source

Returns a string representation of the evaluated configuration.

# File lib/cconfig/cconfig.rb, line 56
def to_s
  hide_password(fetch.dup).to_yaml
end

Protected Instance Methods

fetch_local() click to toggle source

Returns a hash with the alternate values that have to override the default ones.

# File lib/cconfig/cconfig.rb, line 64
def fetch_local
  if File.file?(@local)
    # Check for bad user input in the local config.yml file.
    local = YAML.load_file(@local)
    raise FormatError unless local.is_a?(::Hash)

    local
  else
    {}
  end
end