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)


26
27
28
29
# File 'lib/fargo/supports/persistence.rb', line 26

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
22
23
24
# File 'lib/fargo/supports/persistence.rb', line 14

def connection_for nick
  c = @connection_cache.try :[], nick
  if c.nil? || c.connected?
    Fargo.logger.debug "#{self} has connection with: #{nick}: #{c}"
    return c
  end

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

#disconnect_from(nick) ⇒ Object



31
32
33
34
# File 'lib/fargo/supports/persistence.rb', line 31

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



36
37
38
39
40
41
# File 'lib/fargo/supports/persistence.rb', line 36

def nicks_connected_with
  return [] if @connection_cache.nil?

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

#setup_connection_cacheObject



43
44
45
46
47
48
49
50
51
# File 'lib/fargo/supports/persistence.rb', line 43

def setup_connection_cache
  @connection_cache = {}

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