Module: SousChef::Config
Instance Method Summary collapse
- #compile_attribute(value, context, key_stack) ⇒ Object
-
#compile_attribute_hash(hash, context = nil, key_stack = []) ⇒ Object
Attribute hash “compiler”.
Instance Method Details
#compile_attribute(value, context, key_stack) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/souschef/config.rb', line 7 def compile_attribute(value, context, key_stack) if value.is_a? Hash if value.key? :__items__ value[:__root__] = context[:__root__] context = value end return compile_attribute_hash(value, context, key_stack) elsif value.is_a? String return value.gsub(/\{([\w\.0-9]+)\}/) {|m| res = context.access $1 if not res puts "Couldn\'t read attribute #{$1} from.." ap context raise "Couldn\'t read attribute #{$1}" end if res[/\{([\w\.0-9]+)\}/] next compile_attribute(res, context, key_stack) end res } end value end |
#compile_attribute_hash(hash, context = nil, key_stack = []) ⇒ Object
Attribute hash “compiler”
Parses a hash replacing SousChef::Config.propprop.subpropprop.subprop.subsub tokens by it’s values
Usage:
-
Reference other properties using dot-notation SousChef::Config.sitesite.certssite.certs.root
-
Use a :__default__ key to define default values
-
Use an :__items__ key to define the items to create using the default values
-
:__key__ and :__item__ keys only exist inside __default__
-
Overwrite defaults by adding a key with the same name of the item
Example: default =
name: 'myapp',
user: 'appuser',
certs: {
root: '/etc/ssl/certs/{name',
cert: 'SousChef::Config.certscerts.root/cert.pem'
},
envs: {
__default__: {
name: '__key__',
debug: true
},
__items__: ['dev', 'staging', 'prod'],
prod: {
debug: false
}
}
}
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/souschef/config.rb', line 66 def compile_attribute_hash(hash, context=nil, key_stack=[]) context ||= hash context[:__key__] = '' if not context.key? :__root__ context[:__root__] = context end attr_hash = Hash.new hash.each do |key, val| if not key.to_s.start_with? '__' key_stack.push key begin attr_hash[key] = compile_attribute(val, context, key_stack) rescue Exception => e puts "Error compiling value for #{key_stack.join('.')} in..." ap context[:__item__] raise e end key_stack.pop end end if hash.key? :__items__ hash[:__items__].each do |item| context[:__key__] = item if hash.key? item attr_hash[item] = hash[item] else attr_hash[item] = {} end if hash.key? :__default__ attr_hash[item] = hash[:__default__].deep_merge(attr_hash[item]) end context[:__item__] = attr_hash[item] attr_hash[item].each do |key, val| key_stack.push key attr_hash[item][key] = compile_attribute(val, context, key_stack) key_stack.pop end end end attr_hash.delete :__key__ attr_hash.delete :__root__ attr_hash end |