Class: Gnucash::Book

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

Overview

Represent a GnuCash Book.

Defined Under Namespace

Classes: PriceRow

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.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/gnucash/book.rb', line 44

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
  build_price_quotes
  build_commodity_isin_index
  finalize
end

Instance Attribute Details

#accountsArray<Account> (readonly)

Returns Accounts in the book.

Returns:

  • (Array<Account>)

    Accounts in the book.



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

def accounts
  @accounts
end

#customersArray<Account> (readonly)

Returns Customers in the book.

Returns:

  • (Array<Account>)

    Customers in the book.

Since:

  • 1.6.0



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

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.



35
36
37
# File 'lib/gnucash/book.rb', line 35

def end_date
  @end_date
end

#price_rowsArray<PriceRow> (readonly)

Returns Raw price-database rows (commodity/currency/value/date). Prefer Security#value_on for valuations.

Returns:

Since:

  • 1.6.0



29
30
31
# File 'lib/gnucash/book.rb', line 29

def price_rows
  @price_rows
end

#start_dateDate (readonly)

Returns Date of the first transaction in the book.

Returns:

  • (Date)

    Date of the first transaction in the book.



32
33
34
# File 'lib/gnucash/book.rb', line 32

def start_date
  @start_date
end

#transactionsArray<Transaction> (readonly)

Returns Transactions in the book.

Returns:



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

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:



157
158
159
# File 'lib/gnucash/book.rb', line 157

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.



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

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.



68
69
70
# File 'lib/gnucash/book.rb', line 68

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



92
93
94
# File 'lib/gnucash/book.rb', line 92

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

#find_security(space, id) ⇒ Security?

Look up a security by GnuCash commodity space and id.

Returns:

Since:

  • 1.6.0



112
113
114
115
# File 'lib/gnucash/book.rb', line 112

def find_security(space, id)
  return nil unless @price_rows.any? { |r| r.commodity_space == space && r.commodity_id == id }
  Security.new(self, space, id)
end

#find_security_by_isin(isin) ⇒ Security?

Look up a priced security whose commodity defines this ISIN (cmdty:xcode or a slot whose key matches isin, e.g. user:ISIN). Comparison ignores spaces, hyphens and case.

Parameters:

  • isin (String)

    ISIN as stored or typed (e.g. “US0378331005”).

Returns:

Since:

  • 1.6.0



125
126
127
128
129
130
131
132
133
# File 'lib/gnucash/book.rb', line 125

def find_security_by_isin(isin)
  key = ISIN.normalize(isin)
  return nil if key.empty?

  pair = @isin_index[key]
  return nil unless pair

  find_security(pair[0], pair[1])
end

#isin_for_commodity(space, id) ⇒ String?

ISIN for a commodity if present in the book (nil otherwise).

Returns:

  • (String, nil)

    Normalized ISIN (12 uppercase alphanumeric characters).

Since:

  • 1.6.0



140
141
142
# File 'lib/gnucash/book.rb', line 140

def isin_for_commodity(space, id)
  @isin_for_commodity[[space, id]]
end

#quotes_for_commodity(space, id) ⇒ Array<PriceRow>

Price-database rows for one commodity (used by Security#value_on).

Returns:

Since:

  • 1.6.0



149
150
151
# File 'lib/gnucash/book.rb', line 149

def quotes_for_commodity(space, id)
  @price_rows.select { |r| r.commodity_space == space && r.commodity_id == id }
end

#securitiesArray<Security>

Return every Security that appears in the price database (unique commodity).

Returns:

Since:

  • 1.6.0



101
102
103
104
105
# File 'lib/gnucash/book.rb', line 101

def securities
  @securities ||= @price_rows.map { |r| [r.commodity_space, r.commodity_id] }.uniq.map do |space, id|
    Security.new(self, space, id)
  end
end