class Rledger::Transaction

A transaction models a multiple entry transactions It should contain a minimum of two entries

Attributes

posts[RW]

Public Class Methods

new() click to toggle source
Calls superclass method Rledger::TransactionBase::new
# File lib/rledger/ledger/transaction.rb, line 76
def initialize
  super
  @posts = []
end
read(filename) click to toggle source
# File lib/rledger/ledger/transaction.rb, line 81
def self.read(filename)
  transactions = []
  file_string = IO.read(filename)
  array = file_string.split(/\n([\t ]*\n)+/)  # ignore empty lines (possibly with blanks)
  array.each do |record|
    if record =~ /^[0-9]/
      t = Transaction.new
      t.parse(record)
      transactions << t
    end
  end
  transactions
end

Public Instance Methods

<=>(other) click to toggle source
# File lib/rledger/ledger/transaction.rb, line 95
def <=> other
  date <=> other.date
end
contains?(voice) click to toggle source
# File lib/rledger/ledger/transaction.rb, line 134
def contains? voice
  posts_with(voice).size > 0
end
parse(s) click to toggle source
Calls superclass method Rledger::TransactionBase#parse
# File lib/rledger/ledger/transaction.rb, line 99
def parse(s)
  lines = s.split("\n")
  super(lines[0]) # call super.parse!
  lines[1..-1].each do |line|
    p = Post.new
    p.parse(line)
    @posts << p
  end

  # fix the derived amount in the derived entry (if there is one)
  # TODO Raise a lot of errors:
  # - the other entries are not in a single currency
  # - there is more than a derived entry
  p = @posts.select { |x| x.derived? }
  if p[0] != nil then
    p[0].amount = total_no_derived.multiply!(BigDecimal.new(-1))
  end
end
posts_with(voice) click to toggle source
# File lib/rledger/ledger/transaction.rb, line 126
def posts_with voice
  @posts.select { |e| e.voice == voice }
end
posts_without(voice) click to toggle source
# File lib/rledger/ledger/transaction.rb, line 130
def posts_without voice
  @posts.select { |e| e.voice != voice }
end
to_qif(account) click to toggle source
Calls superclass method Rledger::TransactionBase#to_qif
# File lib/rledger/ledger/transaction.rb, line 144
def to_qif(account)
  accumulator = super.to_s   
  @posts.each { |post|
    accumulator <<  
    "L[#{post.item}]\n" +
    "$#{post.amount}\n" +
    "M#{post.comment}\n"
  }
  accumulator + "^"
end
to_s() click to toggle source
Calls superclass method Rledger::TransactionBase#to_s
# File lib/rledger/ledger/transaction.rb, line 138
def to_s
  accumulator = ""
  @posts.each { |post| accumulator << post.to_s }
  super.to_s + accumulator + "\n"
end
total_no_derived() click to toggle source
# File lib/rledger/ledger/transaction.rb, line 118
def total_no_derived
  a = Amount.new
  @posts.each do |e|
    a.add!(e.amount) if not e.derived?
  end
  a
end