class Pantry::ClientRegistry

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

Public Class Methods

new() click to toggle source
# File lib/pantry/client_registry.rb, line 7
def initialize
  clear!
end

Public Instance Methods

all() click to toggle source

Return all known clients

# File lib/pantry/client_registry.rb, line 12
def all
  @clients.map {|identity, record| record.client }
end
all_matching(stream_or_filter) { |client, record| ... } click to toggle source

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.

# 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) click to toggle source

Check in a client

# File lib/pantry/client_registry.rb, line 22
def check_in(client)
  @clients[client.identity].check_in(client)
end
clear!() click to toggle source

Clear out the registry entirely

# File lib/pantry/client_registry.rb, line 17
def clear!
  @clients = Hash.new {|hash, key| hash[key] = ClientRecord.new }
end
find(identity) click to toggle source

Find info for Client that matches the given identity

# File lib/pantry/client_registry.rb, line 32
def find(identity)
  if found = @clients[identity]
    found.client
  else
    nil
  end
end
include?(client) click to toggle source

Has the given client checked in?

# File lib/pantry/client_registry.rb, line 27
def include?(client)
  @clients[client.identity].checked_in?
end

Protected Instance Methods

select_records_matching() { |record| ... } click to toggle source
# File lib/pantry/client_registry.rb, line 77
def select_records_matching
  selected_records = @clients.clone.select do |identity, record|
    yield(record)
  end

  selected_records.values
end