module Emittance::EventLookup

For looking up event classes by their identifiers. Can perform a reverse lookup of identifiers by their associated event class.

Public Class Methods

find_event_klass(*objs) click to toggle source

Look up an {Emittance::Event} class by an identifier. Generates an Event class if no such class exists for that identifier.

EventLookup.find_event_klass :foo
# => FooEvent

When passed subclass of {Emittance::Event}, it returns that event class.

class BarEvent < Emittance::Event
end

EventLookup.find_event_klass BarEvent
# => BarEvent

Instances of an {Emittance::Event} will fetch the class of that instance.

EventLookup.find_event_klass BarEvent.new(nil, nil, nil)
# => BarEvent

Can be passed multiple arguments as a composite identifier. Useful for identifying events by Class#method.

# Not entirely necessary, but for illustrative purposes.
class Baz
  def greet
  end
end

EventLookup.find_event_klass Baz, :greet
# => BazGreetEvent

@param objs [*] anything that can be used to identify an Event class @return [Emittance::Event] the event class identifiable by the params

# File lib/emittance/event_lookup.rb, line 46
def find_event_klass(*objs)
  klass = nil

  klass ||= pass_klass_through(*objs)
  klass ||= klass_of_event(*objs)
  klass ||= find_by_identifier(*objs)

  klass
end
identifiers_for_klass(klass) click to toggle source

@param klass [Class] a subclass of {Emittance::Event} you wish to find the identifiers for @return [Set<Symbol>] a collection of identifiers that can be used to identify that event class

# File lib/emittance/event_lookup.rb, line 58
def identifiers_for_klass(klass)
  Emittance::EventLookup::Registry.identifiers_for_klass(klass)
end
register_identifier(klass, identifier) click to toggle source

Registers an identifier for an Event class. After registering, that identifier can be used to identify those events.

@param klass [Class] the class you wish to register the identifier for @param identifier [Symbol] identifier you want to identify the class as @return [Class] the class for which you've just registered an identifier

# File lib/emittance/event_lookup.rb, line 68
def register_identifier(klass, identifier)
  Emittance::EventLookup::Registry.register_identifier klass: klass, identifier: identifier
end

Private Class Methods

event_klass?(obj) click to toggle source
# File lib/emittance/event_lookup.rb, line 87
def event_klass?(obj)
  obj.is_a?(Class) && obj < Emittance::Event
end
event_object?(obj) click to toggle source
# File lib/emittance/event_lookup.rb, line 91
def event_object?(obj)
  obj.is_a? Emittance::Event
end
find_by_identifier(*objs) click to toggle source
# File lib/emittance/event_lookup.rb, line 82
def find_by_identifier(*objs)
  identifier = CompositeIdentifier.new(*objs).generate
  lookup_identifier identifier
end
klass_of_event(*objs) click to toggle source
# File lib/emittance/event_lookup.rb, line 78
def klass_of_event(*objs)
  objs.length == 1 && event_object?(objs[0]) ? objs[0].class : nil
end
lookup_identifier(identifier) click to toggle source
# File lib/emittance/event_lookup.rb, line 95
def lookup_identifier(identifier)
  Emittance::EventLookup::Registry.fetch_event_klass(identifier)
end
pass_klass_through(*objs) click to toggle source
# File lib/emittance/event_lookup.rb, line 74
def pass_klass_through(*objs)
  objs.length == 1 && event_klass?(objs[0]) ? objs[0] : nil
end