Class: Svenbot::UserManager

Inherits:
Object
  • Object
show all
Defined in:
lib/svenbot/user_manager.rb

Overview

Keep state tracking who is interested in what commits. Users can register or unregister for paths. Commits to those paths will result in a message being emitted.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeUserManager

Create a new UserManager instance



15
16
17
18
# File 'lib/svenbot/user_manager.rb', line 15

def initialize
  @users = {}
  @paths = {}
end

Instance Attribute Details

#pathsObject (readonly)

A hash of paths we are interested in, and which users map to them.



12
13
14
# File 'lib/svenbot/user_manager.rb', line 12

def paths
  @paths
end

#usersObject (readonly)

A hash of registered users.



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

def users
  @users
end

Instance Method Details

#paths_for(jid) ⇒ Object

Return a sorted list of paths that jid has registered.



35
36
37
# File 'lib/svenbot/user_manager.rb', line 35

def paths_for(jid)
  get_user(jid).paths.sort
end

#register(jid, path) ⇒ Object

Register jid as interested in commits to path. If path isn’t specified, default to “everything” (i.e. “/”)



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

def register(jid, path)
  user = get_user(jid)
  user << path
  add_path_for_user path, user
end

#unregister(jid, path) ⇒ Object

Remove messages about commits to a certain path.



29
30
31
32
# File 'lib/svenbot/user_manager.rb', line 29

def unregister(jid, path)
  user = @users.delete jid
  remove_path_for_user path, user if user
end

#users_for(path) ⇒ Object

Return a sorted list of jids that have registered an interest in path



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/svenbot/user_manager.rb', line 40

def users_for(path)
  interested = Set.new
  while path != "/"
    interested.merge users_for_path(path)
    path = File.dirname path
  end
  # Be nice to get rid of this special case.
  interested.merge users_for_path(path)
  # Return jids, as User is an impl detail
  return interested.collect { |u| u.jid }.sort
end