module Config
Simple YAML configuration for an application. Uses singleton pattern for single source of truth
Public Instance Methods
[](key)
click to toggle source
Hash
like getter
# File lib/nub/config.rb, line 76 def [](key) return @@yml[key] end
[]=(key, val)
click to toggle source
Hash
like setter
# File lib/nub/config.rb, line 81 def []=(key, val) return @@yml[key] = val end
exists?()
click to toggle source
Simple bool whether the config exists or not on disk
# File lib/nub/config.rb, line 71 def exists? return File.exists?(@@path) end
get!(key)
click to toggle source
Get the given key and raise an error if it doesn't exist
# File lib/nub/config.rb, line 86 def get!(key) Log.die("couldn't find '#{key}' in config") if !@@yml.key?(key) return @@yml[key] end
init(config)
click to toggle source
Singleton new alternate @param config [String] name or path of the config file
# File lib/nub/config.rb, line 41 def init(config) # Determine caller's file path to look for sidecar config caller_path = caller_locations(1, 1).first.path path = File.expand_path(File.join(File.dirname(caller_path), config)) if !File.exists?(path) path = "/home/#{User.name}/.config/#{config.split('/').first}" end # Don't reload if its already been done return Config if @@path == path @@path = path # Open the config file or create in memory yml begin @@yml = File.exists?(@@path) ? YAML.load_file(@@path) : {} rescue Exception => e Log.die(e) end return Config end
reset()
click to toggle source
Set back to defaults read for init again
# File lib/nub/config.rb, line 65 def reset self.yml = {} self.path = "" end
save()
click to toggle source
Save the config file
# File lib/nub/config.rb, line 92 def save return unless @@yml File.open(@@path, 'w', 0600){|f| f.write(@@yml.to_yaml)} end