Class: Qcmd::Configuration
- Inherits:
-
Object
- Object
- Qcmd::Configuration
- Defined in:
- lib/qcmd/configuration.rb
Class Method Summary collapse
- .config ⇒ Object
-
.config_file ⇒ Object
and the actual files.
-
.history ⇒ Object
not really config file things, but related to config & settings storage.
- .history_file ⇒ Object
- .home_directory ⇒ Object
- .log ⇒ Object
- .log_file ⇒ Object
- .open_file_for_appending(fname) ⇒ Object
- .qcmd_directory ⇒ Object
- .save ⇒ Object
- .update(key, value) ⇒ Object
Class Method Details
.config ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/qcmd/configuration.rb', line 33 def config @config ||= begin if !File.exists?(config_file) File.open(config_file, 'w') {|f| default = JSON.pretty_generate({'aliases' => Qcmd::Aliases.defaults}) Qcmd.debug "[Configuration config] writing defaults: #{ default }" f.write default } end JSON.load(File.open(config_file)) end end |
.config_file ⇒ Object
and the actual files
70 71 72 |
# File 'lib/qcmd/configuration.rb', line 70 def config_file File.join(home_directory, "settings.json") end |
.history ⇒ Object
not really config file things, but related to config & settings storage
60 61 62 |
# File 'lib/qcmd/configuration.rb', line 60 def history @history ||= open_file_for_appending(history_file) end |
.history_file ⇒ Object
74 75 76 |
# File 'lib/qcmd/configuration.rb', line 74 def history_file File.join(home_directory, "history.log") end |
.home_directory ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/qcmd/configuration.rb', line 10 def home_directory @home_directory ||= begin full_path = File.join(File.('~'), qcmd_directory) begin if !File.exists?(full_path) FileUtils.mkdir_p(full_path) end full_path rescue => ex puts "Failed to create qcmd's home directory at #{ full_path }" puts ex. exit 1 end end end |
.log ⇒ Object
64 65 66 |
# File 'lib/qcmd/configuration.rb', line 64 def log @log ||= open_file_for_appending(log_file) end |
.log_file ⇒ Object
78 79 80 |
# File 'lib/qcmd/configuration.rb', line 78 def log_file File.join(home_directory, "debug.log") end |
.open_file_for_appending(fname) ⇒ Object
27 28 29 30 31 |
# File 'lib/qcmd/configuration.rb', line 27 def open_file_for_appending(fname) f = File.new(fname, 'a') f.sync = true f end |
.qcmd_directory ⇒ Object
6 7 8 |
# File 'lib/qcmd/configuration.rb', line 6 def qcmd_directory ".qcmd" end |
.save ⇒ Object
52 53 54 55 56 |
# File 'lib/qcmd/configuration.rb', line 52 def save File.open(config_file, 'w') {|conf_file| conf_file.write(JSON.pretty_generate(config)) } end |
.update(key, value) ⇒ Object
47 48 49 50 |
# File 'lib/qcmd/configuration.rb', line 47 def update key, value config[key] = value save end |