Class: Gnucash::Book
- Inherits:
-
Object
- Object
- Gnucash::Book
- Includes:
- Support::LightInspect
- Defined in:
- lib/gnucash/book.rb
Overview
Represent a GnuCash Book.
Defined Under Namespace
Classes: PriceRow
Instance Attribute Summary collapse
-
#accounts ⇒ Array<Account>
readonly
Accounts in the book.
-
#customers ⇒ Array<Account>
readonly
Customers in the book.
-
#end_date ⇒ Date
readonly
Date of the last transaction in the book.
-
#price_rows ⇒ Array<PriceRow>
readonly
Raw price-database rows (commodity/currency/value/date).
-
#start_date ⇒ Date
readonly
Date of the first transaction in the book.
-
#transactions ⇒ Array<Transaction>
readonly
Transactions in the book.
Instance Method Summary collapse
-
#attributes ⇒ Array<Symbol>
Attributes available for inspection.
-
#find_account_by_full_name(full_name) ⇒ Account?
Return a handle to the Account object that has the given fully-qualified name.
-
#find_account_by_id(id) ⇒ Account?
Return a handle to the Account object that has the given GUID.
-
#find_customer_by_full_name(full_name) ⇒ Customer?
Return a handle to the Customer object that has the given fully-qualified name.
-
#find_security(space, id) ⇒ Security?
Look up a security by GnuCash commodity
spaceandid. -
#find_security_by_isin(isin) ⇒ Security?
Look up a priced security whose commodity defines this ISIN (
cmdty:xcodeor a slot whose key matchesisin, e.g.user:ISIN). -
#initialize(fname) ⇒ Book
constructor
Construct a Book object.
-
#isin_for_commodity(space, id) ⇒ String?
ISIN for a commodity if present in the book (
nilotherwise). -
#quotes_for_commodity(space, id) ⇒ Array<PriceRow>
Price-database rows for one commodity (used by Security#value_on).
-
#securities ⇒ Array<Security>
Return every Security that appears in the price database (unique commodity).
Methods included from Support::LightInspect
Constructor Details
#initialize(fname) ⇒ Book
Construct a Book object.
Normally called internally by Gnucash.open.
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
#accounts ⇒ Array<Account> (readonly)
Returns Accounts in the book.
16 17 18 |
# File 'lib/gnucash/book.rb', line 16 def accounts @accounts end |
#customers ⇒ Array<Account> (readonly)
Returns Customers in the book.
20 21 22 |
# File 'lib/gnucash/book.rb', line 20 def customers @customers end |
#end_date ⇒ Date (readonly)
Returns 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_rows ⇒ Array<PriceRow> (readonly)
Returns Raw price-database rows (commodity/currency/value/date). Prefer Security#value_on for valuations.
29 30 31 |
# File 'lib/gnucash/book.rb', line 29 def price_rows @price_rows end |
#start_date ⇒ Date (readonly)
Returns Date of the first transaction in the book.
32 33 34 |
# File 'lib/gnucash/book.rb', line 32 def start_date @start_date end |
#transactions ⇒ Array<Transaction> (readonly)
Returns Transactions in the book.
23 24 25 |
# File 'lib/gnucash/book.rb', line 23 def transactions @transactions end |
Instance Method Details
#attributes ⇒ Array<Symbol>
Attributes available for inspection
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.
79 80 81 |
# File 'lib/gnucash/book.rb', line 79 def find_account_by_full_name(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.
68 69 70 |
# File 'lib/gnucash/book.rb', line 68 def find_account_by_id(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.
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.
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.
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).
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).
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 |
#securities ⇒ Array<Security>
Return every Security that appears in the price database (unique commodity).
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 |