Compare commits
13 Commits
Author | SHA1 | Date | |
---|---|---|---|
9bfb6e8578 | |||
aa3b65d1e2 | |||
326dbd3fcd | |||
4e882e60dc | |||
9ee49d949d | |||
4cf3949005 | |||
36313ddcc4 | |||
d72de51977 | |||
6ff11c012f | |||
c14ac56a32 | |||
309e8a9c29 | |||
c3f3b80bf3 | |||
0c07735cea |
17
README.md
17
README.md
@ -18,8 +18,25 @@ Or install it yourself as:
|
|||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
|
require "gnucash"
|
||||||
|
|
||||||
book = Gnucash.open("MyBook.gnucash")
|
book = Gnucash.open("MyBook.gnucash")
|
||||||
|
|
||||||
|
book.accounts.each do |account|
|
||||||
|
puts "#{account.full_name}: #{account.final_balance}"
|
||||||
|
end
|
||||||
|
|
||||||
|
act = book.find_account_by_full_name("Assets::Checking")
|
||||||
|
balance = Gnucash::Value.zero
|
||||||
|
act.transactions.each do |txn|
|
||||||
|
balance += txn.value
|
||||||
|
$stdout.puts(sprintf("%s %8s %8s %s",
|
||||||
|
txn.date,
|
||||||
|
txn.value,
|
||||||
|
balance,
|
||||||
|
txn.description))
|
||||||
|
end
|
||||||
|
|
||||||
## Contributing
|
## Contributing
|
||||||
|
|
||||||
1. Fork it
|
1. Fork it
|
||||||
|
@ -10,7 +10,8 @@ Gem::Specification.new do |gem|
|
|||||||
gem.email = ["jholtrop@gmail.com"]
|
gem.email = ["jholtrop@gmail.com"]
|
||||||
gem.description = %q{Ruby library for extracting data from GnuCash data files}
|
gem.description = %q{Ruby library for extracting data from GnuCash data files}
|
||||||
gem.summary = %q{Extract data from GnuCash data files}
|
gem.summary = %q{Extract data from GnuCash data files}
|
||||||
gem.homepage = ""
|
gem.homepage = "https://github.com/holtrop/ruby-gnucash"
|
||||||
|
gem.license = "MIT"
|
||||||
|
|
||||||
gem.files = `git ls-files`.split($/)
|
gem.files = `git ls-files`.split($/)
|
||||||
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
||||||
|
@ -2,17 +2,23 @@ module Gnucash
|
|||||||
# Represent a GnuCash account object
|
# Represent a GnuCash account object
|
||||||
class Account
|
class Account
|
||||||
# _String_: The name of the account (unqualified)
|
# _String_: The name of the account (unqualified)
|
||||||
attr_accessor :name
|
attr_reader :name
|
||||||
|
|
||||||
|
# _String_: The account description
|
||||||
|
attr_reader :description
|
||||||
|
|
||||||
# _String_: The account type (such as "EXPENSE")
|
# _String_: The account type (such as "EXPENSE")
|
||||||
attr_accessor :type
|
attr_reader :type
|
||||||
|
|
||||||
# _String_: The GUID of the account
|
# _String_: The GUID of the account
|
||||||
attr_accessor :id
|
attr_reader :id
|
||||||
|
|
||||||
# _Array_: List of _AccountTransaction_ transactions associated with this
|
# _Array_: List of _AccountTransaction_ transactions associated with this
|
||||||
# account.
|
# account.
|
||||||
attr_accessor :transactions
|
attr_reader :transactions
|
||||||
|
|
||||||
|
# Boolean: whether the account is a placeholder or not
|
||||||
|
attr_reader :placeholder
|
||||||
|
|
||||||
# Create an Account object.
|
# Create an Account object.
|
||||||
# === Arguments
|
# === Arguments
|
||||||
@ -23,23 +29,21 @@ module Gnucash
|
|||||||
@node = node
|
@node = node
|
||||||
@name = node.xpath('act:name').text
|
@name = node.xpath('act:name').text
|
||||||
@type = node.xpath('act:type').text
|
@type = node.xpath('act:type').text
|
||||||
|
@description = node.xpath('act:description').text
|
||||||
@id = node.xpath('act:id').text
|
@id = node.xpath('act:id').text
|
||||||
@parent_id = node.xpath('act:parent').text
|
@parent_id = node.xpath('act:parent').text
|
||||||
@parent_id = nil if @parent_id == ""
|
@parent_id = nil if @parent_id == ""
|
||||||
@transactions = []
|
@transactions = []
|
||||||
@balances = []
|
@balances = []
|
||||||
|
@placeholder = node.xpath("act:slots/slot").find do |slot|
|
||||||
|
(slot.xpath("slot:key").first.text == "placeholder" and
|
||||||
|
slot.xpath("slot:value").first.text == "true")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Return the fully qualified account name
|
# Return the fully qualified account name
|
||||||
def full_name
|
def full_name
|
||||||
prefix = ""
|
@full_name ||= calculate_full_name
|
||||||
if @parent_id
|
|
||||||
parent = @book.find_account_by_id(@parent_id)
|
|
||||||
if parent and parent.type != 'ROOT'
|
|
||||||
prefix = parent.full_name + "::"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
prefix + name
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Internal method used to associate a transaction with the account
|
# Internal method used to associate a transaction with the account
|
||||||
@ -87,5 +91,18 @@ module Gnucash
|
|||||||
end
|
end
|
||||||
@balances[idx][:value]
|
@balances[idx][:value]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def calculate_full_name
|
||||||
|
prefix = ""
|
||||||
|
if @parent_id
|
||||||
|
parent = @book.find_account_by_id(@parent_id)
|
||||||
|
if parent and parent.type != 'ROOT'
|
||||||
|
prefix = parent.full_name + "::"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
prefix + name
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -2,7 +2,7 @@ module Gnucash
|
|||||||
# Class to link a transaction object to an Account.
|
# Class to link a transaction object to an Account.
|
||||||
class AccountTransaction
|
class AccountTransaction
|
||||||
# _Gnucash::Value_: The transaction value for the linked account
|
# _Gnucash::Value_: The transaction value for the linked account
|
||||||
attr_accessor :value
|
attr_reader :value
|
||||||
|
|
||||||
# Construct an AccountTransaction object.
|
# Construct an AccountTransaction object.
|
||||||
# This method is used internally when building a Transaction object.
|
# This method is used internally when building a Transaction object.
|
||||||
|
@ -5,16 +5,16 @@ module Gnucash
|
|||||||
# Represent a GnuCash Book
|
# Represent a GnuCash Book
|
||||||
class Book
|
class Book
|
||||||
# _Array_ of _Gnucash::Account_ objects in the book
|
# _Array_ of _Gnucash::Account_ objects in the book
|
||||||
attr_accessor :accounts
|
attr_reader :accounts
|
||||||
|
|
||||||
# _Array_ of _Gnucash::Transaction_ objects in the book
|
# _Array_ of _Gnucash::Transaction_ objects in the book
|
||||||
attr_accessor :transactions
|
attr_reader :transactions
|
||||||
|
|
||||||
# _String_ in "YYYY-MM-DD" format of the first transaction in the book
|
# _String_ in "YYYY-MM-DD" format of the first transaction in the book
|
||||||
attr_accessor :start_date
|
attr_reader :start_date
|
||||||
|
|
||||||
# _String_ in "YYYY-MM-DD" format of the last transaction in the book
|
# _String_ in "YYYY-MM-DD" format of the last transaction in the book
|
||||||
attr_accessor :end_date
|
attr_reader :end_date
|
||||||
|
|
||||||
# Construct a Book object.
|
# Construct a Book object.
|
||||||
# Normally called internally by Gnucash.open()
|
# Normally called internally by Gnucash.open()
|
||||||
@ -76,6 +76,9 @@ module Gnucash
|
|||||||
end
|
end
|
||||||
|
|
||||||
def finalize
|
def finalize
|
||||||
|
@accounts.sort! do |a, b|
|
||||||
|
a.full_name <=> b.full_name
|
||||||
|
end
|
||||||
@accounts.each do |account|
|
@accounts.each do |account|
|
||||||
account.finalize
|
account.finalize
|
||||||
end
|
end
|
||||||
|
@ -5,13 +5,16 @@ module Gnucash
|
|||||||
# with an individual account.
|
# with an individual account.
|
||||||
class Transaction
|
class Transaction
|
||||||
# _String_: The date of the transaction, in ISO format ("YYYY-MM-DD")
|
# _String_: The date of the transaction, in ISO format ("YYYY-MM-DD")
|
||||||
attr_accessor :date
|
attr_reader :date
|
||||||
|
|
||||||
# _String_: The GUID of the transaction
|
# _String_: The GUID of the transaction
|
||||||
attr_accessor :id
|
attr_reader :id
|
||||||
|
|
||||||
# _String_: The description of the transaction
|
# _String_: The description of the transaction
|
||||||
attr_accessor :description
|
attr_reader :description
|
||||||
|
|
||||||
|
# _Array_ of _Hash_ with keys +account+ and +value+
|
||||||
|
attr_reader :splits
|
||||||
|
|
||||||
# Create a new Transaction object
|
# Create a new Transaction object
|
||||||
# === Arguments
|
# === Arguments
|
||||||
@ -32,7 +35,7 @@ module Gnucash
|
|||||||
end
|
end
|
||||||
account.add_transaction(AccountTransaction.new(self, value))
|
account.add_transaction(AccountTransaction.new(self, value))
|
||||||
{
|
{
|
||||||
account_id: account_id,
|
account: account,
|
||||||
value: value,
|
value: value,
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
@ -5,7 +5,7 @@ module Gnucash
|
|||||||
include Comparable
|
include Comparable
|
||||||
|
|
||||||
# _Fixnum_:: The raw, undivided integer value
|
# _Fixnum_:: The raw, undivided integer value
|
||||||
attr_accessor :val
|
attr_reader :val
|
||||||
|
|
||||||
# Create a new Value object with value 0
|
# Create a new Value object with value 0
|
||||||
def self.zero
|
def self.zero
|
||||||
@ -59,6 +59,11 @@ module Gnucash
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Negate a Value
|
||||||
|
def -@
|
||||||
|
Value.new(-@val, @div)
|
||||||
|
end
|
||||||
|
|
||||||
# Multiply a Value object
|
# Multiply a Value object
|
||||||
# +other+ should be a Numeric
|
# +other+ should be a Numeric
|
||||||
def *(other)
|
def *(other)
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
module Gnucash
|
module Gnucash
|
||||||
# gem version
|
# gem version
|
||||||
VERSION = "1.0.0"
|
VERSION = "1.1.0"
|
||||||
end
|
end
|
||||||
|
@ -3,7 +3,9 @@ module Gnucash
|
|||||||
before(:all) do
|
before(:all) do
|
||||||
# just read the file once
|
# just read the file once
|
||||||
@book = Gnucash.open("spec/books/sample.gnucash")
|
@book = Gnucash.open("spec/books/sample.gnucash")
|
||||||
|
@assets = @book.find_account_by_full_name("Assets")
|
||||||
@checking = @book.find_account_by_full_name("Assets::Current Assets::Checking Account")
|
@checking = @book.find_account_by_full_name("Assets::Current Assets::Checking Account")
|
||||||
|
@income = @book.find_account_by_full_name("Income")
|
||||||
@salary = @book.find_account_by_full_name("Income::Salary")
|
@salary = @book.find_account_by_full_name("Income::Salary")
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -11,6 +13,10 @@ module Gnucash
|
|||||||
@salary.name.should == "Salary"
|
@salary.name.should == "Salary"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "gives access to the account description" do
|
||||||
|
@checking.description.should == "Checking Account"
|
||||||
|
end
|
||||||
|
|
||||||
it "gives access to the fully-qualified account name" do
|
it "gives access to the fully-qualified account name" do
|
||||||
@checking.full_name.should == "Assets::Current Assets::Checking Account"
|
@checking.full_name.should == "Assets::Current Assets::Checking Account"
|
||||||
end
|
end
|
||||||
@ -36,5 +42,12 @@ module Gnucash
|
|||||||
@checking.balance_on("2007-03-27").should == Value.new(780000)
|
@checking.balance_on("2007-03-27").should == Value.new(780000)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "stores whether the account was a placeholder" do
|
||||||
|
@assets.placeholder.should be_true
|
||||||
|
@checking.placeholder.should be_false
|
||||||
|
@income.placeholder.should be_true
|
||||||
|
@salary.placeholder.should be_false
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -51,6 +51,12 @@ module Gnucash
|
|||||||
c.should == 1.1
|
c.should == 1.1
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "allows negating a Value object" do
|
||||||
|
a = Value.new("123/100")
|
||||||
|
b = -a
|
||||||
|
b.to_s.should == "-1.23"
|
||||||
|
end
|
||||||
|
|
||||||
it "allows multiplying a Value by a Numeric" do
|
it "allows multiplying a Value by a Numeric" do
|
||||||
a = Value.new("100/100")
|
a = Value.new("100/100")
|
||||||
b = 12
|
b = 12
|
||||||
|
Loading…
x
Reference in New Issue
Block a user