Class: Eg::Book::ChatRoom

Inherits:
Object
  • Object
show all
Defined in:
lib/eg/book/chat_server_actions.rb

Instance Method Summary collapse

Constructor Details

#initializeChatRoom

Returns a new instance of ChatRoom.



31
32
33
34
# File 'lib/eg/book/chat_server_actions.rb', line 31

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

Instance Method Details

#connect_user(user_name) ⇒ Object



35
36
37
38
39
# File 'lib/eg/book/chat_server_actions.rb', line 35

def connect_user user_name
  return false unless @users[user_name].nil?
  @users[user_name] = User.new user_name
  return true
end

#occupants(room_name) ⇒ Object

Raises:

  • (Exception)


53
54
55
56
57
# File 'lib/eg/book/chat_server_actions.rb', line 53

def occupants room_name
  room = @rooms[room_name]
  raise Exception.new("Unknown room: #{room_name}") unless room
  room.occupant_count
end

#user_creates_room(user_name, room_name) ⇒ Object

Raises:

  • (Exception)


40
41
42
43
44
45
# File 'lib/eg/book/chat_server_actions.rb', line 40

def user_creates_room user_name, room_name
  user = @users[user_name]
  raise Exception.new("Unknown user name: #{user_name}") unless user
  raise Exception.new("Duplicate room name: #{room_name}") if @rooms.include? room_name
  @rooms[room_name] = Room.new room_name, user, self
end

#user_enters_room(user_name, room_name) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/eg/book/chat_server_actions.rb', line 46

def user_enters_room user_name, room_name
  user = @users[user_name]
  room = @rooms[room_name]
  return false if user.nil? or room.nil?
  room.add user
  return true
end