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.
56 57 58 59 |
# File 'lib/sourced/message.rb', line 56 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.
80 81 82 83 84 85 86 87 88 89 |
# File 'lib/sourced/message.rb', line 80 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.
71 72 73 |
# File 'lib/sourced/message.rb', line 71 def []=(key, klass) @lookup[key] = klass end |
#all {|Class| ... } ⇒ Enumerator<Class>
All registered message classes across this registry and subclass registries.
95 96 97 98 99 100 |
# File 'lib/sourced/message.rb', line 95 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.
62 |
# File 'lib/sourced/message.rb', line 62 def keys = @lookup.keys |
#subclasses ⇒ Array<Class>
Returns direct subclasses of the root message class.
65 |
# File 'lib/sourced/message.rb', line 65 def subclasses = .subclasses |