Class: AM::Config

Inherits:
Object
  • Object
show all
Includes:
MessageControl
Defined in:
lib/config.rb

Constant Summary

Constants included from MessageControl

MessageControl::ERROR_MESSAGE, MessageControl::NOTICE_MESSAGE, MessageControl::WARNING_MESSAGE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from MessageControl

#after_sepalate, #before_sepalate, #error, #notice, #print_message, #warning

Constructor Details

#initializeConfig

Returns a new instance of Config.



10
11
12
# File 'lib/config.rb', line 10

def initialize
  load_config
end

Instance Attribute Details

#alObject

Returns the value of attribute al.



8
9
10
# File 'lib/config.rb', line 8

def al
  @al
end

#pgObject

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_fail,new_alias.flatten)
  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(:delete_command_fail, del_alias)
  end
end

#file_load(file_name) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/config.rb', line 54

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



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/config.rb', line 42

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_configObject



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')            #delete default alias
end

#save_configObject



38
39
40
# File 'lib/config.rb', line 38

def save_config
  (file_write(CONFIG_FILE, @al.merge({"aml" => "'source ~/.am_config'"})))
end