Class: Gnucash::Book

Inherits:
Object
  • Object
show all
Includes:
Support::LightInspect
Defined in:
lib/gnucash/book.rb

Overview

Represent a GnuCash Book.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Support::LightInspect

#inspect

Constructor Details

#initialize(fname) ⇒ Book

Construct a Book object.

Normally called internally by Gnucash.open.

Parameters:

  • fname (String)

    The file name of the GnuCash file to open. Only XML format (or gzipped XML format) GnuCash data files are recognized.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/gnucash/book.rb', line 32

def initialize(fname)
  begin
    @ng = Nokogiri.XML(Zlib::GzipReader.open(fname).read)
  rescue Zlib::GzipFile::Error
    @ng = Nokogiri.XML(File.read(fname))
  end
  book_nodes = @ng.xpath('/gnc-v2/gnc:book')
  if book_nodes.count != 1
    raise "Error: Expected to find one gnc:book entry"
  end
  @book_node = book_nodes.first
  build_customers
  build_accounts
  build_transactions
  finalize
end

Instance Attribute Details

#accountsArray<Account> (readonly)

Returns Accounts in the book.

Returns:

  • (Array<Account>)

    Accounts in the book.



10
11
12
# File 'lib/gnucash/book.rb', line 10

def accounts
  @accounts
end

#customersArray<Account> (readonly)

Returns Customers in the book.

Returns:

  • (Array<Account>)

    Customers in the book.

Since:

  • 1.6.0



14
15
16
# File 'lib/gnucash/book.rb', line 14

def customers
  @customers
end

#end_dateDate (readonly)

Returns Date of the last transaction in the book.

Returns:

  • (Date)

    Date of the last transaction in the book.



23
24
25
# File 'lib/gnucash/book.rb', line 23

def end_date
  @end_date
end

#start_dateDate (readonly)

Returns Date of the first transaction in the book.

Returns:

  • (Date)

    Date of the first transaction in the book.



20
21
22
# File 'lib/gnucash/book.rb', line 20

def start_date
  @start_date
end

#transactionsArray<Transaction> (readonly)

Returns Transactions in the book.

Returns:



17
18
19
# File 'lib/gnucash/book.rb', line 17

def transactions
  @transactions
end

Instance Method Details

#attributesArray<Symbol>

Attributes available for inspection

Returns:

  • (Array<Symbol>)

    Attributes used to build the inspection string

See Also:



86
87
88
# File 'lib/gnucash/book.rb', line 86

def attributes
  %i[start_date end_date]
end

#find_account_by_full_name(full_name) ⇒ Account?

Return a handle to the Account object that has the given fully-qualified name.

Parameters:

  • full_name (String)

    Fully-qualified account name (ex: “Expenses::Auto::Gas”).

Returns:

  • (Account, nil)

    Account object, or nil if not found.



65
66
67
# File 'lib/gnucash/book.rb', line 65

def (full_name)
  @accounts.find { |a| a.full_name == full_name }
end

#find_account_by_id(id) ⇒ Account?

Return a handle to the Account object that has the given GUID.

Parameters:

  • id (String)

    GUID.

Returns:

  • (Account, nil)

    Account object, or nil if not found.



54
55
56
# File 'lib/gnucash/book.rb', line 54

def (id)
  @accounts.find { |a| a.id == id }
end

#find_customer_by_full_name(full_name) ⇒ Customer?

Return a handle to the Customer object that has the given fully-qualified name.

Parameters:

  • full_name (String)

    Fully-qualified customer name (ex: “Joe Doe”).

Returns:

  • (Customer, nil)

    Customer object, or nil if not found.

Since:

  • 1.6.0



78
79
80
# File 'lib/gnucash/book.rb', line 78

def find_customer_by_full_name(full_name)
  @customers.find { |a| a.full_name == full_name }
end