Class: Legion::Extensions::Extinction::Helpers::ProtocolState

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/extensions/extinction/helpers/protocol_state.rb

Constant Summary collapse

MAX_HISTORY =
500

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeProtocolState

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

#historyObject (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.required_authority(@current_level)
  return { success: false, reason: :insufficient_authority, required: required, provided: authority } if required && authority != required

  previous = @current_level
  @current_level = target_level
  record_history(action: :deescalate, from: previous, to: target_level, authority: 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.required_authority(level)
  return { success: false, reason: :insufficient_authority, required: required, provided: authority } if required && authority != required

  previous = @current_level
  @current_level = level
  record_history(action: :escalate, from: previous, to: level, authority: authority, reason: reason)
  trim_history
  save_to_local

  { success: true, previous_level: previous, current_level: level }
end

#to_hObject



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