Class: Legion::Extensions::Extinction::Helpers::ProtocolState
- Inherits:
-
Object
- Object
- Legion::Extensions::Extinction::Helpers::ProtocolState
- Defined in:
- lib/legion/extensions/extinction/helpers/protocol_state.rb
Constant Summary collapse
- MAX_HISTORY =
500
Instance Attribute Summary collapse
-
#history ⇒ Object
readonly
Returns the value of attribute history.
Instance Method Summary collapse
- #deescalate(target_level:, authority:, reason:) ⇒ Object
- #escalate(level:, authority:, reason:) ⇒ Object
-
#initialize ⇒ ProtocolState
constructor
A new instance of ProtocolState.
- #to_h ⇒ Object
Constructor Details
#initialize ⇒ ProtocolState
Returns a new instance of ProtocolState.
14 15 16 17 18 19 |
# File 'lib/legion/extensions/extinction/helpers/protocol_state.rb', line 14 def initialize @current_level = 0 @history = [] @store = {} load_from_local end |
Instance Attribute Details
#history ⇒ Object (readonly)
Returns the value of attribute history.
12 13 14 |
# File 'lib/legion/extensions/extinction/helpers/protocol_state.rb', line 12 def history @history end |
Instance Method Details
#deescalate(target_level:, authority:, reason:) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/legion/extensions/extinction/helpers/protocol_state.rb', line 37 def deescalate(target_level:, authority:, reason:) return { success: false, reason: :invalid_level } unless Levels.valid_level?(target_level) return { success: false, reason: :invalid_deescalation } if target_level >= @current_level return { success: false, reason: :not_reversible } unless Levels.reversible?(@current_level) required = Levels.(@current_level) return { success: false, reason: :insufficient_authority, required: required, provided: } if required && != required previous = @current_level @current_level = target_level record_history(action: :deescalate, from: previous, to: target_level, authority: , reason: reason) trim_history save_to_local { success: true, previous_level: previous, current_level: target_level } end |
#escalate(level:, authority:, reason:) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/legion/extensions/extinction/helpers/protocol_state.rb', line 21 def escalate(level:, authority:, reason:) return { success: false, reason: :invalid_level } unless Levels.valid_level?(level) return { success: false, reason: :invalid_escalation } if level <= @current_level required = Levels.(level) return { success: false, reason: :insufficient_authority, required: required, provided: } if required && != required previous = @current_level @current_level = level record_history(action: :escalate, from: previous, to: level, authority: , reason: reason) trim_history save_to_local { success: true, previous_level: previous, current_level: level } end |
#to_h ⇒ Object
56 57 58 59 60 61 62 63 64 |
# File 'lib/legion/extensions/extinction/helpers/protocol_state.rb', line 56 def to_h { current_level: @current_level, level_name: Levels.level_info(@current_level)&.dig(:name), reversible: Levels.reversible?(@current_level), history_count: @history.size, last_change: @history.last } end |