Class: Truth::Config

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(folder = '/etc/truth') ⇒ Config

Returns a new instance of Config.



4
5
6
# File 'lib/truth/config.rb', line 4

def initialize(folder = '/etc/truth')
  self.config_folder = folder
end

Instance Attribute Details

#config_folderObject

Returns the value of attribute config_folder.



3
4
5
# File 'lib/truth/config.rb', line 3

def config_folder
  @config_folder
end

Instance Method Details

#add_info(key, value) ⇒ Object



44
45
46
47
# File 'lib/truth/config.rb', line 44

def add_info(key, value)
  data[key] = value
  persist_data
end

#add_roles(roles) ⇒ Object



26
27
28
29
# File 'lib/truth/config.rb', line 26

def add_roles(roles)
  data['roles'] = (current_roles + roles).flatten.uniq
  persist_data
end

#configObject



66
67
68
# File 'lib/truth/config.rb', line 66

def config
  @config ||= read_config('config.yml')
end

#current_rolesObject



36
37
38
# File 'lib/truth/config.rb', line 36

def current_roles
  data['roles'] || []
end

#dataObject



40
41
42
# File 'lib/truth/config.rb', line 40

def data
  @data ||= read_config('data.yml')
end

#data=(obj) ⇒ Object



58
59
60
# File 'lib/truth/config.rb', line 58

def data=(obj)
  write_config('data.yml', obj)
end

#environmentObject



12
13
14
# File 'lib/truth/config.rb', line 12

def environment
  self.config['environment']
end

#full_config_path(file) ⇒ Object



70
71
72
# File 'lib/truth/config.rb', line 70

def full_config_path(file)
  File.expand_path(File.join(config_folder,file))
end

#hostnameObject

reads the systems hostname, can be overridden in the config.yml



17
18
19
# File 'lib/truth/config.rb', line 17

def hostname
  self.config['hostname'] || `hostname`.strip
end

#info(key) ⇒ Object



54
55
56
# File 'lib/truth/config.rb', line 54

def info(key)
  data[key]
end

#persist_dataObject



62
63
64
# File 'lib/truth/config.rb', line 62

def persist_data
  write_config('data.yml', @data)
end

#read_config(filename) ⇒ Object



74
75
76
77
78
# File 'lib/truth/config.rb', line 74

def read_config(filename)
  full_path = File.join(config_folder, filename)
  raise "file #{full_path} not found!" unless File.exist?(full_path)
  return YAML.load(File.read(full_path)) || {}
end

#redis_clientObject



22
23
24
# File 'lib/truth/config.rb', line 22

def redis_client
  @redis_client ||= EM::Hiredis::Client.connect(redis_ip)
end

#redis_ipObject



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

def redis_ip
  self.config['redis_ip']
end

#remove_info(key) ⇒ Object



49
50
51
52
# File 'lib/truth/config.rb', line 49

def remove_info(key)
  data.delete key
  persist_data
end

#remove_roles(roles) ⇒ Object



31
32
33
34
# File 'lib/truth/config.rb', line 31

def remove_roles(roles)
  data['roles'] = (current_roles - roles).flatten.uniq
  persist_data
end

#write_config(filename, content) ⇒ Object



80
81
82
83
84
85
86
87
# File 'lib/truth/config.rb', line 80

def write_config(filename, content)
  full_path = File.join(config_folder, filename)
  raise unless File.exist?(full_path)
  content = YAML.dump(content) unless content.is_a? String
  File.open(full_path, 'w') do |f|
    f.puts content
  end
end