Class: LedgerAccountable::LedgerItem::StateTransition
- Inherits:
-
Object
- Object
- LedgerAccountable::LedgerItem::StateTransition
- Defined in:
- lib/ledger_accountable/ledger_item/state_transition.rb
Overview
Manages ledger entry creation during model lifecycle changes. Determines which entries are needed (addition, deletion, modification) and ensures all entries are created within the same transaction as the model changes.
Simplified control flow: Am I already in a ledger?
YES
am I still in that ledger?
YES
has my amount changed?
YES
=> create entry with the net change
NO
=> no update unless overridden
NO
=> create a deletion ledger entry
do I have a new ledger?
YES
=> create a new ledger entry
NO
=> create a new ledger entry
Class Method Summary collapse
Instance Method Summary collapse
- #execute(&block) ⇒ Object
-
#initialize(item) ⇒ StateTransition
constructor
A new instance of StateTransition.
Constructor Details
#initialize(item) ⇒ StateTransition
Returns a new instance of StateTransition.
31 32 33 |
# File 'lib/ledger_accountable/ledger_item/state_transition.rb', line 31 def initialize(item) @item = item end |
Class Method Details
.execute(item, &block) ⇒ Object
27 28 29 |
# File 'lib/ledger_accountable/ledger_item/state_transition.rb', line 27 def self.execute(item, &block) new(item).execute(&block) end |
Instance Method Details
#execute(&block) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/ledger_accountable/ledger_item/state_transition.rb', line 35 def execute(&block) return yield unless should_record_entry? # Compute all values that depend on object state BEFORE the save ledger_entries_to_create = determine_required_entries = @item. ActiveRecord::Base.transaction do with_required_save(&block) # Create all necessary entries after the save ledger_entries_to_create.each do |entry_params| record_entry( owner: entry_params[:owner], amount: entry_params[:amount], entry_type: entry_params[:entry_type], metadata: ) end end end |