Class: Arver::Config

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



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

def initialize
  @tree = Arver::Tree.new
  @users = {}
end

Instance Attribute Details

#treeObject

Returns the value of attribute tree.



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

def tree
  @tree
end

#usersObject

Returns the value of attribute users.



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

def users
  @users
end

Instance Method Details

#==(other) ⇒ Object



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

def == other
  return tree == other.tree && users == other.users if other.is_a?(Arver::Config)
  false
end

#exists?(user) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/arver/config.rb', line 42

def exists?( user )
  !users[user].nil?
end

#gpg_key(user) ⇒ Object



46
47
48
# File 'lib/arver/config.rb', line 46

def gpg_key user
  users[user]['gpg'] if exists?(user)
end

#loadObject



17
18
19
20
21
22
23
24
25
26
# File 'lib/arver/config.rb', line 17

def load
  if !File.directory?(path)
    Arver::Log.error("config "+path+" does not exist")
    exit 1
  end
  @users = load_file(File.join(path,'users')) || {}

  tree.clear
  tree.from_hash(load_file(File.join(path,'disks')))
end

#load_file(filename) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/arver/config.rb', line 28

def load_file( filename )
  if !File.exist?(filename)
    Arver::Log.error("missing config #{filename}")
    exit 1
  end
  YAML.load(File.read(filename))
end

#pathObject



13
14
15
# File 'lib/arver/config.rb', line 13

def path
  File.expand_path( Arver::LocalConfig.instance.config_dir )
end

#saveObject



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

def save
  FileUtils.mkdir_p( path ) unless File.exist?( path )
  File.open( File.join(path,'users'), 'w' ) { |f| f.write( users.to_yaml ) }
  File.open( File.join(path,'disks'), 'w' ) { |f| f.write( tree.to_yaml ) }
end

#slot(user) ⇒ Object



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

def slot user
  users[user]['slot'] if exists?(user)
end

#user_at(slot) ⇒ Object



54
55
56
57
58
59
# File 'lib/arver/config.rb', line 54

def user_at(slot)
  users.each do |name, conf|
    return name if slot == conf['slot']
  end
  'unknown'
end