/tools/Rake/configuration.rb

http://github.com/agross/netopenspace · Ruby · 47 lines · 36 code · 11 blank · 0 comment · 5 complexity · 1138b27a55b6e3848ffadf72c0b4c44a MD5 · raw file

  1. require 'yamler'
  2. class Configuration
  3. def self.load_yaml(path, opts = {})
  4. yml = Yamler.load(path)
  5. config = build_inheritance_chain yml, opts[:hash].to_s, opts[:inherit].to_s unless opts[:hash].nil? or opts[:inherit].nil?
  6. apply_overrides yml, config, opts[:override_with].to_s unless opts[:override_with].nil?
  7. config
  8. end
  9. def self.build_inheritance_chain(everything, selector, inheritance_attr)
  10. selection = everything[selector]
  11. if not selection[inheritance_attr].nil?
  12. puts "Settings for '#{selector}' inherit '#{selection[inheritance_attr]}' via attribute '#{inheritance_attr}'"
  13. inherited = build_inheritance_chain everything, selection[inheritance_attr], inheritance_attr
  14. selection = inherited.recursive_merge(selection)
  15. selection.delete(inheritance_attr)
  16. end
  17. selection
  18. end
  19. def self.apply_overrides(everything, config, selector)
  20. overrides = everything[selector]
  21. return if overrides.nil?
  22. puts "Applying configuration overrides from #{selector}"
  23. config.recursive_merge(overrides)
  24. end
  25. end
  26. class Hash
  27. def recursive_merge(h)
  28. self.merge!(h) do |key, _old, _new|
  29. if _old.class == Hash
  30. _old.recursive_merge(_new)
  31. else
  32. _new
  33. end
  34. end
  35. end
  36. end