Class: RVGP::Pta::HLedger::Output::Register

Inherits:
JsonBase
  • Object
show all
Defined in:
lib/rvgp/pta/hledger.rb

Overview

An json parser, to structure the output of register queries to ledger. This object exists, as a return value, from the RVGP::Pta::HLedger#register method

Instance Attribute Summary collapse

Attributes inherited from JsonBase

#json, #pricer

Instance Method Summary collapse

Constructor Details

#initialize(json, options = {}) ⇒ Register

Declare the registry, and initialize with the relevant options

Parameters:



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/rvgp/pta/hledger.rb', line 95

def initialize(json, options = {})
  super json, options

  @transactions = @json.each_with_object([]) do |row, sum|
    row[0] ? (sum << [row]) : (sum.last << row)
  end

  @transactions.map! do |postings|
    date = Date.strptime postings[0][0], '%Y-%m-%d'

    RVGP::Pta::RegisterTransaction.new(
      date,
      postings[0][2], # Payee
      (postings.map do |posting|
        amounts, totals = [posting[3][:pamount], posting[4]].map do |pamounts|
          pamounts.map { |pamount| commodity_from_json pamount }
        end

        RVGP::Pta::RegisterPosting.new(
          posting[3][:paccount],
          amounts,
          totals,
          posting[3][:ptags].to_h do |pair|
            [pair.first, pair.last.empty? ? true : pair.last]
          end,
          pricer: pricer,
          # This sets our date to the end -of the month, if this is a
          # monthly query
          price_date: options[:monthly] ? Date.new(date.year, date.month, -1) : date
        )
      end)
    )
  end
end

Instance Attribute Details

#transactionsRVGP::Pta::RegisterTransaction (readonly)

The transactions, and their components, that were returned by ledger.

Returns:



89
90
91
# File 'lib/rvgp/pta/hledger.rb', line 89

def transactions
  @transactions
end