Sha256: b4d92aa581a6f94b4dac1b64b0df14e32d5ba820e7f53da2ddf48178255dcdc3

Contents?: true

Size: 1.01 KB

Versions: 2

Compression:

Stored size: 1.01 KB

Contents

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

    def date(date)
      @date = date
    end

    def payee(payee)
      @payee = payee
    end

    def cleared!
      @cleared = true
    end

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

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

    def comment(comment)
      @comments << comment
    end

    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

    private

    def date_string
      @date.strftime(@date_format)
    end

    def cleared_string
      @cleared ? ' *' : ''
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ledger_gen-0.2.0 lib/ledger_gen/transaction.rb
ledger_gen-0.1.2 lib/ledger_gen/transaction.rb