Module: Fargo::Supports::Persistence

Extended by:
ActiveSupport::Concern
Included in:
Client
Defined in:
lib/fargo/supports/persistence.rb

Instance Method Summary collapse

Instance Method Details

#connected_with?(nick) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
# File 'lib/fargo/supports/persistence.rb', line 23

def connected_with? nick
  c = @connection_cache.try :[], nick
  c.connected? unless c.nil?
end

#connection_for(nick) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/fargo/supports/persistence.rb', line 14

def connection_for nick
  c = @connection_cache.try :[], nick
  return c if c.nil? || c.connected?

  # If it's present and not connected, remove it from the cache
  @connection_cache.try :delete, nick
  nil
end

#disconnect_from(nick) ⇒ Object



28
29
30
31
# File 'lib/fargo/supports/persistence.rb', line 28

def disconnect_from nick
  c = @connection_cache.try :delete, nick
  c.disconnect unless c.nil?
end

#lock_connection_with!(nick, connection) ⇒ Object



10
11
12
# File 'lib/fargo/supports/persistence.rb', line 10

def lock_connection_with! nick, connection
  @connection_cache[nick] = connection
end

#nicks_connected_withObject



33
34
35
36
37
38
# File 'lib/fargo/supports/persistence.rb', line 33

def nicks_connected_with
  return [] if @connection_cache.nil?

  nicks = @connection_cache.keys
  nicks.reject{ |n| !connected_with? n } 
end

#setup_connection_cacheObject



40
41
42
43
44
45
46
47
48
# File 'lib/fargo/supports/persistence.rb', line 40

def setup_connection_cache
  @connection_cache = {}

  subscribe { |type, hash|
    if type == :hub_disconnected
      nicks_connected_with.each{ |n| disconnect_from n }
    end
  }
end