Class: Ya::Directory

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

Instance Method Summary collapse

Constructor Details

#initialize(directory_file) ⇒ Directory

Returns a new instance of Directory.



4
5
6
7
# File 'lib/ya/directory.rb', line 4

def initialize(directory_file)
  @directory_file = directory_file
  FileUtils.touch(@directory_file)
end

Instance Method Details

#add(login, password, attrs = {}) ⇒ Object



21
22
23
# File 'lib/ya/directory.rb', line 21

def add(, password, attrs = {})
  dump_record(.to_sym => Digest.salt_and_hash(password).merge(attrs))
end

#authenticate(login, password) ⇒ Object



14
15
16
17
18
19
# File 'lib/ya/directory.rb', line 14

def authenticate(, password)
  record = lookup()
  return nil unless record
  challenge = Digest.salt_password(record[:salt], password)
  challenge == record[:hash] ? record.merge(:login => ) : false
end

#lookup(login) ⇒ Object



9
10
11
12
# File 'lib/ya/directory.rb', line 9

def lookup()
  return if ! ||  == ''
  load[.to_sym]
end

#mod(login, attrs = {}) ⇒ Object



25
26
27
28
29
# File 'lib/ya/directory.rb', line 25

def mod(, attrs = {})
  record = lookup()
  return nil unless record
  dump_record(.to_sym => record.merge(attrs))
end

#remove(login) ⇒ Object



37
38
39
40
# File 'lib/ya/directory.rb', line 37

def remove()
  dictionary = load
  dictionary.delete(.to_sym) ? dump(dictionary) : false
end

#reset(login, new_password) ⇒ Object



31
32
33
34
35
# File 'lib/ya/directory.rb', line 31

def reset(, new_password)
  record = lookup()
  return nil unless record
  dump_record(.to_sym => record.merge(Digest.salt_and_hash(new_password)))
end