Class: Gnucash::Account
- Inherits:
-
Object
- Object
- Gnucash::Account
- Includes:
- Support::LightInspect
- Defined in:
- lib/gnucash/account.rb
Overview
Represent a GnuCash account object.
Instance Attribute Summary collapse
-
#description ⇒ String
readonly
The account description.
-
#id ⇒ String
readonly
The GUID of the account.
-
#name ⇒ String
readonly
The name of the account (unqualified).
-
#parent_id ⇒ String?
readonly
The GUID of the parent account, if any.
-
#placeholder ⇒ Boolean
readonly
Whether the account is a placeholder or not.
-
#transactions ⇒ Array<AccountTransaction>
readonly
List of transactions associated with this account.
-
#type ⇒ String
readonly
The account type (such as “EXPENSE”).
Instance Method Summary collapse
-
#add_transaction(act_txn) ⇒ void
Internal method used to associate a transaction with the account.
-
#attributes ⇒ Array<Symbol>
Attributes available for inspection.
-
#balance_on(date, options = {}) ⇒ Value
Return the balance of the account as of the date given as a Value.
-
#final_balance ⇒ Value
Return the final balance of the account as a Value.
-
#finalize ⇒ void
Internal method used to complete initialization of the Account after all transactions have been associated with it.
-
#full_name ⇒ String
Return the fully qualified account name.
-
#initialize(book, node) ⇒ Account
constructor
Create an Account object.
-
#parent ⇒ Account?
Lookup for the parent account in the book.
Methods included from Support::LightInspect
Constructor Details
#initialize(book, node) ⇒ Account
Create an Account object.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/gnucash/account.rb', line 34 def initialize(book, node) @book = book @node = node @name = node.xpath('act:name').text @type = node.xpath('act:type').text @description = node.xpath('act:description').text @id = node.xpath('act:id').text @parent_id = node.xpath('act:parent').text @parent_id = nil if @parent_id == "" @transactions = [] @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 ? true : false end |
Instance Attribute Details
#description ⇒ String (readonly)
Returns The account description.
10 11 12 |
# File 'lib/gnucash/account.rb', line 10 def description @description end |
#id ⇒ String (readonly)
Returns The GUID of the account.
16 17 18 |
# File 'lib/gnucash/account.rb', line 16 def id @id end |
#name ⇒ String (readonly)
Returns The name of the account (unqualified).
7 8 9 |
# File 'lib/gnucash/account.rb', line 7 def name @name end |
#parent_id ⇒ String? (readonly)
Returns The GUID of the parent account, if any.
28 29 30 |
# File 'lib/gnucash/account.rb', line 28 def parent_id @parent_id end |
#placeholder ⇒ Boolean (readonly)
Returns Whether the account is a placeholder or not.
23 24 25 |
# File 'lib/gnucash/account.rb', line 23 def placeholder @placeholder end |
#transactions ⇒ Array<AccountTransaction> (readonly)
Returns List of transactions associated with this account.
20 21 22 |
# File 'lib/gnucash/account.rb', line 20 def transactions @transactions end |
#type ⇒ String (readonly)
Returns The account type (such as “EXPENSE”).
13 14 15 |
# File 'lib/gnucash/account.rb', line 13 def type @type end |
Instance Method Details
#add_transaction(act_txn) ⇒ void
This method returns an undefined value.
Internal method used to associate a transaction with the account.
70 71 72 |
# File 'lib/gnucash/account.rb', line 70 def add_transaction(act_txn) @transactions << act_txn end |
#attributes ⇒ Array<Symbol>
Attributes available for inspection
143 144 145 |
# File 'lib/gnucash/account.rb', line 143 def attributes %i[id name description type placeholder parent_id] end |
#balance_on(date, options = {}) ⇒ Value
Return the balance of the account as of the date given as a Value. Transactions that occur on the given date are included in the returned balance.
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/gnucash/account.rb', line 111 def balance_on(date, = {}) date = Date.parse(date) if date.is_a?(String) return_value = Value.new(0) if [:recursive] # Get all child accounts from this account and accumulate the balances of them. @book.accounts.reject { |account| account.parent != self }.each do |child_account| return_value += child_account.balance_on(date, recursive: true) end end return return_value unless @balances.size > 0 return return_value if @balances.first[:date] > date return @balances.last[:value] if date >= @balances.last[:date] imin = 0 imax = @balances.size - 2 idx = imax / 2 until @balances[idx][:date] <= date and @balances[idx + 1][:date] > date if @balances[idx][:date] <= date imin = idx + 1 else imax = idx end idx = (imin + imax) / 2 end @balances[idx][:value] end |
#final_balance ⇒ Value
Return the final balance of the account as a Value.
93 94 95 96 |
# File 'lib/gnucash/account.rb', line 93 def final_balance return Value.new(0) unless @balances.size > 0 @balances.last[:value] end |
#finalize ⇒ void
This method returns an undefined value.
Internal method used to complete initialization of the Account after all transactions have been associated with it.
78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/gnucash/account.rb', line 78 def finalize @transactions.sort! { |a, b| a.date <=> b.date } balance = Value.new(0) @balances = @transactions.map do |act_txn| balance += act_txn.value { date: act_txn.date, value: balance, } end end |
#full_name ⇒ String
Return the fully qualified account name.
54 55 56 |
# File 'lib/gnucash/account.rb', line 54 def full_name @full_name ||= calculate_full_name end |
#parent ⇒ Account?
Lookup for the parent account in the book.
63 64 65 |
# File 'lib/gnucash/account.rb', line 63 def parent @parent ||= @parent_id ? @book.find_account_by_id(@parent_id) : nil end |