Class: Yauth::UserManager

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/yauth/user_manager.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeUserManager

Returns a new instance of UserManager.



5
6
7
# File 'lib/yauth/user_manager.rb', line 5

def initialize
  @list = []
end

Class Method Details

.load(path) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/yauth/user_manager.rb', line 37

def self.load(path)
  manager = self.new
  return manager unless File.exists? path
  open(path) do |io|
    YAML.load(io).each { |h| manager.add(Yauth::User.new(h)) }
  end
  manager
end

Instance Method Details

#add(user) ⇒ Object



9
10
11
12
# File 'lib/yauth/user_manager.rb', line 9

def add(user)
  remove(user.username)
  @list << user
end

#authenticate(username, password) ⇒ Object



32
33
34
35
# File 'lib/yauth/user_manager.rb', line 32

def authenticate(username, password)
  user = find_by_username(username)
  user if user and user.authenticate(password)
end

#each(&block) ⇒ Object



18
19
20
# File 'lib/yauth/user_manager.rb', line 18

def each(&block)
  @list.each(&block)
end

#find_by_username(name) ⇒ Object



28
29
30
# File 'lib/yauth/user_manager.rb', line 28

def find_by_username(name)
  @list.find { |u| u.username == name }
end

#remove(name) ⇒ Object



14
15
16
# File 'lib/yauth/user_manager.rb', line 14

def remove(name)
  @list.delete(find_by_username(name))
end

#save(path) ⇒ Object



22
23
24
25
26
# File 'lib/yauth/user_manager.rb', line 22

def save(path)
  open(path, "w") do |io|
    io << @list.to_yaml
  end
end