X-Git-Url: https://git.r.bdr.sh/rbdr/lyricli.rb/blobdiff_plain/823e558b5cd2ec219d0fc7226c54f2ee7ad807d2..803172831f77eea8c97c6141de1c1a9375c81ca1:/lib/lyricli/configuration.rb diff --git a/lib/lyricli/configuration.rb b/lib/lyricli/configuration.rb index 8d26b7f..53a3e2b 100644 --- a/lib/lyricli/configuration.rb +++ b/lib/lyricli/configuration.rb @@ -1,6 +1,9 @@ module Lyricli + + # This class handles the configuration of Lyricli class Configuration + # Defines the paths to the default and user configuration files def initialize @config_path = "~/.lyricli.conf" @defaults_path = "defaults.json" @@ -9,45 +12,79 @@ module Lyricli @@instance = Configuration.new + # Ensure this is only called once. Only use the instance class variable + # to access this method, as its constructor is private. def self.instance @@instance end + # Access configuration properties, loads config if needed beforehand. + # + # @param [String] key the configuration key to access + # @return [String, Hash, Array] the value of the configuration key. def [](key) load_config unless @config @config[key] end + # Assigns a new value to a configuration key, loads config if needed and + # saves it after updating. + # + # @param [String] key the configuration key to set + # @param [Object] value the value for the configuration key, can be any + # object as long as it can be converted to JSON def []=(key, value) load_config unless @config @config[key] = value save_config end - private_class_method :new + # Deletes a key from the configuration, loads config if needed and saves + # it after deleting. + # + # @param [String] key the key to delete + def delete(key) + load_config unless @config + @config.delete(key) + save_config + end - private + private_class_method :new - # TODO: Apart from this, load a default yml that will be used for this. - # And just extend everything from the user's config. + # Loads the configuration from the user file, attempts to create it from + # defaults if it's not present. sets the `@config` instance variable. def load_config - # path = File.expand_path(@config_path) - path_root = File.expand_path(File.dirname(__FILE__)) - path = File.join(path_root, @defaults_path) + path = File.expand_path(@config_path) - if File.exists?(path) + if File.exist?(path) file = File.new(path, "r") @config = MultiJson.decode(file.read) else - @config = {} + load_default_config end end + # Serializes the `@config` Hash to JSON and saves it to a file. def save_config path = File.expand_path(@config_path) file = File.new(path, "w") file.print(MultiJson.encode(@config)) file.close end + + private + + # Loads the default configuration from a JSON file + def load_default_config + # Load the default + path = File.join(::Lyricli.root, "config", @defaults_path) + + if File.exist?(path) + file = File.new(path, "r") + @config = MultiJson.decode(file.read) + else + @config = {} + end + end end end