Class: Sourced::Message::Registry
- Inherits:
-
Object
- Object
- Sourced::Message::Registry
- Defined in:
- lib/sourced/message.rb
Overview
Lookup table mapping type strings to message subclasses.
Instance Method Summary collapse
-
#[](key) ⇒ Class?
Look up a message class by type string.
-
#[]=(key, klass) ⇒ Object
Register a message class under a type string.
-
#all {|Class| ... } ⇒ Enumerator<Class>
All registered message classes across this registry and subclass registries.
-
#initialize(message_class) ⇒ Registry
constructor
A new instance of Registry.
-
#keys ⇒ Array<String>
Registered type strings.
-
#subclasses ⇒ Array<Class>
Direct subclasses of the root message class.
Constructor Details
#initialize(message_class) ⇒ Registry
Returns a new instance of Registry.
55 56 57 58 |
# File 'lib/sourced/message.rb', line 55 def initialize() @message_class = @lookup = {} end |
Instance Method Details
#[](key) ⇒ Class?
Look up a message class by type string. Searches this registry first, then recurses into subclass registries.
79 80 81 82 83 84 85 86 87 88 |
# File 'lib/sourced/message.rb', line 79 def [](key) klass = lookup[key] return klass if klass subclasses.each do |c| klass = c.registry[key] return klass if klass end nil end |
#[]=(key, klass) ⇒ Object
Register a message class under a type string.
70 71 72 |
# File 'lib/sourced/message.rb', line 70 def []=(key, klass) @lookup[key] = klass end |
#all {|Class| ... } ⇒ Enumerator<Class>
All registered message classes across this registry and subclass registries.
94 95 96 97 98 99 |
# File 'lib/sourced/message.rb', line 94 def all(&block) return enum_for(:all) unless block lookup.each_value(&block) subclasses.each { |c| c.registry.all(&block) } end |
#keys ⇒ Array<String>
Returns registered type strings.
61 |
# File 'lib/sourced/message.rb', line 61 def keys = @lookup.keys |
#subclasses ⇒ Array<Class>
Returns direct subclasses of the root message class.
64 |
# File 'lib/sourced/message.rb', line 64 def subclasses = .subclasses |