]>
Commit | Line | Data |
---|---|---|
823e558b BB |
1 | module 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 | private_class_method :new | |
28 | ||
29 | private | |
30 | ||
31 | # TODO: Apart from this, load a default yml that will be used for this. | |
32 | # And just extend everything from the user's config. | |
33 | def load_config | |
34 | # path = File.expand_path(@config_path) | |
35 | path_root = File.expand_path(File.dirname(__FILE__)) | |
36 | path = File.join(path_root, @defaults_path) | |
37 | ||
38 | if File.exists?(path) | |
39 | file = File.new(path, "r") | |
40 | @config = MultiJson.decode(file.read) | |
41 | else | |
42 | @config = {} | |
43 | end | |
44 | end | |
45 | ||
46 | def save_config | |
47 | path = File.expand_path(@config_path) | |
48 | file = File.new(path, "w") | |
49 | file.print(MultiJson.encode(@config)) | |
50 | file.close | |
51 | end | |
52 | end | |
53 | end |