Class: Legion::Extensions::Agentic::Executive::CognitiveDebt::Helpers::DebtItem

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/extensions/agentic/executive/cognitive_debt/helpers/debt_item.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(label:, debt_type:, principal:, domain:) ⇒ DebtItem

Returns a new instance of DebtItem.



15
16
17
18
19
20
21
22
23
24
# File 'lib/legion/extensions/agentic/executive/cognitive_debt/helpers/debt_item.rb', line 15

def initialize(label:, debt_type:, principal:, domain:)
  @id               = SecureRandom.uuid
  @label            = label
  @debt_type        = debt_type.to_sym
  @principal        = principal.clamp(0.0, Float::INFINITY)
  @accrued_interest = 0.0
  @domain           = domain
  @created_at       = Time.now.utc
  @repaid_at        = nil
end

Instance Attribute Details

#accrued_interestObject (readonly)

Returns the value of attribute accrued_interest.



12
13
14
# File 'lib/legion/extensions/agentic/executive/cognitive_debt/helpers/debt_item.rb', line 12

def accrued_interest
  @accrued_interest
end

#created_atObject (readonly)

Returns the value of attribute created_at.



12
13
14
# File 'lib/legion/extensions/agentic/executive/cognitive_debt/helpers/debt_item.rb', line 12

def created_at
  @created_at
end

#debt_typeObject (readonly)

Returns the value of attribute debt_type.



12
13
14
# File 'lib/legion/extensions/agentic/executive/cognitive_debt/helpers/debt_item.rb', line 12

def debt_type
  @debt_type
end

#domainObject (readonly)

Returns the value of attribute domain.



12
13
14
# File 'lib/legion/extensions/agentic/executive/cognitive_debt/helpers/debt_item.rb', line 12

def domain
  @domain
end

#idObject (readonly)

Returns the value of attribute id.



12
13
14
# File 'lib/legion/extensions/agentic/executive/cognitive_debt/helpers/debt_item.rb', line 12

def id
  @id
end

#labelObject (readonly)

Returns the value of attribute label.



12
13
14
# File 'lib/legion/extensions/agentic/executive/cognitive_debt/helpers/debt_item.rb', line 12

def label
  @label
end

#principalObject (readonly)

Returns the value of attribute principal.



12
13
14
# File 'lib/legion/extensions/agentic/executive/cognitive_debt/helpers/debt_item.rb', line 12

def principal
  @principal
end

#repaid_atObject (readonly)

Returns the value of attribute repaid_at.



12
13
14
# File 'lib/legion/extensions/agentic/executive/cognitive_debt/helpers/debt_item.rb', line 12

def repaid_at
  @repaid_at
end

Instance Method Details

#accrue!Object



30
31
32
33
34
# File 'lib/legion/extensions/agentic/executive/cognitive_debt/helpers/debt_item.rb', line 30

def accrue!
  return if repaid?

  @accrued_interest = (@accrued_interest + (Constants::INTEREST_RATE * @principal)).round(10)
end

#age_secondsObject



56
57
58
59
# File 'lib/legion/extensions/agentic/executive/cognitive_debt/helpers/debt_item.rb', line 56

def age_seconds
  reference = @repaid_at || Time.now.utc
  (reference - @created_at).to_f
end

#repaid?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/legion/extensions/agentic/executive/cognitive_debt/helpers/debt_item.rb', line 44

def repaid?
  !@repaid_at.nil?
end

#repay!(amount: Constants::REPAYMENT_RATE) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/legion/extensions/agentic/executive/cognitive_debt/helpers/debt_item.rb', line 36

def repay!(amount: Constants::REPAYMENT_RATE)
  return if repaid?

  @accrued_interest = [0.0, @accrued_interest - amount].max.round(10)
  @principal        = [0.0, @principal - amount].max.round(10)
  @repaid_at        = Time.now.utc if @principal.zero? && @accrued_interest.zero?
end

#severity_labelObject



48
49
50
51
52
53
54
# File 'lib/legion/extensions/agentic/executive/cognitive_debt/helpers/debt_item.rb', line 48

def severity_label
  cost = total_cost
  Constants::SEVERITY_LABELS.each do |range, label|
    return label if range.cover?(cost)
  end
  :critical
end

#to_hObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/legion/extensions/agentic/executive/cognitive_debt/helpers/debt_item.rb', line 61

def to_h
  {
    id:               @id,
    label:            @label,
    debt_type:        @debt_type,
    principal:        @principal,
    accrued_interest: @accrued_interest,
    total_cost:       total_cost,
    domain:           @domain,
    severity:         severity_label,
    age_seconds:      age_seconds.round(2),
    created_at:       @created_at.iso8601,
    repaid_at:        @repaid_at&.iso8601,
    repaid:           repaid?
  }
end

#total_costObject



26
27
28
# File 'lib/legion/extensions/agentic/executive/cognitive_debt/helpers/debt_item.rb', line 26

def total_cost
  (@principal + @accrued_interest).round(10)
end