Class: Rledger::TransactionBase

Inherits:
Object
  • Object
show all
Defined in:
lib/rledger/ledger/transaction.rb

Overview

Transaction base contains the basic elements of any transaction

Direct Known Subclasses

Transaction

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTransactionBase

initialize variables with the correct types



13
14
15
16
17
18
# File 'lib/rledger/ledger/transaction.rb', line 13

def initialize
  @date = Date.new
  @id = ""
  @reconciliation = Reconciliation.new
  @payee = ""
end

Instance Attribute Details

#dateObject

Returns the value of attribute date.



10
11
12
# File 'lib/rledger/ledger/transaction.rb', line 10

def date
  @date
end

#idObject

Returns the value of attribute id.



10
11
12
# File 'lib/rledger/ledger/transaction.rb', line 10

def id
  @id
end

#payeeObject

Returns the value of attribute payee.



10
11
12
# File 'lib/rledger/ledger/transaction.rb', line 10

def payee
  @payee
end

#reconciliationObject

Returns the value of attribute reconciliation.



10
11
12
# File 'lib/rledger/ledger/transaction.rb', line 10

def reconciliation
  @reconciliation
end

Instance Method Details

#parse(s) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 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) ⇒ Object



24
25
26
27
28
# File 'lib/rledger/ledger/transaction.rb', line 24

def to_qif()
  "D#{@date}\n" +
    (@payee != "" ? "P#{@payee}\n" : "") +
    (@id != "" ? "N#{@id}\n" : "")
end

#to_sObject



20
21
22
# 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