Simplify simple config file loading for daemons. Assumes the config files all live in DAEMON_ROOT/config and are YAML files. Loaded configs are accessed like a hash with string keys.
Config files can either be keyed by environment (default behavior) or be a normal hash.
Load a config by passing the filename (with or without the .yml extension) to load.
At this stage the configs are read-only.
Any of the keys can be called as methods as well.
Return the config.yml file as a raw hash.
# File lib/daemon_kit/config.rb, line 34 def hash( config, symbolize = false ) self.load( config ).to_h( symbolize ) end
Load the config.yml file from DAEMON_ROOT/config
# File lib/daemon_kit/config.rb, line 22 def load( config ) config = config.to_s config += '.yml' unless config =~ /\.yml$/ path = File.join( DAEMON_ROOT, 'config', config ) raise ArgumentError, "Can't find #{path}" unless File.exists?( path ) new( YAML.load_file( path ) ) end
Pick out a config by name
# File lib/daemon_kit/config.rb, line 50 def []( key ) @data[ key.to_s ] end
# File lib/daemon_kit/config.rb, line 70 def data=( hash ) @data = hash class << @data def symbolize_keys( hash = self ) hash.inject({}) { |result, (key, value)| new_key = case key when String then key.to_sym else key end new_value = case value when Hash then symbolize_keys(value) else value end result[new_key] = new_value result } end end extend_hash( @data ) end
Generated with the Darkfish Rdoc Generator 2.