Class: ConfigHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/config_handler.rb

Constant Summary collapse

CONFIG_DIRECTORY =
".vdms.d"
CONFIG_FILENAME =
"config"
AUTHORS =
'SCP & NMK'
PASSWORD =
'Password'
@@config_values =
nil

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.config_parameter(name) ⇒ Object



62
63
64
65
66
67
# File 'lib/config_handler.rb', line 62

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



94
95
96
# File 'lib/config_handler.rb', line 94

def delete(name)
  set(name, nil)
end

#get(name) ⇒ Object



69
70
71
72
# File 'lib/config_handler.rb', line 69

def get(name)
  load_config_values
  @@config_values[name]
end

#get_config_fileObject



11
12
13
14
15
16
# File 'lib/config_handler.rb', line 11

def get_config_file
  if (! File.directory?(CONFIG_DIRECTORY))
    FileUtils.mkdir(CONFIG_DIRECTORY)
  end
  CONFIG_DIRECTORY + File::SEPARATOR + CONFIG_FILENAME
end

#load_config_valuesObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/config_handler.rb', line 38

def load_config_values
  if @@config_values.class == Hash
    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
        if name.end_with? PASSWORD
          value = Methodical.new(AUTHORS).backward value
        end
        @@config_values[name] = value
      end
    }
  end
end

#replace_nv(name, value, new_file, orig_file) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/config_handler.rb', line 19

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 && ! value.nil?)
    new_file.puts(new_config_line)
  end
end

#set(name, value) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/config_handler.rb', line 74

def set(name, value)
  if value && name.end_with?(PASSWORD)
    value = Methodical.new(AUTHORS).forward value
  end
  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)
  @@config_values = nil
end