]>
git.r.bdr.sh - rbdr/lyricli.rb/blob - lib/lyricli/configuration.rb
b90e772b1fa3bfc04ccae56ee8cc2a1b8b366b29
3 # This class handles the configuration of Lyricli
6 # Defines the paths to the default and user configuration files
8 @config_path = "~/.lyricli.conf"
9 @defaults_path = "defaults.json"
13 @
@instance = Configuration
.new
15 # Ensure this is only called once. Only use the instance class variable
16 # to access this method, as its constructor is private.
21 # Access configuration properties, loads config if needed beforehand.
23 # @param [String] key the configuration key to access
24 # @return [String, Hash, Array] the value of the configuration key.
26 load_config
unless @config
30 # Assigns a new value to a configuration key, loads config if needed and
31 # saves it after updating.
33 # @param [String] key the configuration key to set
34 # @param [Object] value the value for the configuration key, can be any
35 # object as long as it can be converted to JSON
37 load_config
unless @config
42 # Deletes a key from the configuration, loads config if needed and saves
45 # @param [String] key the key to delete
47 load_config
unless @config
52 private_class_method
:new
54 # Loads the configuration from the user file, attempts to create it from
55 # defaults if it's not present. sets the `@config` instance variable.
57 path
= File
.expand_path(@config_path)
60 file
= File
.new(path
, "r")
61 @config = MultiJson
.decode(file
.read
)
67 # Serializes the `@config` Hash to JSON and saves it to a file.
69 path
= File
.expand_path(@config_path)
70 file
= File
.new(path
, "w")
71 file
.print(MultiJson
.encode(@config))
77 # Loads the default configuration from a JSON file
78 def load_default_config
80 path
= File
.join(::Lyricli.root
, "config", @defaults_path)
83 file
= File
.new(path
, "r")
84 @config = MultiJson
.decode(file
.read
)