Class: Vetinari::ChannelContainer

Inherits:
Object
  • Object
show all
Includes:
Celluloid
Defined in:
lib/vetinari/channel_container.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeChannelContainer

Returns a new instance of ChannelContainer.



11
12
13
# File 'lib/vetinari/channel_container.rb', line 11

def initialize
  @channels = Set.new
end

Instance Attribute Details

#channelsObject (readonly)

Returns the value of attribute channels.



5
6
7
# File 'lib/vetinari/channel_container.rb', line 5

def channels
  @channels
end

Instance Method Details

#[](channel_or_channel_name) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/vetinari/channel_container.rb', line 19

def [](channel_or_channel_name)
  case channel_or_channel_name
  when Channel
    if @channels.include?(channel_or_channel_name)
      channel_or_channel_name
    end
  when String
    @channels.find do |c|
      c.name.downcase == channel_or_channel_name.downcase
    end
  end
end

#add(channel) ⇒ Object



15
16
17
# File 'lib/vetinari/channel_container.rb', line 15

def add(channel)
  @channels << channel
end

#clearObject

Removes all Channels from the ChannelList and returns them.



57
58
59
60
61
# File 'lib/vetinari/channel_container.rb', line 57

def clear
  channels = @channels.dup
  @channels.clear
  channels
end

#finalizeObject



76
77
78
# File 'lib/vetinari/channel_container.rb', line 76

def finalize
  @channels.each(&:terminate)
end

#has_channel?(channel) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/vetinari/channel_container.rb', line 32

def has_channel?(channel)
  self[channel] ? true : false
end

#remove(channel) ⇒ Object



36
37
38
39
40
# File 'lib/vetinari/channel_container.rb', line 36

def remove(channel)
  if has_channel?(channel)
    @channels.delete(channel)
  end
end

#remove_user(user) ⇒ Object

Removes a User from all Channels from the ChannelList. Returning a Set of Channels in which the User was.



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/vetinari/channel_container.rb', line 44

def remove_user(user)
  channels = Set.new

  @channels.each do |channel|
    if channel.remove_user(user)
      channels << channel
    end
  end

  channels
end

#usersObject

Returns a Set of all Users that are in one of the Channels from the ChannelList. TODO: Refactor



66
67
68
69
70
71
72
73
74
# File 'lib/vetinari/channel_container.rb', line 66

def users
  users = Set.new

  @channels.each do |channel|
    users.merge(channel.users)
  end

  users
end