class LedgerGen::Transaction

Public Class Methods

new(date_format='%Y/%m/%d') click to toggle source
# File lib/ledger_gen/transaction.rb, line 7
def initialize(date_format='%Y/%m/%d')
  @date_format = T.let(date_format, String)
  @postings = T.let([], T::Array[Posting])
  @comments = T.let([], T::Array[String])

  @date = T.let(nil, T.nilable(T.any(Date, DateTime)))
  @payee = T.let(nil, T.nilable(String))
  @cleared = T.let(false, T::Boolean)
end

Public Instance Methods

cleared!() click to toggle source
# File lib/ledger_gen/transaction.rb, line 28
def cleared!
  @cleared = true
end
comment(comment) click to toggle source
# File lib/ledger_gen/transaction.rb, line 56
def comment(comment)
  @comments << comment
end
date(date) click to toggle source
# File lib/ledger_gen/transaction.rb, line 18
def date(date)
  @date = date
end
payee(payee) click to toggle source
# File lib/ledger_gen/transaction.rb, line 23
def payee(payee)
  @payee = payee
end
posting(account=nil, amount=nil, &blk) click to toggle source
# File lib/ledger_gen/transaction.rb, line 39
def posting(account=nil, amount=nil, &blk)
  post = Posting.new
  @postings << post

  if account
    post.account account
    if amount
      post.amount amount
    end
  end

  if block_given?
    blk.call(post)
  end
end
to_s() click to toggle source
# File lib/ledger_gen/transaction.rb, line 61
def to_s
  lines = ["#{date_string}#{cleared_string} #{@payee}"]

  @comments.each do |comment|
    lines << "    ; #{comment}"
  end

  @postings.each do |post|
    lines << "    " + post.to_s
  end

  lines.join("\n")
end

Private Instance Methods

cleared_string() click to toggle source
# File lib/ledger_gen/transaction.rb, line 83
def cleared_string
  @cleared ? ' *' : ''
end
date_string() click to toggle source
# File lib/ledger_gen/transaction.rb, line 78
def date_string
  T.must(@date).strftime(@date_format)
end