Class: Hut::Room

Inherits:
Object
  • Object
show all
Defined in:
lib/hut/room.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hut, room_name) ⇒ Room

Returns a new instance of Room.



6
7
8
9
10
11
12
13
14
# File 'lib/hut/room.rb', line 6

def initialize(hut, room_name)
  @hut = hut
  @name = room_name
  @room = @hut.campfire.rooms.select do |room|
    room.name.downcase == room_name
  end.first
  @messages = []
  get_recent_messages
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/hut/room.rb', line 4

def name
  @name
end

Instance Method Details

#add_message(msg) ⇒ Object



30
31
32
33
34
35
# File 'lib/hut/room.rb', line 30

def add_message(msg)
  if msg.body
    @messages << @hut.new_message(self, msg)
    @window.room_was_updated(self) if @window
  end
end

#get_recent_messagesObject



41
42
43
44
45
46
47
# File 'lib/hut/room.rb', line 41

def get_recent_messages
  # Room#recent calls Time.parse
  require 'time'
  @room.recent.each do |msg|
    add_message msg
  end
end

#last_messages(num) ⇒ Object



37
38
39
# File 'lib/hut/room.rb', line 37

def last_messages(num)
  @messages.last(num)
end

#listenObject



20
21
22
23
24
# File 'lib/hut/room.rb', line 20

def listen
  @room.listen do |msg|
    add_message msg
  end
end

#new_message(body) ⇒ Object



26
27
28
# File 'lib/hut/room.rb', line 26

def new_message(body)
  @room.speak body
end

#window=(window) ⇒ Object



16
17
18
# File 'lib/hut/room.rb', line 16

def window=(window)
  @window = window
end