Class: RVGP::Pta::HLedger

Inherits:
RVGP::Pta show all
Defined in:
lib/rvgp/pta/hledger.rb

Overview

A plain text accounting adapter implementation, for the ‘ledger’ pta command. This class conforms the ledger query, and output, interfaces in a ruby-like syntax, and with structured ruby objects as outputs.

For a more detailed example of these queries in action, take a look at the test/test_pta_adapter.rb

Defined Under Namespace

Modules: Output

Instance Method Summary collapse

Methods inherited from RVGP::Pta

#adapter_name, #args_and_opts, #bin_path, #command, hledger, #hledger?, ledger, #ledger?, #present?, pta, pta_adapter=, #stats

Instance Method Details

#balance(*args) ⇒ RVGP::Pta::HLedger::Output::Balance

Run the ‘hledger balance’ command, and return it’s output.

Parameters:

  • args (Array<Object>)

    Arguments and options, passed to the pta command. See RVGP::Pta#args_and_opts for details.

Returns:



211
212
213
214
# File 'lib/rvgp/pta/hledger.rb', line 211

def balance(*args)
  args, opts = args_and_opts(*args)
  RVGP::Pta::HLedger::Output::Balance.new command('balance', *args, { 'output-format': 'json' }.merge(opts))
end

#files(*args) ⇒ Array<String>

Return the files that were encountered, when parsing the provided arguments. The output of this method should be identical, regardless of the Pta Adapter that resolves the request.

Parameters:

  • args (Array<Object>)

    Arguments and options, passed to the pta command. See RVGP::Pta#args_and_opts for details

Returns:

  • (Array<String>)

    An array of paths that were referenced when fetching data in provided arguments.

Raises:

  • (StandardError)


157
158
159
160
161
162
163
# File 'lib/rvgp/pta/hledger.rb', line 157

def files(*args)
  args, opts = args_and_opts(*args)
  # TODO: This should get its own error class...
  raise StandardError, "Unexpected argument(s) : #{args.inspect}" unless args.empty?

  command('files', opts).split("\n")
end

#newest_transaction(*args) ⇒ RVGP::Pta::RegisterTransaction

Returns the newest transaction, retured in set of transactions filtered with the provided arguments. This method is mostly a wrapper around #register, which a return of the .last element in its set. The only reason this method here is to ensure parity with the Ledger class, which, exists because an accelerated query is offered by that pta implementation. This method may produce counterintutive results, if you override the sort: option.

NOTE: For almost any case you think you want to use this method, #newest_transaction_date is probably what you want, as that function has an accelerated implementation provided by hledger.

Parameters:

  • args (Array<Object>)

    Arguments and options, passed to the pta command. See RVGP::Pta#args_and_opts for details.

Returns:



177
178
179
# File 'lib/rvgp/pta/hledger.rb', line 177

def newest_transaction(*args)
  register(*args)&.transactions&.last
end

#newest_transaction_date(*args) ⇒ Date

Returns the value of the ‘Last transaction’ key, of the #RVGP::Pta#stats method. This method is a fast query to resolve.

Parameters:

  • args (Array<Object>)

    Arguments and options, passed to the pta command. See RVGP::Pta#args_and_opts for details.

Returns:

  • (Date)

    The date of the newest transaction found in your files.



203
204
205
# File 'lib/rvgp/pta/hledger.rb', line 203

def newest_transaction_date(*args)
  Date.strptime stats(*args)['Last transaction'], '%Y-%m-%d'
end

#oldest_transaction(*args) ⇒ RVGP::Pta::RegisterTransaction

Returns the oldest transaction, retured in set of transactions filtered with the provided arguments. This method is mostly a wrapper around #register, which a return of the .last element in its set. The only reason this method here is to ensure parity with the Ledger class, which, exists because an accelerated query is offered by that pta implementation. This method may produce counterintutive results, if you override the sort: option.

NOTE: There is almost certainly, no a good reason to be using this method. Perhaps in the future, hledger will offer an equivalent to ledger’s –head and –tail options, at which time this method would make sense.

Parameters:

  • args (Array<Object>)

    Arguments and options, passed to the pta command. See RVGP::Pta#args_and_opts for details.

Returns:



194
195
196
# File 'lib/rvgp/pta/hledger.rb', line 194

def oldest_transaction(*args)
  register(*args)&.transactions&.first
end

#register(*args) ⇒ RVGP::Pta::HLedger::Output::Register

Run the ‘hledger register’ command, and return it’s output.

This method also supports the following options, for additional handling:

  • :pricer (RVGP::Journal::Pricer) - If provided, this option will use the specified pricer object when calculating exchange rates.

Parameters:

  • args (Array<Object>)

    Arguments and options, passed to the pta command. See RVGP::Pta#args_and_opts for details.

Returns:



225
226
227
228
229
230
231
232
233
234
# File 'lib/rvgp/pta/hledger.rb', line 225

def register(*args)
  args, opts = args_and_opts(*args)

  pricer = opts.delete :pricer

  # TODO: Provide and Test translate_meta_accounts here
  RVGP::Pta::HLedger::Output::Register.new command('register', *args, { 'output-format': 'json' }.merge(opts)),
                                           monthly: (opts[:monthly] == true),
                                           pricer: pricer
end

#tags(*args) ⇒ Array<String>

Return the tags that were found, given the specified journal path, and filters.

The behavior between hledger and ledger are rather different here. Ledger has a slightly different featureset than HLedger, regarding tags. As such, while the return format is the same between implementations. The results for a given query won’t be identical between pta implementations. Mostly, these results differ when a {values: true} option is supplied. In that case, ledger will return tags in a series of keys and values, separated by a colon, one per line. hledger, in that case, will only return the tag values themselves, without denotating their key.

This method will simply parse the output of hledger, and return that.

Parameters:

  • args (Array<Object>)

    Arguments and options, passed to the pta command. See RVGP::Pta#args_and_opts for details

Returns:

  • (Array<String>)

    An array of the lines returned by hledger, split into strings. In most cases, this could also be described as simply ‘an array of the filtered tags’.



146
147
148
149
# File 'lib/rvgp/pta/hledger.rb', line 146

def tags(*args)
  args, opts = args_and_opts(*args)
  command('tags', *args, opts).split("\n")
end