Class: DuoRuby::Group
- Inherits:
-
Object
- Object
- DuoRuby::Group
- Defined in:
- lib/duoruby/group.rb
Overview
A named set of clients that can be messaged as a unit.
Groups provide the broadcast primitive for server-side pub/sub. Membership is bidirectional: the group tracks its members, and each Client tracks which groups it belongs to. This makes it cheap to remove a client from all its groups on disconnect without iterating every group.
Groups are created lazily by Server#group and are keyed by symbol name.
Defined Under Namespace
Classes: Selection
Instance Attribute Summary collapse
-
#members ⇒ Array<Client>
readonly
Current members, in the order they joined.
-
#name ⇒ Symbol
readonly
The group’s name.
Instance Method Summary collapse
-
#add(client) ⇒ self
(also: #<<)
Adds
clientto the group (no-op if already a member). - #channel(name) ⇒ Object
- #empty? ⇒ Boolean
- #except(*clients) ⇒ Object
- #include?(client) ⇒ Boolean
-
#initialize(name) ⇒ Group
constructor
A new instance of Group.
-
#remove(client) ⇒ Client
Removes
clientfrom the group and unregisters the group fromclient.groups. -
#send(event, **params) ⇒ self
Sends
eventwithparamsto every current member. - #send_to_others(client, event, **params) ⇒ Object
- #size ⇒ Object
Constructor Details
#initialize(name) ⇒ Group
Returns a new instance of Group.
49 50 51 52 |
# File 'lib/duoruby/group.rb', line 49 def initialize(name) @name = name.to_sym @members = [] end |
Instance Attribute Details
#members ⇒ Array<Client> (readonly)
Returns current members, in the order they joined.
46 47 48 |
# File 'lib/duoruby/group.rb', line 46 def members @members end |
#name ⇒ Symbol (readonly)
Returns the group’s name.
43 44 45 |
# File 'lib/duoruby/group.rb', line 43 def name @name end |
Instance Method Details
#add(client) ⇒ self Also known as: <<
Adds client to the group (no-op if already a member). Also registers this group in client.groups.
59 60 61 62 63 |
# File 'lib/duoruby/group.rb', line 59 def add(client) members << client unless members.include?(client) client.groups[name] = self self end |
#channel(name) ⇒ Object
99 100 101 |
# File 'lib/duoruby/group.rb', line 99 def channel(name) Channel::Namespace.new(self, name) end |
#empty? ⇒ Boolean
86 87 88 |
# File 'lib/duoruby/group.rb', line 86 def empty? members.empty? end |
#except(*clients) ⇒ Object
90 91 92 |
# File 'lib/duoruby/group.rb', line 90 def except(*clients) Selection.new(members - clients) end |
#include?(client) ⇒ Boolean
78 79 80 |
# File 'lib/duoruby/group.rb', line 78 def include?(client) members.include?(client) end |
#remove(client) ⇒ Client
Removes client from the group and unregisters the group from client.groups.
72 73 74 75 76 |
# File 'lib/duoruby/group.rb', line 72 def remove(client) members.delete(client) client.groups.delete(name) client end |
#send(event, **params) ⇒ self
Sends event with params to every current member.
108 109 110 111 112 113 |
# File 'lib/duoruby/group.rb', line 108 def send(event, **params) replies = members.map { |client| client.send(event, **params) } return replies if question_event?(event) self end |
#send_to_others(client, event, **params) ⇒ Object
94 95 96 97 |
# File 'lib/duoruby/group.rb', line 94 def send_to_others(client, event, **params) except(client).send(event, **params) self end |
#size ⇒ Object
82 83 84 |
# File 'lib/duoruby/group.rb', line 82 def size members.size end |