lib/ledger_gen/transaction.rb in ledger_gen-0.2.0 vs lib/ledger_gen/transaction.rb in ledger_gen-1.0.0

- old
+ new

@@ -1,43 +1,65 @@ +# typed: strict module LedgerGen class Transaction - def initialize(date_format=nil) - @date_format = date_format || '%Y/%m/%d' - @postings = [] - @comments = [] + extend T::Sig + + sig {params(date_format: String).void} + 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 + sig {params(date: T.any(Date, DateTime)).returns(T.any(Date, DateTime))} def date(date) @date = date end + sig {params(payee: String).returns(String)} def payee(payee) @payee = payee end + sig {void} def cleared! @cleared = true end - def posting(*args) + sig do + params( + account: T.nilable(String), + amount: T.nilable(Float), + blk: T.nilable(T.proc.params(arg0: Posting).void) + ).void + end + def posting(account=nil, amount=nil, &blk) post = Posting.new @postings << post - if args.length > 0 - post.account args.shift - if args.length > 0 - post.amount args[0] + if account + post.account account + if amount + post.amount amount end - else - yield post end + + if block_given? + blk.call(post) + end end + sig {params(comment: String).void} def comment(comment) @comments << comment end + sig {returns(String)} def to_s lines = ["#{date_string}#{cleared_string} #{@payee}"] @comments.each do |comment| lines << " ; #{comment}" @@ -50,13 +72,15 @@ lines.join("\n") end private + sig {returns(String)} def date_string - @date.strftime(@date_format) + T.must(@date).strftime(@date_format) end + sig {returns(String)} def cleared_string @cleared ? ' *' : '' end end end