Class: SplttyCLI::Commands::SettleRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/spltty_cli/commands/settle.rb

Overview

Orchestrates the settle flow. Extracted from the command so it is unit-testable (same pattern as AddRunner).

Constant Summary collapse

OFFSET_METHOD =
"offset"

Instance Method Summary collapse

Constructor Details

#initialize(requested, opts) ⇒ SettleRunner

Returns a new instance of SettleRunner.



49
50
51
52
53
# File 'lib/spltty_cli/commands/settle.rb', line 49

def initialize(requested, opts)
  @requested = Array(requested)
  @opts = opts
  @interactive = !opts[:yes]
end

Instance Method Details

#runObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/spltty_cli/commands/settle.rb', line 55

def run
  @config = SplttyCLI.load_config(@opts)
  Discovery.sync(@config)
  NotesSync.run(@config)
  @config.save

  names = resolve_scope
  transfers = names.to_h { |n| [n, ledger_transfer(n)] }

  plan = @requested.length == 1 ? cash_plan(names.first, transfers) : offset_plan(transfers)
  return if plan.nil? || plan.empty?

  plan.each { |row| assert_person!(row[:debtor], row[:ledger]) && assert_person!(row[:creditor], row[:ledger]) }

  preview(plan)
  if @interactive && !Prompt.confirm("Record #{plan.length == 1 ? 'this settlement row' : "these #{plan.length} settlement rows"}?", default: true)
    puts "Aborted — nothing written."
    return
  end

  plan.each { |row| write_row(row) }
  puts "Recorded #{plan.length} settlement row#{plan.length == 1 ? '' : 's'}."
end