PageRenderTime 281ms CodeModel.GetById 275ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/Rake/configuration.rb

http://github.com/agross/netopenspace
Ruby | 47 lines | 36 code | 11 blank | 0 comment | 5 complexity | 1138b27a55b6e3848ffadf72c0b4c44a MD5 | raw file
 1require 'yamler'
 2
 3class Configuration
 4	def self.load_yaml(path, opts = {})
 5		yml = Yamler.load(path)
 6		
 7		config = build_inheritance_chain yml, opts[:hash].to_s, opts[:inherit].to_s unless opts[:hash].nil? or opts[:inherit].nil?
 8		apply_overrides yml, config, opts[:override_with].to_s unless opts[:override_with].nil?
 9
10		config
11	end
12	
13	def self.build_inheritance_chain(everything, selector, inheritance_attr)
14		selection = everything[selector]
15		
16		if not selection[inheritance_attr].nil?
17			puts "Settings for '#{selector}' inherit '#{selection[inheritance_attr]}' via attribute '#{inheritance_attr}'"
18			
19			inherited = build_inheritance_chain everything, selection[inheritance_attr], inheritance_attr
20			selection = inherited.recursive_merge(selection)
21			selection.delete(inheritance_attr)
22		end
23		
24		selection
25	end
26	
27	def self.apply_overrides(everything, config, selector)
28		overrides = everything[selector] 
29		
30		return if overrides.nil?
31
32		puts "Applying configuration overrides from #{selector}"
33		config.recursive_merge(overrides)
34	end
35end
36
37class Hash
38	def recursive_merge(h)
39		self.merge!(h) do |key, _old, _new|
40			if _old.class == Hash
41				_old.recursive_merge(_new)
42			else
43				_new
44			end
45		end
46	end
47end