Module: Confuse::ConfigMixin

Included in:
Config, ConfigBase
Defined in:
lib/confuse/config_mixin.rb

Overview

Mixin for configuration.

Instance Method Summary collapse

Instance Method Details

#[](key) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/confuse/config_mixin.rb', line 30

def [](key)
  namespace = find_namespace(key) || :default
  rest_of_key = rest_of_key(key, namespace)
  ns = namespaces[namespace]
  return ns unless rest_of_key
  ns[rest_of_key, self]
end

#[]=(key, value) ⇒ Object



38
39
40
41
# File 'lib/confuse/config_mixin.rb', line 38

def []=(key, value)
  puts 'WARNING: changing config after it has been set!'
  mixin_config!({ key => value })
end

#find_namespace(key) ⇒ Object

We allow the namespace and the key to be concatenated with an ‘_’, so this method is to search the possible substrings that could make up the namespace for a key.

This does not guarentee that the suffix of the namespace that is found is a valid key in that namespace.

Parameters:

  • key (Symbol)

    to search



64
65
66
67
68
69
# File 'lib/confuse/config_mixin.rb', line 64

def find_namespace(key)
  until key.to_s.empty? || @namespaces[key.to_sym]
    key = (s = key.to_s)[0, s.rindex('_') || 0]
  end
  key.to_s.empty? ? nil : key.to_sym
end

#load_config_dir(config_dir) ⇒ Object



86
87
88
89
90
# File 'lib/confuse/config_mixin.rb', line 86

def load_config_dir(config_dir)
  Dir[config_dir + '/*.{ini,conf,yaml}'].map do |conf_file|
    load_config_file(conf_file)
  end
end

#load_config_file(conf_file) ⇒ Object



92
93
94
95
96
# File 'lib/confuse/config_mixin.rb', line 92

def load_config_file(conf_file)
  conf = load_ini(conf_file) || load_yaml(conf_file)
  raise "Can't parse #{conf_file}" unless conf
  conf
end

#load_ini(file) ⇒ Object



113
114
115
116
117
118
119
120
# File 'lib/confuse/config_mixin.rb', line 113

def load_ini(file)
  begin
    # make sure they keys are ruby symbols
    symbolise_hash_keys(IniFile.load(file).to_h)
  rescue IniFile::Error
    nil
  end
end

#load_namespaces(new_namespaces) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/confuse/config_mixin.rb', line 12

def load_namespaces(new_namespaces)
  new_namespaces.each do |key, value|
    existing = namespaces[key]
    existing ? existing.merge!(value) : namespaces[key] = value.clone
  end
  @foo = true
end

#load_yaml(file) ⇒ Object



130
131
132
# File 'lib/confuse/config_mixin.rb', line 130

def load_yaml(file)
  YAML.load(File.read(file))
end

#mixin_config!(config) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/confuse/config_mixin.rb', line 98

def mixin_config!(config)
  config.each do |key, value|
    namespace_name = find_namespace(key) || :default
    namespace = @namespaces[namespace_name]
    if value.respond_to?(:keys)
      # if its a hash, set each key in the hash as a config item in the
      # namespace
      value.each { |k, v| namespace[k] = v }
    else
      # otherwise, set it directly in the namespace
      namespace[rest_of_key(key, namespace_name)] = value
    end
  end
end

#namespacesObject



8
9
10
# File 'lib/confuse/config_mixin.rb', line 8

def namespaces
  @namespaces ||= {}
end

#read_files(file_paths) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/confuse/config_mixin.rb', line 20

def read_files(file_paths)
  Array(file_paths).map do |path|
    if File.directory?(path)
      load_config_dir(path)
    elsif File.exists?(path)
      load_config_file(path)
    end
  end.flatten.compact.each { |c| mixin_config!(c) }
end

#rest_of_key(key, namespace) ⇒ Object

Once we’ve found the namespace, we want to find the rest of the string to use as the rest of the key. If the namespace isn’t a substring of the key (e.g. :default), we return the key unaltered.

namespace.

Parameters:

  • key (Symbol)

    The full key

  • namespace (Symbol)

    The substring of the key that is the



78
79
80
81
82
83
84
# File 'lib/confuse/config_mixin.rb', line 78

def rest_of_key(key, namespace)
  key_s = key.to_s
  namespace_s = namespace.to_s
  return nil if key_s == namespace_s
  index = key_s.index(namespace_s) && (namespace_s.length + 1)
  key_s[index || 0, key_s.length].to_sym
end

#symbolise_hash_keys(hash) ⇒ Object



122
123
124
125
126
127
128
# File 'lib/confuse/config_mixin.rb', line 122

def symbolise_hash_keys(hash)
  hash.reduce({}) do |memo, (key, val)|
    memo[key.to_sym] =
    val.respond_to?(:reduce) ? symbolise_hash_keys(val) : val
  memo
  end
end

#to_hashObject



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/confuse/config_mixin.rb', line 43

def to_hash
  namespaces.reduce({}) do |memo, (name, namespace)|
    namespace.keys.each do |key|
      if name != :default
        memo[:"#{name}_#{key}"] = namespace[key, self]
      else
        memo[:"#{key}"] = namespace[key, self]
      end
    end
    memo
  end
end