Class: LedgerGen::Transaction

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

Instance Method Summary collapse

Constructor Details

#initialize(date_format = nil) ⇒ Transaction

Returns a new instance of Transaction.



3
4
5
6
7
# File 'lib/ledger_gen/transaction.rb', line 3

def initialize(date_format=nil)
  @date_format = date_format || '%Y/%m/%d'
  @postings = []
  @comments = []
end

Instance Method Details

#cleared!Object



17
18
19
# File 'lib/ledger_gen/transaction.rb', line 17

def cleared!
  @cleared = true
end

#comment(comment) ⇒ Object



35
36
37
# File 'lib/ledger_gen/transaction.rb', line 35

def comment(comment)
  @comments << comment
end

#date(date) ⇒ Object



9
10
11
# File 'lib/ledger_gen/transaction.rb', line 9

def date(date)
  @date = date
end

#payee(payee) ⇒ Object



13
14
15
# File 'lib/ledger_gen/transaction.rb', line 13

def payee(payee)
  @payee = payee
end

#posting(*args) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ledger_gen/transaction.rb', line 21

def posting(*args)
  post = Posting.new
  @postings << post

  if args.length > 0
    post. args.shift
    if args.length > 0
      post.amount args[0]
    end
  else
    yield post
  end
end

#to_sObject



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ledger_gen/transaction.rb', line 39

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