Class: Pantry::ClientRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/pantry/client_registry.rb

Overview

The ClientRegistry keeps track of clients who’ve checked in and supports various querying requests against the list of known clients.

Defined Under Namespace

Classes: ClientRecord

Instance Method Summary collapse

Constructor Details

#initializeClientRegistry

Returns a new instance of ClientRegistry.



7
8
9
# File 'lib/pantry/client_registry.rb', line 7

def initialize
  clear!
end

Instance Method Details

#allObject

Return all known clients



12
13
14
# File 'lib/pantry/client_registry.rb', line 12

def all
  @clients.map {|identity, record| record.client }
end

#all_matching(stream_or_filter) ⇒ Object

Find and return all clients who will receive messages on the given stream or ClientFilter.

If this method is given a block, the block will be processed as a #map of the list of clients and records. Block expected to be of the form:

all_matching(filter) do |client, record|
  ...
end

The ‘record` contains internal knowledge of the Client’s activity. See ClientRecord for what’s contained.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/pantry/client_registry.rb', line 53

def all_matching(stream_or_filter)
  found_client_records =
    case stream_or_filter
    when String
      select_records_matching do |record|
        record.client.filter.matches?(stream_or_filter)
      end
    else
      select_records_matching do |record|
        stream_or_filter.includes?(record.client.filter)
      end
    end

  if block_given?
    found_client_records.map do |record|
      yield(record.client, record)
    end
  else
    found_client_records.map(&:client)
  end
end

#check_in(client) ⇒ Object

Check in a client



22
23
24
# File 'lib/pantry/client_registry.rb', line 22

def check_in(client)
  @clients[client.identity].check_in(client)
end

#clear!Object

Clear out the registry entirely



17
18
19
# File 'lib/pantry/client_registry.rb', line 17

def clear!
  @clients = Hash.new {|hash, key| hash[key] = ClientRecord.new }
end

#find(identity) ⇒ Object

Find info for Client that matches the given identity



32
33
34
35
36
37
38
# File 'lib/pantry/client_registry.rb', line 32

def find(identity)
  if found = @clients[identity]
    found.client
  else
    nil
  end
end

#include?(client) ⇒ Boolean

Has the given client checked in?

Returns:

  • (Boolean)


27
28
29
# File 'lib/pantry/client_registry.rb', line 27

def include?(client)
  @clients[client.identity].checked_in?
end