Class: AM::Config
Constant Summary
MessageControl::ERROR_MESSAGE, MessageControl::NOTICE_MESSAGE, MessageControl::WARNING_MESSAGE
Instance Attribute Summary collapse
Instance Method Summary
collapse
#error, #notice, #print, #warning
Constructor Details
#initialize ⇒ Config
Returns a new instance of Config.
10
11
12
|
# File 'lib/config.rb', line 10
def initialize
load_config
end
|
Instance Attribute Details
#al ⇒ Object
Returns the value of attribute al.
8
9
10
|
# File 'lib/config.rb', line 8
def al
@al
end
|
#pg ⇒ Object
Returns the value of attribute pg.
8
9
10
|
# File 'lib/config.rb', line 8
def pg
@pg
end
|
Instance Method Details
#add_config(new_alias) ⇒ Object
20
21
22
23
24
25
26
27
|
# File 'lib/config.rb', line 20
def add_config(new_alias)
@al.merge!(new_alias)
if save_config
notice(:success_add_command, [new_alias.first.to_a, CONFIG_FILE].flatten)
else
error(add_command,[ak, av])
end
end
|
#delete_config(del_alias) ⇒ Object
29
30
31
32
33
34
35
36
|
# File 'lib/config.rb', line 29
def delete_config(del_alias)
@al.delete(del_alias)
if save_config
notice(:success_delete_command, [del_alias, CONFIG_FILE])
else
error(:fail_delete, del_alias)
end
end
|
#file_load(file_name) ⇒ Object
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/config.rb', line 57
def file_load(file_name)
buf = []
File.open(file_name, 'r') do |file|
file.each_line do |line|
line = line.strip
buf << line.gsub(/^alias /, '').split('=', 2) if line !~ /^$/ && line =~ /.+=.+/ && line !~ /^#.*/
end
end if File.exists?(file_name)
Hash[buf]
end
|
#file_write(file_name, config) ⇒ Object
44
45
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/config.rb', line 44
def file_write(file_name, config)
tmp_file = file_name + '.tmp'
file = File.open(tmp_file, "w")
config.each do |k,v|
r = "alias #{k.to_s}=#{v.to_s}"
file.puts(r)
end
file.close
(File.rename(tmp_file, file_name) == 0)
end
|
#load_config ⇒ Object
14
15
16
17
18
|
# File 'lib/config.rb', line 14
def load_config
@al = file_load(CONFIG_FILE)
@pg = file_load(LOCAL_FILE)
@al.delete('aml')
end
|
#save_config ⇒ Object
38
39
40
41
42
|
# File 'lib/config.rb', line 38
def save_config
(
file_write(CONFIG_FILE, @al.merge({"aml" => "'source ~/.am_config'"}))
)
end
|