Class: CConfig::Config

Inherits:
Object
  • Object
show all
Includes:
HashUtils
Defined in:
lib/cconfig/cconfig.rb

Overview

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 the special method ‘::CConfig::HashUtils::Extensions#enabled?`.

Instance Method Summary collapse

Constructor Details

#initialize(default:, local:, prefix:) ⇒ Config

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.



37
38
39
40
41
# File 'lib/cconfig/cconfig.rb', line 37

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

Instance Method Details

#fetchObject

Returns a hash with the app configuration contained in it.



44
45
46
47
48
49
50
51
# File 'lib/cconfig/cconfig.rb', line 44

def fetch
  cfg   = {}
  cfg   = YAML.load_file(@default) if File.file?(@default)
  local = fetch_local

  hsh = strict_merge_with_env(default: cfg, local: local, prefix: @prefix)
  hsh.extend(::CConfig::HashUtils::Extensions)
end

#to_sObject

Returns a string representation of the evaluated configuration.



54
55
56
# File 'lib/cconfig/cconfig.rb', line 54

def to_s
  hide_password(fetch.dup).to_yaml
end