Class: LedgerGen::Transaction

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/ledger_gen/transaction.rb

Instance Method Summary collapse

Constructor Details

#initialize(date_format = '%Y/%m/%d') ⇒ Transaction

Returns a new instance of Transaction.



7
8
9
10
11
12
13
14
15
# File 'lib/ledger_gen/transaction.rb', line 7

def initialize(date_format='%Y/%m/%d')
  @date_format = T.let(date_format, String)
  @postings = T.let([], T::Array[Posting])
  @comments = T.let([], T::Array[String])

  @date = T.let(nil, T.nilable(T.any(Date, DateTime)))
  @payee = T.let(nil, T.nilable(String))
  @cleared = T.let(false, T::Boolean)
end

Instance Method Details

#cleared!Object



28
29
30
# File 'lib/ledger_gen/transaction.rb', line 28

def cleared!
  @cleared = true
end

#comment(comment) ⇒ Object



56
57
58
# File 'lib/ledger_gen/transaction.rb', line 56

def comment(comment)
  @comments << comment
end

#date(date) ⇒ Object



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

def date(date)
  @date = date
end

#payee(payee) ⇒ Object



23
24
25
# File 'lib/ledger_gen/transaction.rb', line 23

def payee(payee)
  @payee = payee
end

#posting(account = nil, amount = nil, &blk) ⇒ Object



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

def posting(=nil, amount=nil, &blk)
  post = Posting.new
  @postings << post

  if 
    post. 
    if amount
      post.amount amount
    end
  end

  if block_given?
    blk.call(post)
  end
end

#to_sObject



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/ledger_gen/transaction.rb', line 61

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