Module: SplttyCLI::Ledger

Defined in:
lib/spltty_cli/ledger.rb

Overview

Resolve a ledger name to a concrete file path / schema / title, and scaffold the entries file (title + notes pointer + table header) when it is missing.

Constant Summary collapse

HEADERS =
{
  "standard" => [
    "| Date       | Title | Value (R$) | Paid By | Payment Method | Responsible | Source |",
    "|------------|-------|-----------:|---------|----------------|-------------|--------|",
  ],
  "montreal" => [
    "| Date       | Title | Orig. Value | Cur. | Value (R$) | Paid By | Payment Method | Responsible | Source |",
    "|------------|-------|------------:|------|-----------:|---------|----------------|-------------|--------|",
  ],
}.freeze

Class Method Summary collapse

Class Method Details

.config_entry(config, name) ⇒ Object



23
24
25
26
# File 'lib/spltty_cli/ledger.rb', line 23

def config_entry(config, name)
  key = config.ledger_key(name)
  key ? [key, config.ledgers[key]] : [name, nil]
end

.montreal?(entry) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/spltty_cli/ledger.rb', line 32

def montreal?(entry)
  schema(entry) == "montreal"
end

.notes_pointer(name, entry) ⇒ Object



53
54
55
56
# File 'lib/spltty_cli/ledger.rb', line 53

def notes_pointer(name, entry)
  monthly = entry && entry["type"] == "monthly"
  (entry && entry["notes"]) || (monthly ? "notes.md" : "#{name}.notes.md")
end

.scaffold(path, title_line, notes, schema) ⇒ Object

Create the entries file: title, notes-pointer blockquote, table header.



59
60
61
62
63
64
65
66
# File 'lib/spltty_cli/ledger.rb', line 59

def scaffold(path, title_line, notes, schema)
  header, separator = HEADERS.fetch(schema, HEADERS["standard"])
  content = +"# #{title_line}\n\n"
  content << "> Rules & conventions: [`#{notes}`](#{notes})\n\n"
  content << header << "\n" << separator << "\n"
  FileUtils.mkdir_p(File.dirname(path))
  File.write(path, content)
end

.schema(entry) ⇒ Object



28
29
30
# File 'lib/spltty_cli/ledger.rb', line 28

def schema(entry)
  (entry && entry["schema"]) || "standard"
end

.target_path(config, name, entry, date) ⇒ Object

Absolute path of the entries file for name on date.



37
38
39
40
41
42
43
44
45
# File 'lib/spltty_cli/ledger.rb', line 37

def target_path(config, name, entry, date)
  if entry && entry["type"] == "monthly"
    dir = entry["dir"] || name
    File.join(config.accounts_dir, dir, "#{date.strftime('%Y-%m')}.md")
  else
    file = (entry && entry["file"]) || "#{name}.ledger.md"
    File.join(config.accounts_dir, file)
  end
end

.title(name, entry, date) ⇒ Object



47
48
49
50
51
# File 'lib/spltty_cli/ledger.rb', line 47

def title(name, entry, date)
  monthly = entry && entry["type"] == "monthly"
  template = (entry && entry["title"]) || (monthly ? "%{name} — %{month_name} %{year}" : "%{name}")
  format(template, name: name, month_name: date.strftime("%B"), year: date.year)
end