Class: VSM::Capsule
- Inherits:
-
Object
- Object
- VSM::Capsule
- Defined in:
- lib/vsm/capsule.rb
Instance Attribute Summary collapse
-
#bus ⇒ Object
readonly
Returns the value of attribute bus.
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#homeostat ⇒ Object
readonly
Returns the value of attribute homeostat.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#roles ⇒ Object
readonly
Returns the value of attribute roles.
Instance Method Summary collapse
- #dispatch(message) ⇒ Object
-
#initialize(name:, roles:, children: {}) ⇒ Capsule
constructor
A new instance of Capsule.
- #route(message) ⇒ Object
- #run ⇒ Object
Constructor Details
#initialize(name:, roles:, children: {}) ⇒ Capsule
Returns a new instance of Capsule.
7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/vsm/capsule.rb', line 7 def initialize(name:, roles:, children: {}) @name = name.to_sym @roles = roles @children = children ctx = { operations_children: children.transform_keys(&:to_s) } @bus = AsyncChannel.new(context: ctx) @homeostat = Homeostat.new # Inject bus into children that accept it, to enable richer observability @children.each_value { |c| c.bus = @bus if c.respond_to?(:bus=) } wire_observers! end |
Instance Attribute Details
#bus ⇒ Object (readonly)
Returns the value of attribute bus.
5 6 7 |
# File 'lib/vsm/capsule.rb', line 5 def bus @bus end |
#children ⇒ Object (readonly)
Returns the value of attribute children.
5 6 7 |
# File 'lib/vsm/capsule.rb', line 5 def children @children end |
#homeostat ⇒ Object (readonly)
Returns the value of attribute homeostat.
5 6 7 |
# File 'lib/vsm/capsule.rb', line 5 def homeostat @homeostat end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/vsm/capsule.rb', line 5 def name @name end |
#roles ⇒ Object (readonly)
Returns the value of attribute roles.
5 6 7 |
# File 'lib/vsm/capsule.rb', line 5 def roles @roles end |
Instance Method Details
#dispatch(message) ⇒ Object
29 30 31 32 |
# File 'lib/vsm/capsule.rb', line 29 def dispatch() return roles[:identity].alert() if homeostat.alarm?() roles[:governance].enforce() { route(_1) } end |
#route(message) ⇒ Object
34 35 36 37 38 |
# File 'lib/vsm/capsule.rb', line 34 def route() roles[:operations].handle(, bus: @bus, children: @children) || roles[:intelligence].handle(, bus: @bus) || roles[:identity].handle(, bus: @bus) end |
#run ⇒ Object
19 20 21 22 23 24 25 26 27 |
# File 'lib/vsm/capsule.rb', line 19 def run Async do loop do = @bus.pop roles[:coordination].stage() roles[:coordination].drain(@bus) { |m| dispatch(m) } end end end |