Module: Bitcoin::Config

Defined in:
lib/bitcoin/config.rb

Overview

Load config files, merge options, etc.

Multiple config files are loaded in order, and their settings merged into an existing options hash.

Each config file defines one or more categories which hold the actual settings. Which categories are loaded, and in what order, is specified when you load the config (ie. the order in the file doesn’t matter). The default category “all” is always used, and is always the first (gets overridden by all others)

Constant Summary collapse

CONFIG_PATHS =
"./bitcoin-ruby.yml:~/.bitcoin-ruby.yml:/etc/bitcoin-ruby.yml"

Class Method Summary collapse

Class Method Details

.load(options, categories = [], paths = CONFIG_PATHS) ⇒ Object

Load categories from all files at paths into given options hash.



21
22
23
24
25
26
27
28
# File 'lib/bitcoin/config.rb', line 21

def self.load(options, categories = [], paths = CONFIG_PATHS)
  paths.split(":").reverse.each do |path|
    path.sub!("~", ENV["HOME"])
    next  unless File.exist?(path)
    options = load_file(options, path, categories)
  end
  options
end

.load_file(options, file, c = []) ⇒ Object

Load categories c of a single config file into given options hash.



31
32
33
34
35
36
37
# File 'lib/bitcoin/config.rb', line 31

def self.load_file(options, file, c = [])
  categories = YAML::load_file(file)
  [:all, *(c.is_a?(Array) ? c : [c])].each do |category|
    options = merge(options, categories[category.to_s])  if categories[category.to_s]
  end
  options
end

.merge(a, b) ⇒ Object

Deep-merge hash b into a.



40
41
42
43
44
45
46
47
48
49
# File 'lib/bitcoin/config.rb', line 40

def self.merge(a, b)
  return a unless b
  symbolize(a).merge(symbolize(b)) do |k, o, n|
    if o.is_a?(Hash) && n.is_a?(Hash)
      merge(symbolize(o), symbolize(n))
    else
      n
    end
  end
end

.symbolize(hash) ⇒ Object

Turn all keys in hash into symbols.



52
53
54
# File 'lib/bitcoin/config.rb', line 52

def self.symbolize(hash)
  Hash[hash.map{|k,v|[k.to_sym,v]}]
end