module Riffer::Registrable
Registry of a class’s direct subclasses, keyed by identifier. Extend it onto a base class to look up subclasses in constant time via find and all. Subclasses join implicitly by inheriting; register adds one explicitly, for ephemeral classes a test suite builds and tears down. Registration is not synchronized — register during boot or from a single-threaded test, before concurrent lookups begin.
class Riffer::Tool extend Riffer::Registrable end Riffer::Tool.find("weather_tool") # => WeatherTool
@rbs module-self Class
Public Instance Methods
Source
# File lib/riffer/registrable.rb, line 41 def all identifier_registry.values end
Returns all registered subclasses, implicit and explicit. Carries the same registration rules as find.
Source
# File lib/riffer/registrable.rb, line 32 def find(identifier) identifier_registry[identifier.to_s] end
Finds a registered subclass by identifier, or nil when none matches. Implicit registration covers only *named direct* subclasses: grandchildren are not visible to a grandparent’s find (call find on their direct parent instead), anonymous classes are never registered implicitly, and a subclass whose name no longer resolves back to it is dropped at the next registry rebuild. Duplicate identifiers raise Riffer::DuplicateIdentifierError at first lookup.
Source
# File lib/riffer/registrable.rb, line 57 def register(klass) unless klass.superclass.equal?(self) raise Riffer::ArgumentError, "#{klass} must be a direct subclass of #{self} to register" end key = identifier_key(klass) raise Riffer::ArgumentError, "#{klass} must declare a non-blank identifier to register" if key.strip.empty? existing = identifier_registry[key] raise_duplicate_identifier!(key, existing, klass) if existing explicit_registrations[key] = klass @identifier_registry = nil end
Registers a direct subclass under its identifier, whether or not it is named — unlike implicit registration, it survives a name that no longer resolves, so an ephemeral class stays findable until unregister. Prefer Riffer::Testing for ordinary test setup, which stubs and cleans up automatically.
Raises Riffer::ArgumentError when the identifier is blank or the class is not a direct subclass, and Riffer::DuplicateIdentifierError when the identifier is already taken — including by this same class.
Source
# File lib/riffer/registrable.rb, line 76 def unregister(klass) key, = explicit_registrations.find { |_key, registered| registered.equal?(klass) } return if key.nil? explicit_registrations.delete(key) @identifier_registry = nil end
Removes an explicit registration of klass, leaving implicit registrations untouched.