Class: Takagi::EventBus::AddressPrefix

Inherits:
Object
  • Object
show all
Defined in:
lib/takagi/event_bus/address_prefix.rb

Overview

EventBus Address Prefix Registry

Defines which event address prefixes are distributed via CoAP and which remain local-only.

Extensible registry allows plugins to register custom prefixes without modifying core code.

Examples:

Using predefined prefixes

AddressPrefix.distributed?('sensor.temperature.room1')  # => true
AddressPrefix.distributed?('system.startup')            # => false

Registering a custom distributed prefix

AddressPrefix.register_distributed('custom.', 'Custom Events')

Registering a custom local prefix

AddressPrefix.register_local('internal.', 'Internal Events')

Class Method Summary collapse

Class Method Details

.allHash

Get all registered prefixes

Returns:

  • (Hash)

    Combined map of all prefixes



90
91
92
93
94
# File 'lib/takagi/event_bus/address_prefix.rb', line 90

def all
  @mutex.synchronize do
    @distributed.merge(@local)
  end
end

.clear!Object

Clear all registrations (useful for testing)



115
116
117
118
119
120
# File 'lib/takagi/event_bus/address_prefix.rb', line 115

def clear!
  @mutex.synchronize do
    @distributed.clear
    @local.clear
  end
end

.distributed?(address) ⇒ Boolean

Check if address matches a distributed prefix

Parameters:

  • address (String)

    Event address

Returns:

  • (Boolean)

    true if distributed



59
60
61
62
63
64
65
# File 'lib/takagi/event_bus/address_prefix.rb', line 59

def distributed?(address)
  return false if local?(address)

  @mutex.synchronize do
    @distributed.keys.any? { |prefix| address.start_with?(prefix) }
  end
end

.distributed_prefixesHash

Get all distributed prefixes

Returns:

  • (Hash)

    Map of prefix => metadata



78
79
80
# File 'lib/takagi/event_bus/address_prefix.rb', line 78

def distributed_prefixes
  @mutex.synchronize { @distributed.dup }
end

.initialize_defaults!Object

Initialize default prefixes



123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/takagi/event_bus/address_prefix.rb', line 123

def initialize_defaults!
  # Distributed prefixes (published via CoAP Observe)
  register_distributed('sensor.', 'Sensor Events', rfc: 'RFC 7641')
  register_distributed('alert.', 'Alert Events', rfc: 'RFC 7641')
  register_distributed('cluster.', 'Cluster Events')
  register_distributed('reactor.', 'Reactor Events')
  register_distributed('event.', 'General Events')

  # Local-only prefixes (stay in-process)
  register_local('system.', 'System Events')
  register_local('coap.', 'CoAP Protocol Events')
  register_local('plugin.', 'Plugin Lifecycle Events')
end

.local?(address) ⇒ Boolean

Check if address matches a local-only prefix

Parameters:

  • address (String)

    Event address

Returns:

  • (Boolean)

    true if local-only



70
71
72
73
74
# File 'lib/takagi/event_bus/address_prefix.rb', line 70

def local?(address)
  @mutex.synchronize do
    @local.keys.any? { |prefix| address.start_with?(prefix) }
  end
end

.local_prefixesHash

Get all local prefixes

Returns:

  • (Hash)

    Map of prefix => metadata



84
85
86
# File 'lib/takagi/event_bus/address_prefix.rb', line 84

def local_prefixes
  @mutex.synchronize { @local.dup }
end

.metadata_for(prefix) ⇒ Hash?

Get metadata for a specific prefix

Parameters:

  • prefix (String)

    The prefix to look up

Returns:

  • (Hash, nil)

    Prefix metadata



99
100
101
102
103
# File 'lib/takagi/event_bus/address_prefix.rb', line 99

def (prefix)
  @mutex.synchronize do
    @distributed[prefix] || @local[prefix]
  end
end

.register_distributed(prefix, description, rfc: nil) ⇒ Object

Register a distributed prefix (events are published via CoAP)

Parameters:

  • prefix (String)

    Address prefix (e.g., ‘sensor.’)

  • description (String)

    Human-readable description

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

    Optional RFC reference



32
33
34
35
36
37
38
39
40
# File 'lib/takagi/event_bus/address_prefix.rb', line 32

def register_distributed(prefix, description, rfc: nil)
  @mutex.synchronize do
    @distributed[prefix] = {
      description: description,
      rfc: rfc,
      type: :distributed
    }
  end
end

.register_local(prefix, description, rfc: nil) ⇒ Object

Register a local-only prefix (events stay in-process)

Parameters:

  • prefix (String)

    Address prefix (e.g., ‘system.’)

  • description (String)

    Human-readable description

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

    Optional RFC reference



46
47
48
49
50
51
52
53
54
# File 'lib/takagi/event_bus/address_prefix.rb', line 46

def register_local(prefix, description, rfc: nil)
  @mutex.synchronize do
    @local[prefix] = {
      description: description,
      rfc: rfc,
      type: :local
    }
  end
end

.unregister(prefix) ⇒ Boolean

Unregister a prefix (useful for testing/plugins)

Parameters:

  • prefix (String)

    The prefix to remove

Returns:

  • (Boolean)

    true if was registered



108
109
110
111
112
# File 'lib/takagi/event_bus/address_prefix.rb', line 108

def unregister(prefix)
  @mutex.synchronize do
    @distributed.delete(prefix) || @local.delete(prefix)
  end
end