Module: Takagi::EventBus::Scope

Defined in:
lib/takagi/event_bus/scope.rb

Overview

Message scope levels for clustering support

LOCAL: Message stays on this instance only CLUSTER: Message delivered to all instances in cluster GLOBAL: Message delivered to cluster + external CoAP subscribers

Examples:

Local event (default)

EventBus.publish('system.startup', data, scope: :local)

Cluster-wide event

EventBus.publish('cache.invalidate', { key: 'user:123' }, scope: :cluster)

Global event (cluster + external)

EventBus.publish('sensor.temperature', { value: 25.5 }, scope: :global)

Constant Summary collapse

LOCAL =

This instance only - never leaves process

:local
CLUSTER =

All instances in cluster - distributed via CoAP OBSERVE

:cluster
GLOBAL =

Cluster + external CoAP subscribers - published to /.well-known/core

:global
DEFAULT =

Default scope if not specified

LOCAL
ALL =

All valid scope values

[LOCAL, CLUSTER, GLOBAL].freeze

Class Method Summary collapse

Class Method Details

.distributed?(scope) ⇒ Boolean

Check if scope requires cluster distribution

Parameters:

  • scope (Symbol)

    Scope to check

Returns:

  • (Boolean)


55
56
57
# File 'lib/takagi/event_bus/scope.rb', line 55

def self.distributed?(scope)
  scope == CLUSTER || scope == GLOBAL
end

.external?(scope) ⇒ Boolean

Check if scope allows external CoAP subscribers

Parameters:

  • scope (Symbol)

    Scope to check

Returns:

  • (Boolean)


62
63
64
# File 'lib/takagi/event_bus/scope.rb', line 62

def self.external?(scope)
  scope == GLOBAL
end

.local_only?(scope) ⇒ Boolean

Check if scope is local-only

Parameters:

  • scope (Symbol)

    Scope to check

Returns:

  • (Boolean)


69
70
71
# File 'lib/takagi/event_bus/scope.rb', line 69

def self.local_only?(scope)
  scope == LOCAL
end

.normalize(scope) ⇒ Symbol

Normalize scope value

Parameters:

  • scope (Symbol, String, nil)

    Scope to normalize

Returns:

  • (Symbol)

    Normalized scope (defaults to LOCAL)



45
46
47
48
49
50
# File 'lib/takagi/event_bus/scope.rb', line 45

def self.normalize(scope)
  return DEFAULT if scope.nil?

  scope_sym = scope.to_sym
  valid?(scope_sym) ? scope_sym : DEFAULT
end

.valid?(scope) ⇒ Boolean

Check if scope is valid

Parameters:

  • scope (Symbol)

    Scope to check

Returns:

  • (Boolean)


38
39
40
# File 'lib/takagi/event_bus/scope.rb', line 38

def self.valid?(scope)
  ALL.include?(scope)
end