Class: LEnc::ConfigFile
- Inherits:
-
Object
- Object
- LEnc::ConfigFile
- Defined in:
- lib/lenc/config_file.rb
Overview
Manages a configuration file, which is a set of key/value pairs that can be saved to the file system.
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
-
#exists ⇒ Object
Determine if configuration file exists on disk.
-
#get_directory ⇒ Object
Get the directory containing the configuration file.
-
#initialize(filename, parentDir = nil) ⇒ ConfigFile
constructor
A new instance of ConfigFile.
- #inspect ⇒ Object
-
#remove(key) ⇒ Object
Remove key (and value), if it exists.
-
#set(key, val) ⇒ Object
Store a key => value pair (any existing value for this key is overwritten).
- #to_s ⇒ Object
-
#val(key, defVal = nil) ⇒ Object
Get value for a key.
-
#write ⇒ Object
Write configuration file, if it has changed.
Constructor Details
#initialize(filename, parentDir = nil) ⇒ ConfigFile
Returns a new instance of ConfigFile.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/lenc/config_file.rb', line 17 def initialize(filename, parentDir=nil) parentDir ||= Dir.home @path = File.join(parentDir, filename) @content = {} @origContentStr = "!!!" if exists() contents = read_text_file(@path).strip @content = JSON.parse(contents) @origContentStr = JSON.dump(@content) end end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
11 12 13 |
# File 'lib/lenc/config_file.rb', line 11 def path @path end |
Instance Method Details
#exists ⇒ Object
Determine if configuration file exists on disk
34 35 36 |
# File 'lib/lenc/config_file.rb', line 34 def exists() File.exists?(@path) end |
#get_directory ⇒ Object
Get the directory containing the configuration file
39 40 41 |
# File 'lib/lenc/config_file.rb', line 39 def get_directory() File.dirname(@path) end |
#inspect ⇒ Object
81 82 83 |
# File 'lib/lenc/config_file.rb', line 81 def inspect to_s end |
#remove(key) ⇒ Object
Remove key (and value), if it exists
49 50 51 |
# File 'lib/lenc/config_file.rb', line 49 def remove(key) @content.remove(key) end |
#set(key, val) ⇒ Object
Store a key => value pair (any existing value for this key is overwritten)
44 45 46 |
# File 'lib/lenc/config_file.rb', line 44 def set(key,val) @content[key] = val end |
#to_s ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/lenc/config_file.rb', line 69 def to_s s = 'ConfigFile ' s << path() s << dh(@content) # " [\n" # @content.each_pair do |k,v| # s << d(k) << ' ==> ' << d(v) << "\n" # end # s << "]\n" s end |
#val(key, defVal = nil) ⇒ Object
Get value for a key
65 66 67 |
# File 'lib/lenc/config_file.rb', line 65 def val(key, defVal = nil) @content[key] || defVal end |
#write ⇒ Object
Write configuration file, if it has changed
54 55 56 57 58 59 60 61 |
# File 'lib/lenc/config_file.rb', line 54 def write newStr = JSON.dump(@content) if newStr != @origContentStr @origContentStr = newStr write_text_file(@path,@origContentStr + "\n") end end |