Module: Tinker::Context::InstanceMethods

Defined in:
lib/tinker/context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



16
17
18
# File 'lib/tinker/context.rb', line 16

def id
  @id
end

#rosterObject (readonly)

Returns the value of attribute roster.



16
17
18
# File 'lib/tinker/context.rb', line 16

def roster
  @roster
end

Instance Method Details

#add_client(client) ⇒ Object

Public: Adds a client to the roster

client - The client to add to the roster

Examples

@room.add_client(client)

Returns self



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/tinker/context.rb', line 70

def add_client(client)
  @roster.add(client)
  client.join(self)

  env = Tinker::Event::Environment.new(client, self)
  dispatch Tinker::Event.new("client.join", env)

  broadcast(:action => "meta.roster.add", :params => {:id => client.id}, :except => client)

  self
end

#broadcast(params) ⇒ Object

Public: Sends the message to all connected clients of this room

params - The message to send to the clients

Examples

@room.broadcast({:chat => "message"})

options:

- except => list of clients to whom the message will not be sent

Returns self



55
56
57
58
59
# File 'lib/tinker/context.rb', line 55

def broadcast(params)
  except = Array(params.delete(:except)) || []
  @roster.each{ |client| self.send_to_client(client, params) unless except.include?(client) }
  self
end

#dispatch(event) ⇒ Object



109
110
111
112
# File 'lib/tinker/context.rb', line 109

def dispatch(event)
  event.environment = Tinker::Event::Environment.new(event.environment.client, self)
  super(event)
end

#initializeObject



18
19
20
21
22
23
24
# File 'lib/tinker/context.rb', line 18

def initialize
  @id = SecureRandom.uuid
  @roster = Set.new
  initialize_listeners
  Tinker::Context.contexts[@id] = self
  super
end

#releaseObject

Public: Removes this context from accessible contexts

Examples

@room.release

Returns self



35
36
37
38
39
40
41
42
# File 'lib/tinker/context.rb', line 35

def release
  @roster.each do |client|
    remove_client(client)
  end

  Tinker::Context.contexts.delete @id
  self
end

#remove_client(client) ⇒ Object

Public: Removes a client from the roster

client - The client to remove

Examples

@room.remove_client(client)

Returns self



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/tinker/context.rb', line 91

def remove_client(client)
  @roster.delete client
  client.leave(self)

  env = Tinker::Event::Environment.new(client, self)
  dispatch Tinker::Event.new("client.leave", env)

  broadcast(:action => "meta.roster.remove", :params => {:id => client.id}, :except => client)

  self
end

#send_to_client(client, reply_to = nil, params) ⇒ Object



103
104
105
106
# File 'lib/tinker/context.rb', line 103

def send_to_client(client, reply_to = nil, params)
  params = params.dup.merge({:context => @id, :reply_to => reply_to})
  client.send(params)
end