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
-
#code ⇒ String?
readonly
Account code (
act:code), if set in GnuCash. -
#commodity_id ⇒ String?
readonly
Commodity id (
cmdty:id) fromact:commodity, if present. -
#commodity_space ⇒ String?
readonly
Commodity namespace (
cmdty:space) fromact:commodity, if present. -
#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.
-
#security ⇒ Security?
Priced Security for this account’s commodity, if the commodity appears in the book’s price database (
gnc:pricedb).
Methods included from Support::LightInspect
Constructor Details
#initialize(book, node) ⇒ Account
Create an Account object.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/gnucash/account.rb', line 49 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 code_raw = node.at_xpath('act:code')&.text @code = (code_raw.nil? || code_raw.empty?) ? nil : code_raw @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 cmd = node.at_xpath("act:commodity") if cmd @commodity_space = cmd.at_xpath("cmdty:space")&.text&.strip @commodity_id = cmd.at_xpath("cmdty:id")&.text&.strip @commodity_space = nil if @commodity_space.nil? || @commodity_space.empty? @commodity_id = nil if @commodity_id.nil? || @commodity_id.empty? else @commodity_space = nil @commodity_id = nil end end |
Instance Attribute Details
#code ⇒ String? (readonly)
Returns Account code (act:code), if set in GnuCash.
21 22 23 |
# File 'lib/gnucash/account.rb', line 21 def code @code end |
#commodity_id ⇒ String? (readonly)
Returns Commodity id (cmdty:id) from act:commodity, if present.
38 39 40 |
# File 'lib/gnucash/account.rb', line 38 def commodity_id @commodity_id end |
#commodity_space ⇒ String? (readonly)
Returns Commodity namespace (cmdty:space) from act:commodity, if present.
33 34 35 |
# File 'lib/gnucash/account.rb', line 33 def commodity_space @commodity_space end |
#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.
43 44 45 |
# File 'lib/gnucash/account.rb', line 43 def parent_id @parent_id end |
#placeholder ⇒ Boolean (readonly)
Returns Whether the account is a placeholder or not.
28 29 30 |
# File 'lib/gnucash/account.rb', line 28 def placeholder @placeholder end |
#transactions ⇒ Array<AccountTransaction> (readonly)
Returns List of transactions associated with this account.
25 26 27 |
# File 'lib/gnucash/account.rb', line 25 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.
110 111 112 |
# File 'lib/gnucash/account.rb', line 110 def add_transaction(act_txn) @transactions << act_txn end |
#attributes ⇒ Array<Symbol>
Attributes available for inspection
183 184 185 |
# File 'lib/gnucash/account.rb', line 183 def attributes %i[id name description type code placeholder parent_id commodity_space commodity_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.
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/gnucash/account.rb', line 151 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.
133 134 135 136 |
# File 'lib/gnucash/account.rb', line 133 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.
118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/gnucash/account.rb', line 118 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.
94 95 96 |
# File 'lib/gnucash/account.rb', line 94 def full_name @full_name ||= calculate_full_name end |
#parent ⇒ Account?
Lookup for the parent account in the book.
103 104 105 |
# File 'lib/gnucash/account.rb', line 103 def parent @parent ||= @parent_id ? @book.find_account_by_id(@parent_id) : nil end |
#security ⇒ Security?
Priced Security for this account’s commodity, if the commodity appears in the book’s price database (gnc:pricedb). Otherwise nil.
85 86 87 88 89 |
# File 'lib/gnucash/account.rb', line 85 def security return nil unless @commodity_space && @commodity_id @book.find_security(@commodity_space, @commodity_id) end |