Class: Takagi::CoAP::Registries::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/takagi/coap/registries/base.rb

Direct Known Subclasses

ContentFormat, MessageType, Method, Option, Response, Signaling

Class Method Summary collapse

Class Method Details

.allHash

Get all registered constants

Returns:

  • (Hash)

    Map of value => name



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

Yields:

  • (Integer)

    each registered numeric value

Returns:

  • (Enumerator)

    if no block given



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

Parameters:

  • value (Integer)

    The numeric value

Returns:

  • (Hash, nil)

    Full metadata hash



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

Parameters:

  • value (Integer)

    The numeric value

Returns:

  • (String, nil)

    The name, or nil if not found



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

Examples:

register(69, '2.05 Content', :content, rfc: 'RFC 7252 ยง5.9.1.4')

Parameters:

  • value (Integer)

    Numeric value of the constant

  • name (String)

    Human-readable name

  • symbol (Symbol, nil) (defaults to: nil)

    Optional symbol for constant access

  • rfc (String, nil) (defaults to: nil)

    Optional RFC reference



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

Parameters:

  • value (Integer)

    The value to check

Returns:

  • (Boolean)

    true if 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

Parameters:

  • value (Integer)

    The numeric value

Returns:

  • (String, nil)

    The RFC reference, or nil if not found



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

Parameters:

  • key (String, Symbol)

    The name or symbol

Returns:

  • (Integer, nil)

    The value, or nil if not found



84
85
86
# File 'lib/takagi/coap/registries/base.rb', line 84

def value_for(key)
  @mutex.synchronize { reverse_registry[key] }
end

.valuesArray<Integer>

Get all registered values

Returns:

  • (Array<Integer>)

    Array of all values



126
127
128
# File 'lib/takagi/coap/registries/base.rb', line 126

def values
  @mutex.synchronize { registry.keys.dup }
end