Class: Hector::Channel

Inherits:
Object
  • Object
show all
Defined in:
lib/hector/channel.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Channel

Returns a new instance of Channel.



49
50
51
52
53
# File 'lib/hector/channel.rb', line 49

def initialize(name)
  @name = name
  @user_sessions = []
  @created_at = Time.now
end

Instance Attribute Details

#created_atObject (readonly)

Returns the value of attribute created_at.



3
4
5
# File 'lib/hector/channel.rb', line 3

def created_at
  @created_at
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/hector/channel.rb', line 3

def name
  @name
end

#topicObject (readonly)

Returns the value of attribute topic.



3
4
5
# File 'lib/hector/channel.rb', line 3

def topic
  @topic
end

#user_sessionsObject (readonly)

Returns the value of attribute user_sessions.



3
4
5
# File 'lib/hector/channel.rb', line 3

def user_sessions
  @user_sessions
end

Class Method Details

.create(name) ⇒ Object



16
17
18
19
20
# File 'lib/hector/channel.rb', line 16

def create(name)
  new(name).tap do |channel|
    channels[normalize(name)] = channel
  end
end

.delete(name) ⇒ Object



26
27
28
# File 'lib/hector/channel.rb', line 26

def delete(name)
  channels.delete(name)
end

.find(name) ⇒ Object



6
7
8
# File 'lib/hector/channel.rb', line 6

def find(name)
  channels[normalize(name)]
end

.find_all_for_session(session) ⇒ Object



10
11
12
13
14
# File 'lib/hector/channel.rb', line 10

def find_all_for_session(session)
  channels.values.find_all do |channel|
    channel.has_session?(session)
  end
end

.find_or_create(name) ⇒ Object



22
23
24
# File 'lib/hector/channel.rb', line 22

def find_or_create(name)
  find(name) || create(name)
end

.normalize(name) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/hector/channel.rb', line 30

def normalize(name)
  name.force_encoding("UTF-8") if name.respond_to?(:force_encoding)
  if name =~ /^[#&+!][#&+!\-\w\p{L}\p{M}\p{N}\p{S}\p{P}\p{Co}]{1,49}$/u && name !~ /,/
    name.downcase
  else
    raise NoSuchChannel, name
  end
end

.reset!Object



39
40
41
# File 'lib/hector/channel.rb', line 39

def reset!
  @channels = nil
end

Instance Method Details

#broadcast(command, *args) ⇒ Object



55
56
57
58
59
# File 'lib/hector/channel.rb', line 55

def broadcast(command, *args)
  catch :stop do
    Session.broadcast_to(sessions, command, *args)
  end
end

#change_topic(session, topic) ⇒ Object



61
62
63
# File 'lib/hector/channel.rb', line 61

def change_topic(session, topic)
  @topic = { :body => topic, :nickname => session.nickname, :time => Time.now }
end

#channel?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/hector/channel.rb', line 65

def channel?
  true
end

#deliver(message_type, session, options) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/hector/channel.rb', line 69

def deliver(message_type, session, options)
  if has_session?(session)
    broadcast(message_type, name, options.merge(:except => session))
  else
    raise CannotSendToChannel, name
  end
end

#destroyObject



77
78
79
# File 'lib/hector/channel.rb', line 77

def destroy
  self.class.delete(name)
end

#has_session?(session) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/hector/channel.rb', line 81

def has_session?(session)
  sessions.include?(session)
end

#join(session) ⇒ Object



85
86
87
88
# File 'lib/hector/channel.rb', line 85

def join(session)
  return if has_session?(session)
  user_sessions.push(session)
end

#nicknamesObject



90
91
92
# File 'lib/hector/channel.rb', line 90

def nicknames
  user_sessions.map { |session| session.nickname }
end

#part(session) ⇒ Object



94
95
96
97
# File 'lib/hector/channel.rb', line 94

def part(session)
  user_sessions.delete(session)
  destroy if user_sessions.empty?
end

#servicesObject



99
100
101
# File 'lib/hector/channel.rb', line 99

def services
  Service.all
end

#sessionsObject



103
104
105
# File 'lib/hector/channel.rb', line 103

def sessions
  services + user_sessions
end