Class: SplttyCLI::Commands::AddRunner

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

Overview

Orchestrates the add flow. Extracted from the command so it is unit-testable.

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ AddRunner

Returns a new instance of AddRunner.



48
49
50
51
52
# File 'lib/spltty_cli/commands/add.rb', line 48

def initialize(opts)
  @opts = opts
  @interactive = !opts[:yes]
  @dirty = false
end

Instance Method Details

#runObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/spltty_cli/commands/add.rb', line 54

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

  name, entry = resolve_ledger
  montreal = Ledger.montreal?(entry)

  method_name, method_cfg = resolve_method
  date = resolve_date(method_cfg)

  values = gather(entry, montreal, method_name, method_cfg, date)

  path = Ledger.target_path(@config, name, entry, date)
  ensure_file(name, entry, path, date)

  text = File.read(path)
  parsed = Table.parse(text)
  row = Table.format_row(parsed, values)

  preview(name, path, row)
  if @interactive && !Prompt.confirm("Append this row?", default: true)
    puts "Aborted — nothing written."
    return
  end

  File.write(path, Table.insert_row(text, parsed, row))
  @config.save if @dirty
  puts "Appended to #{path}"
  puts row
end