module Terrestrial::YamlHelper
Public Class Methods
read(path)
click to toggle source
# File lib/terrestrial/yaml_helper.rb, line 13 def read(path) result = YAML.load_file(path) if result symbolize_keys(result) else {} end end
update(path, new_content)
click to toggle source
# File lib/terrestrial/yaml_helper.rb, line 23 def update(path, new_content) begin old_content = read(path) rescue Errno::ENOENT old_content = {} end write(path, old_content.merge(new_content)) end
write(path, content)
click to toggle source
# File lib/terrestrial/yaml_helper.rb, line 7 def write(path, content) File.open(path, 'w+') do |f| f.write stringfy_keys(content).to_yaml end end
Private Class Methods
stringfy_keys(h)
click to toggle source
# File lib/terrestrial/yaml_helper.rb, line 35 def stringfy_keys(h) h.keys.each do |k| ks = k.respond_to?(:to_s) ? k.to_s: k h[ks] = h.delete k # Preserve order even when k == ks stringfy_keys h[ks] if h[ks].kind_of? Hash end h end
symbolize_keys(h)
click to toggle source
# File lib/terrestrial/yaml_helper.rb, line 44 def symbolize_keys(h) h.keys.each do |k| ks = k.respond_to?(:to_sym) ? k.to_sym : k h[ks] = h.delete k # Preserve order even when k == ks symbolize_keys! h[ks] if h[ks].kind_of? Hash end h end