Class: LedgerGen::Journal

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeJournal

Returns a new instance of Journal.



20
21
22
23
# File 'lib/ledger_gen/journal.rb', line 20

def initialize
  @transactions = T.let([], T::Array[Transaction])
  @date_format = T.let('%Y/%m/%d', String)
end

Instance Attribute Details

#date_formatObject

Returns the value of attribute date_format.



8
9
10
# File 'lib/ledger_gen/journal.rb', line 8

def date_format
  @date_format
end

Class Method Details

.build(&blk) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/ledger_gen/journal.rb', line 11

def self.build(&blk)
  journal = new
  
  blk.call(journal)

  return journal
end

Instance Method Details

#pretty_print(ledger_options = '') ⇒ Object



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

def pretty_print(ledger_options='')
  if ledger_options == ''
    ledger_options = %Q{-y "#{date_format}" --sort=date}
  end

  IO.popen("ledger #{ledger_options} -f - print", 'r+') do |io|
    io.write to_s
    io.close_write
    io.read
  end
end

#to_sObject



34
35
36
# File 'lib/ledger_gen/journal.rb', line 34

def to_s
  @transactions.map(&:to_s).join("\n\n") + "\n"
end

#transaction {|txn| ... } ⇒ Object

Yields:

  • (txn)


26
27
28
29
30
31
# File 'lib/ledger_gen/journal.rb', line 26

def transaction(&blk)
  txn = Transaction.new(date_format)
  @transactions << txn

  yield txn
end