diff --git a/lib/gnucash/account.rb b/lib/gnucash/account.rb index 097ac59..8750346 100644 --- a/lib/gnucash/account.rb +++ b/lib/gnucash/account.rb @@ -1,9 +1,28 @@ module Gnucash class Account + attr_accessor :name + attr_accessor :type + attr_accessor :id + def initialize(book, node) + @book = book @node = node @name = node.xpath('act:name').text @type = node.xpath('act:type').text + @id = node.xpath('act:id').text + @parent_id = node.xpath('act:parent').text + @parent_id = nil if @parent_id == "" + end + + def full_name + prefix = "" + if @parent_id + parent = @book.find_account_by_id(@parent_id) + if parent and parent.type != 'ROOT' + prefix = parent.full_name + "::" + end + end + prefix + name end end end diff --git a/lib/gnucash/book.rb b/lib/gnucash/book.rb index 2e670eb..1be221b 100644 --- a/lib/gnucash/book.rb +++ b/lib/gnucash/book.rb @@ -15,6 +15,10 @@ module Gnucash build_accounts end + def find_account_by_id(id) + @accounts.find { |a| a.id == id } + end + private def build_accounts