Class: Interfacets::Shared::Entities::Bus

Inherits:
Object
  • Object
show all
Defined in:
lib/interfacets/shared/entities/bus.rb

Constant Summary collapse

InvalidAction =
Class.new(StandardError)
InvalidReceiver =
Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entity:) ⇒ Bus

Returns a new instance of Bus.



21
22
23
24
# File 'lib/interfacets/shared/entities/bus.rb', line 21

def initialize(entity:)
  @manifest = entity.class.manifest
  @entity = entity
end

Instance Attribute Details

#entity ⇒ Object (readonly)

} }



20
21
22
# File 'lib/interfacets/shared/entities/bus.rb', line 20

def entity
  @entity
end

#manifest ⇒ Object (readonly)

} }



20
21
22
# File 'lib/interfacets/shared/entities/bus.rb', line 20

def manifest
  @manifest
end

Instance Method Details

#handle(event:) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/interfacets/shared/entities/bus.rb', line 41

def handle(event:)
  action = event.fetch("action")

  assert_valid_receiver(to: event.fetch("to"))
  assert_valid_action(
    action:,
    to: entity.role,
    nesting: event.fetch("nesting"),
  )

  attributes = event.fetch("payload").fetch("attributes", {})

  merge(entity:, manifest:, attributes:, action:)

  target_entity = entity.entity_at(event.fetch("nesting"))

  target_entity
    &.class
    &.actions
    &.fetch(event.fetch("action"))
    &.dispatch(target_entity)
end

#serialize(to:, action:, nesting:) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/interfacets/shared/entities/bus.rb', line 26

def serialize(to:, action:, nesting:)
  assert_valid_action(action:, to:, nesting:)

  {
    id: SecureRandom.uuid,
    from: entity.role,
    to: to,
    nesting:,
    action:,
    payload: {
      attributes: serialize_attributes(manifest:, entity:, to:)
    }
  }
end