/ledgerjournal

ledgerjournal is a Ruby gem to read and write ledger accounting files.

Primary LanguageRubyMIT LicenseMIT

ledgerjournal

Gem Version Build Status Yard Docs

ledgerjournal is a Ruby gem to read and write ledger accounting files. For parsing, it uses the ledger xml command. For outputting, it formats the ledger data as String in Ruby. The ledger binary needs to be installed to parse and pretty-print.

Usage

Parsing a ledger journal file:

journal = Ledger::Journal.new(path: "example_journal.dat")
journal.transactions.each do |tx|
  puts tx.date, tx.payee
end

Creating a ledger file from scratch:

journal = Ledger::Journal.new()

journal.transactions << Ledger::Transaction.new(
  date: Date.new(2020, 1, 2),
  payee: 'Example Payee',
  metadata: { "Foo" => "Bar", "Description" => "Example Transaction" },
  postings: [
    Ledger::Posting.new(account: "Expenses:Unknown", currency: "EUR", amount: BigDecimal('1234.56'), metadata: { "Foo" => "Bar", "Description" => "Example Posting" }),
    Ledger::Posting.new(account: "Assets:Checking", currency: "EUR", amount: BigDecimal('-1234.56'))
  ]
)

puts(journal.to_s)

Appending to a ledger journal:

journal = Ledger::Journal.new(path: "example_journal.dat")
journal.transactions << Ledger::Transaction.new(
  date: Date.new(2020, 1, 2),
  payee: 'Example Payee',
  postings: [
    Ledger::Posting.new(account: "Expenses:Unknown", currency: "EUR", amount: BigDecimal('1234.56')),
    Ledger::Posting.new(account: "Assets:Checking", currency: "EUR", amount: BigDecimal('-1234.56'))
  ]
)
journal.save!

Running ledger commands:

puts Ledger.defaults.run('--version')

Locale-specific settings

By default ledgerjournal expects the date format '%Y/%m/%d' and amounts with decimal point (1234.56). This is configurable:

Ledger.defaults = Options.new(date_format: '%d.%m.%Y', decimal_comma: true)

or:

Ledger.defaults = Ledger::Options.for_locale(:de)

Installation

Add this line to your application's Gemfile:

gem 'ledgerjournal'

Or install it yourself as:

$ gem install ledgerjournal

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/ralfebert/ledgerjournal.

License

ledgerjournal is released under the MIT License. See LICENSE.md.