Class: Gnucash::Transaction

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

Overview

Represent a GnuCash transaction.

Transactions have multiple splits with individual values. Splits are created as AccountTransaction objects which are associated with an individual account.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Support::LightInspect

#inspect

Constructor Details

#initialize(book, node) ⇒ Transaction

Create a new Transaction object.

Parameters:

  • book (Book)

    The Book containing the transaction.

  • node (Nokogiri::XML::Node)

    Nokogiri XML node.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/gnucash/transaction.rb', line 28

def initialize(book, node)
  @book = book
  @node = node
  @id = node.xpath('trn:id').text
  @date = Date.parse(node.xpath('trn:date-posted/ts:date').text.split(' ').first)
  @description = node.xpath('trn:description').text
  @splits = node.xpath('trn:splits/trn:split').map do |split_node|
    # Note: split:value represents the split value in the transaction's
    # currency while split:quantity represents it in the currency
    # associated with the account associated with this split.
    value = Value.new(split_node.xpath('split:quantity').text)
     = split_node.xpath('split:account').text
     = @book.()
    unless 
      raise "Could not find account with ID #{} for transaction #{@id}"
    end
    .add_transaction(AccountTransaction.new(self, value))
    {
      account: ,
      value: value,
    }
  end
end

Instance Attribute Details

#dateDate (readonly)

Returns The date of the transaction.

Returns:

  • (Date)

    The date of the transaction.



13
14
15
# File 'lib/gnucash/transaction.rb', line 13

def date
  @date
end

#descriptionString (readonly)

Returns The description of the transaction.

Returns:

  • (String)

    The description of the transaction.



19
20
21
# File 'lib/gnucash/transaction.rb', line 19

def description
  @description
end

#idString (readonly)

Returns The GUID of the transaction.

Returns:

  • (String)

    The GUID of the transaction.



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

def id
  @id
end

#splitsArray<Hash> (readonly)

Returns Hashes with keys :account and :value.

Returns:

  • (Array<Hash>)

    Hashes with keys :account and :value.



22
23
24
# File 'lib/gnucash/transaction.rb', line 22

def splits
  @splits
end

Instance Method Details

#attributesArray<Symbol>

Attributes available for inspection

Returns:

  • (Array<Symbol>)

    Attributes used to build the inspection string

See Also:



56
57
58
# File 'lib/gnucash/transaction.rb', line 56

def attributes
  %i[id date description splits]
end