Class: Protobug::Registry
- Inherits:
-
Object
- Object
- Protobug::Registry
- Defined in:
- lib/protobug/registry.rb
Instance Method Summary collapse
- #fetch ⇒ Object
- #freeze ⇒ Object
-
#initialize {|_self| ... } ⇒ Registry
constructor
A new instance of Registry.
- #register(klass) ⇒ Object
Constructor Details
#initialize {|_self| ... } ⇒ Registry
Returns a new instance of Registry.
5 6 7 8 9 10 11 |
# File 'lib/protobug/registry.rb', line 5 def initialize(&blk) @registry = {} return unless blk yield self freeze end |
Instance Method Details
#fetch ⇒ Object
32 33 34 |
# File 'lib/protobug/registry.rb', line 32 def fetch(...) @registry.fetch(...) end |
#freeze ⇒ Object
13 14 15 16 |
# File 'lib/protobug/registry.rb', line 13 def freeze @registry.freeze super end |
#register(klass) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/protobug/registry.rb', line 18 def register(klass) unless klass.is_a? Protobug::BaseDescriptor raise ArgumentError, "expected Protobug::BaseDescriptor, got #{klass.inspect}" end full_name = klass.full_name existing = @registry[full_name] raise ArgumentError, "duplicate class #{full_name}" if existing && existing != klass @registry[full_name] = klass klass.freeze end |