Class: ConfigHandler
- Inherits:
-
Object
- Object
- ConfigHandler
- Defined in:
- lib/config_handler.rb
Constant Summary collapse
- CONFIG_DIRECTORY =
".vdms.d"- CONFIG_FILENAME =
"config"
Class Method Summary collapse
Instance Method Summary collapse
- #delete(name) ⇒ Object
- #get(name) ⇒ Object
- #get_config_file ⇒ Object
- #load_config_values ⇒ Object
- #replace_nv(name, value, new_file, orig_file) ⇒ Object
- #set(name, value) ⇒ Object
Class Method Details
.config_parameter(name) ⇒ Object
55 56 57 58 59 60 |
# File 'lib/config_handler.rb', line 55 def self.config_parameter(name) instance = ConfigHandler.new instance.load_config_values result = @@config_values[name] result = result ? result : "" end |
Instance Method Details
#delete(name) ⇒ Object
83 84 85 |
# File 'lib/config_handler.rb', line 83 def delete(name) set(name, nil) end |
#get(name) ⇒ Object
62 63 64 65 |
# File 'lib/config_handler.rb', line 62 def get(name) load_config_values @@config_values[name] end |
#get_config_file ⇒ Object
7 8 9 10 11 12 |
# File 'lib/config_handler.rb', line 7 def get_config_file if (! File.directory?(CONFIG_DIRECTORY)) FileUtils.mkdir(CONFIG_DIRECTORY) end CONFIG_DIRECTORY + File::SEPARATOR + CONFIG_FILENAME end |
#load_config_values ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/config_handler.rb', line 34 def load_config_values if defined? @@config_values return else @@config_values = {} if (! File.file?(get_config_file)) return end File.open(get_config_file).each { |line| line = line.strip if line[0] == '#' next end name, value = Util.parse_line(line) if value @@config_values[name] = value end } end end |
#replace_nv(name, value, new_file, orig_file) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/config_handler.rb', line 15 def replace_nv(name, value, new_file, orig_file) new_config_line = name + '=' + value.to_s wrote_line = false orig_file.each { |line| old_name, old_value = Util.parse_line(line) if (old_name == name) if ! value.nil? new_file.puts(new_config_line) end wrote_line = true else new_file.puts(line) end } if ( ! wrote_line) new_file.puts(new_config_line) end end |
#set(name, value) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/config_handler.rb', line 67 def set(name, value) config_file = get_config_file if ! File.exists? config_file # make empty file File.open(config_file, "w").close end new_config_file = config_file + ".tmp" updated_file = File.open(new_config_file, "w") orig_file = File.open(config_file) replace_nv(name, value, updated_file, orig_file) updated_file.close orig_file.close FileUtils.rm(config_file, force: true) FileUtils.mv(new_config_file, config_file, :force => true) end |