Class: DuoRuby::Group

Inherits:
Object
  • Object
show all
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.

Examples:

Adding a client to a group and broadcasting

server.group(:lobby) << client
server.group(:lobby).send(:announcement, text: "Server restart in 5 min")

Defined Under Namespace

Classes: Selection

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Group

Returns a new instance of Group.

Parameters:

  • name (String, Symbol)

    the group name; stored as a Symbol



49
50
51
52
# File 'lib/duoruby/group.rb', line 49

def initialize(name)
  @name = name.to_sym
  @members = []
end

Instance Attribute Details

#membersArray<Client> (readonly)

Returns current members, in the order they joined.

Returns:

  • (Array<Client>)

    current members, in the order they joined



46
47
48
# File 'lib/duoruby/group.rb', line 46

def members
  @members
end

#nameSymbol (readonly)

Returns the group’s name.

Returns:

  • (Symbol)

    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.

Parameters:

Returns:

  • (self)


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

Returns:

  • (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

Returns:

  • (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.

Parameters:

Returns:

  • (Client)

    the removed client



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.

Parameters:

  • event (String, Symbol)

    the event name

  • params

    keyword arguments forwarded to each Client#send

Returns:

  • (self)


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

#sizeObject



82
83
84
# File 'lib/duoruby/group.rb', line 82

def size
  members.size
end