]> git.r.bdr.sh - rbdr/lyricli.rb/blame_incremental - lib/lyricli/configuration.rb
Remove the old lyricly.
[rbdr/lyricli.rb] / lib / lyricli / configuration.rb
... / ...
CommitLineData
1module Lyricli
2 class Configuration
3
4 def initialize
5 @config_path = "~/.lyricli.conf"
6 @defaults_path = "defaults.json"
7 @config = nil
8 end
9
10 @@instance = Configuration.new
11
12 def self.instance
13 @@instance
14 end
15
16 def [](key)
17 load_config unless @config
18 @config[key]
19 end
20
21 def []=(key, value)
22 load_config unless @config
23 @config[key] = value
24 save_config
25 end
26
27 def delete(key)
28 load_config unless @config
29 @config.delete(key)
30 save_config
31 end
32
33 private_class_method :new
34
35 # TODO: Apart from this, load a default yml that will be used for this.
36 # And just extend everything from the user's config.
37 def load_config
38 path = File.expand_path(@config_path)
39
40 if File.exists?(path)
41 file = File.new(path, "r")
42 @config = MultiJson.decode(file.read)
43 else
44 load_default_config
45 end
46 end
47
48 def save_config
49 path = File.expand_path(@config_path)
50 file = File.new(path, "w")
51 file.print(MultiJson.encode(@config))
52 file.close
53 end
54
55 private
56
57 def load_default_config
58 # Load the default
59 path_root = File.expand_path(File.dirname(__FILE__))
60 path = File.join(path_root, @defaults_path)
61
62 if File.exists?(path)
63 file = File.new(path, "r")
64 @config = MultiJson.decode(file.read)
65 else
66 @config = {}
67 end
68 end
69 end
70end