Class: Rledger::Transaction

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

Overview

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

Instance Attribute Summary collapse

Attributes inherited from TransactionBase

#date, #id, #payee, #reconciliation

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTransaction

Returns a new instance of Transaction.



76
77
78
79
# File 'lib/rledger/ledger/transaction.rb', line 76

def initialize
  super
  @posts = []
end

Instance Attribute Details

#postsObject

Returns the value of attribute posts.



74
75
76
# File 'lib/rledger/ledger/transaction.rb', line 74

def posts
  @posts
end

Class Method Details

.read(filename) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
# 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

Instance Method Details

#contains?(voice) ⇒ Boolean

Returns:

  • (Boolean)


130
131
132
# File 'lib/rledger/ledger/transaction.rb', line 130

def contains? voice
  posts_with(voice).size > 0
end

#parse(s) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/rledger/ledger/transaction.rb', line 95

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) ⇒ Object



122
123
124
# File 'lib/rledger/ledger/transaction.rb', line 122

def posts_with voice
  @posts.select { |e| e.voice == voice }
end

#posts_without(voice) ⇒ Object



126
127
128
# File 'lib/rledger/ledger/transaction.rb', line 126

def posts_without voice
  @posts.select { |e| e.voice != voice }
end

#to_qif(account) ⇒ Object



140
141
142
143
144
145
146
147
148
149
# File 'lib/rledger/ledger/transaction.rb', line 140

def to_qif()
  accumulator = super.to_s   
  @posts.each { |post|
    accumulator <<  
    "L[#{post.item}]\n" +
    "$#{post.amount}\n" +
    "M#{post.comment}\n"
  }
  accumulator + "^"
end

#to_sObject



134
135
136
137
138
# File 'lib/rledger/ledger/transaction.rb', line 134

def to_s
  accumulator = ""
  @posts.each { |post| accumulator << post.to_s }
  super.to_s + accumulator + "\n"
end

#total_no_derivedObject



114
115
116
117
118
119
120
# File 'lib/rledger/ledger/transaction.rb', line 114

def total_no_derived
  a = Amount.new
  @posts.each do |e|
    a.add!(e.amount) if not e.derived?
  end
  a
end