lib/hieracles/formats/yaml.rb in hieracles-0.0.2 vs lib/hieracles/formats/yaml.rb in hieracles-0.0.3

- old
+ new

@@ -25,42 +25,42 @@ def allparams(args) commented_yaml_tree(false) end - def modules(args) - @node.modules.to_yaml - end - - def commented_yaml_tree(without_common = true) tree = @node.params_tree(without_common) params = Hash[@node.params(without_common)] mergetree('---', [], tree, params) end def mergetree(output, key, leaf, params) indent = ' ' * key.count - case leaf.class.name - when 'Hash' - leaf.each do |k, v| - output += "\n" + indent + k + ': ' - output = mergetree(output, key + [k], v, params) - end - when 'Array' - yaml = leaf.to_yaml[4..-1] - aryaml = yaml.each_line.map do |l| - indent + l - end - output += "\n" + indent + "# " + params[key.join('.')][0][:file] - output += "\n" + aryaml.join().chomp - when 'String' - output += leaf - if params["#{key.join('.')}"] - output += " # " + params[key.join('.')][0][:file] - else - raise "#{key}" - end + send("add_#{leaf.class.name.downcase}".to_sym, output, key, leaf, params, indent) + end + + def add_hash(output, key, leaf, params, indent) + leaf.each do |k, v| + output += "\n" + indent + k + ': ' + output = mergetree(output, key + [k], v, params) + end + output + end + + def add_array(output, key, leaf, params, indent) + yaml = leaf.to_yaml[4..-1] + aryaml = yaml.each_line.map do |l| + indent + l + end + output += "\n" + indent + "# " + params[key.join('.')][0][:file] + output += "\n" + aryaml.join().chomp + output + end + + def add_string(output, key, leaf, params, indent) + output += leaf + if params["#{key.join('.')}"] + output += " # " + params[key.join('.')][0][:file] end output end end