class Emittance::Dispatcher::RegistrationMap

A proxy for a hash. Identifies special identifiers.

Constants

SPECIAL_IDENTIFIER_REGEX

Attributes

reg_map[R]

Public Class Methods

new() click to toggle source

Build a registration map.

# File lib/emittance/dispatcher/registration_map.rb, line 22
def initialize
  @reg_map = {}
end
special_identifier?(identifier) click to toggle source

@param identifier the identifier we want to know information about @return [Boolean] true if the identifier is a special one, false otherwise

# File lib/emittance/dispatcher/registration_map.rb, line 16
def special_identifier?(identifier)
  identifier.to_s =~ SPECIAL_IDENTIFIER_REGEX
end

Public Instance Methods

[](identifier) click to toggle source

@param identifier the identifier you wish to lookup registrations for @return [RegistrationCollectionProxy] a collection of registrations for that identifier

# File lib/emittance/dispatcher/registration_map.rb, line 28
def [](identifier)
  lookup_term, keys = keys_for(identifier)
  collection_for(lookup_term, keys)
end
each_key(*args, &blk) click to toggle source

@param args args passed to +Hash#each_key+ @param blk block passed to +Hash#each_key+ @return [RegistrationMap] self

# File lib/emittance/dispatcher/registration_map.rb, line 36
def each_key(*args, &blk)
  reg_map.each_key(*args, &blk)
  self
end

Private Instance Methods

collection_for(lookup_term, keys) click to toggle source
# File lib/emittance/dispatcher/registration_map.rb, line 74
def collection_for(lookup_term, keys)
  mappings = Hash[
    keys.map do |key|
      reg_map[key] ||= empty_registration
      [key, reg_map[key]]
    end
  ]

  RegistrationCollectionProxy.new(lookup_term, mappings)
end
empty_registration() click to toggle source
# File lib/emittance/dispatcher/registration_map.rb, line 85
def empty_registration
  Set.new
end
keys_for(identifier) click to toggle source
# File lib/emittance/dispatcher/registration_map.rb, line 45
def keys_for(identifier)
  if special_identifier?(identifier)
    keys_for_special_identifier(identifier)
  else
    keys_for_event_identifier(identifier)
  end
end
keys_for_event_identifier(identifier) click to toggle source
# File lib/emittance/dispatcher/registration_map.rb, line 64
def keys_for_event_identifier(identifier)
  klass = Emittance::EventLookup.find_event_klass(identifier)
  keys = [klass] + keys_matching_event_klass(klass)
  [klass, keys]
end
keys_for_special_identifier(identifier) click to toggle source
# File lib/emittance/dispatcher/registration_map.rb, line 53
def keys_for_special_identifier(identifier)
  keys = [identifier.to_sym]

  case identifier.to_s
  when '@all'
    keys += reg_map.keys.select { |key| key.is_a?(Class) }
  end

  [identifier.to_sym, keys]
end
keys_matching_event_klass(_klass) click to toggle source
# File lib/emittance/dispatcher/registration_map.rb, line 70
def keys_matching_event_klass(_klass)
  [:@all]
end
special_identifier?(identifier) click to toggle source
# File lib/emittance/dispatcher/registration_map.rb, line 89
def special_identifier?(identifier)
  self.class.special_identifier?(identifier)
end