class Rledger::TransactionBase

Transaction base contains the basic elements of any transaction

Attributes

date[RW]
id[RW]
payee[RW]
reconciliation[RW]

Public Class Methods

new() click to toggle source

initialize variables with the correct types

# File lib/rledger/ledger/transaction.rb, line 13
def initialize
  @date = Date.new
  @id = ""
  @reconciliation = Reconciliation.new
  @payee = ""
end

Public Instance Methods

parse(s) click to toggle source
# File lib/rledger/ledger/transaction.rb, line 30
def parse(s)
  # regular expressions of various components of a transaction base
  date = "([0-9][0-9]/[0-9][0-9]/[0-9][0-9][0-9][0-9])"
  ob = "[\t ]*"
  rec = "([!?*]|)"
  id = "(\\([^)]+\\)|)"
  payee = "(.*)"

  match = Regexp.new(date + ob + rec + ob + id + ob + payee).match(s)
  if match
    @date = s_to_date(match[1])  # TODO: replace with Chronic o Date.parse
    @reconciliation.parse(match[2])
    @id = match[3] == "" ? "" : match[3][1..-2] # so that id is always a string
    @payee = match[4]
    true
  else
    false
  end
end
to_qif(account) click to toggle source
# File lib/rledger/ledger/transaction.rb, line 24
def to_qif(account)
  "D#{@date}\n" +
    (@payee != "" ? "P#{@payee}\n" : "") +
    (@id != "" ? "N#{@id}\n" : "")
end
to_s() click to toggle source
# File lib/rledger/ledger/transaction.rb, line 20
def to_s
  "#{@date.strftime("%d/%m/%Y")}#{rec_to_s}#{id_to_s}#{payee_to_s}\n"
end

Private Instance Methods

id_to_s() click to toggle source
# File lib/rledger/ledger/transaction.rb, line 52
def id_to_s
  @id == "" ? "" : "\t(#{@id})"
end
payee_to_s() click to toggle source
# File lib/rledger/ledger/transaction.rb, line 60
def payee_to_s
  @payee == "" ? "" : "\t#{@payee}"
end
rec_to_s() click to toggle source
# File lib/rledger/ledger/transaction.rb, line 56
def rec_to_s
  @reconciliation.unknown? ? "" : "\t#{@reconciliation.to_s}"
end
s_to_date(s) click to toggle source
# File lib/rledger/ledger/transaction.rb, line 64
def s_to_date(s)
  day, month, year = s.split("/");
  Date.civil(year.to_i, month.to_i, day.to_i)
end