Class: Takagi::CoAP::Registries::Base
- Inherits:
-
Object
- Object
- Takagi::CoAP::Registries::Base
- Defined in:
- lib/takagi/coap/registries/base.rb
Direct Known Subclasses
ContentFormat, MessageType, Method, Option, Response, Signaling
Class Method Summary collapse
-
.all ⇒ Hash
Get all registered constants.
-
.clear! ⇒ Object
Clear all registrations (useful for testing).
-
.each_value {|Integer| ... } ⇒ Enumerator
Iterate over registered values.
- .inherited(subclass) ⇒ Object
-
.metadata_for(value) ⇒ Hash?
Get registry metadata for a value.
-
.name_for(value) ⇒ String?
Get human-readable name for a value.
-
.register(value, name, symbol = nil, rfc: nil) ⇒ Object
Register a new constant in the registry.
-
.registered?(value) ⇒ Boolean
Check if a value is registered.
-
.rfc_for(value) ⇒ String?
Get RFC reference for a value.
-
.value_for(key) ⇒ Integer?
Get numeric value for a name or symbol.
-
.values ⇒ Array<Integer>
Get all registered values.
Class Method Details
.all ⇒ Hash
Get all registered constants
107 108 109 |
# File 'lib/takagi/coap/registries/base.rb', line 107 def all @mutex.synchronize { registry.transform_values { |info| info[:name] } } end |
.clear! ⇒ Object
Clear all registrations (useful for testing)
139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/takagi/coap/registries/base.rb', line 139 def clear! @mutex.synchronize do registry.clear reverse_registry.clear # Remove constants (carefully) constants(false).each do |const_name| remove_const(const_name) if const_name =~ /^[A-Z_]+$/ end end Takagi::Hooks.emit(:coap_registry_cleared, registry: self) end |
.each_value {|Integer| ... } ⇒ Enumerator
Iterate over registered values
115 116 117 118 119 120 121 |
# File 'lib/takagi/coap/registries/base.rb', line 115 def each_value(&block) return enum_for(:each_value) unless block_given? # Get snapshot to avoid holding lock during iteration snapshot = @mutex.synchronize { registry.keys.dup } snapshot.each(&block) end |
.inherited(subclass) ⇒ Object
28 29 30 31 |
# File 'lib/takagi/coap/registries/base.rb', line 28 def self.inherited(subclass) super subclass.instance_variable_set(:@mutex, Mutex.new) end |
.metadata_for(value) ⇒ Hash?
Get registry metadata for a value
134 135 136 |
# File 'lib/takagi/coap/registries/base.rb', line 134 def (value) @mutex.synchronize { registry[value]&.dup } end |
.name_for(value) ⇒ String?
Get human-readable name for a value
76 77 78 |
# File 'lib/takagi/coap/registries/base.rb', line 76 def name_for(value) @mutex.synchronize { registry[value]&.dig(:name) } end |
.register(value, name, symbol = nil, rfc: nil) ⇒ Object
Register a new constant in the registry
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/takagi/coap/registries/base.rb', line 43 def register(value, name, symbol = nil, rfc: nil) @mutex.synchronize do registry[value] = { name: name, symbol: symbol, rfc: rfc } # Create constant if symbol provided if symbol const_name = symbol.to_s.upcase const_set(const_name, value) unless const_defined?(const_name, false) end # Store reverse lookup reverse_registry[name] = value if name reverse_registry[symbol] = value if symbol end Takagi::Hooks.emit( :coap_registry_registered, registry: self, value: value, name: name, symbol: symbol, rfc: rfc ) end |
.registered?(value) ⇒ Boolean
Check if a value is registered
100 101 102 |
# File 'lib/takagi/coap/registries/base.rb', line 100 def registered?(value) @mutex.synchronize { registry.key?(value) } end |
.rfc_for(value) ⇒ String?
Get RFC reference for a value
92 93 94 |
# File 'lib/takagi/coap/registries/base.rb', line 92 def rfc_for(value) @mutex.synchronize { registry[value]&.dig(:rfc) } end |
.value_for(key) ⇒ Integer?
Get numeric value for a name or symbol
84 85 86 |
# File 'lib/takagi/coap/registries/base.rb', line 84 def value_for(key) @mutex.synchronize { reverse_registry[key] } end |
.values ⇒ Array<Integer>
Get all registered values
126 127 128 |
# File 'lib/takagi/coap/registries/base.rb', line 126 def values @mutex.synchronize { registry.keys.dup } end |