Class: RVGP::Pta::Ledger

Inherits:
RVGP::Pta show all
Defined in:
lib/rvgp/pta/ledger.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::Ledger::Output::Balance

Run the ‘ledger 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:



262
263
264
265
266
# File 'lib/rvgp/pta/ledger.rb', line 262

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

  RVGP::Pta::Ledger::Output::Balance.new command('xml', *args, 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)


210
211
212
213
214
215
216
217
218
219
# File 'lib/rvgp/pta/ledger.rb', line 210

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?

  stats(*args, opts)['Files these postings came from'].tap do |ret|
    ret.unshift opts[:file] if opts.key?(:file) && !ret.include?(opts[:file])
  end
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} with the {tail: 1} option passed to that method. 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 more efficient. Particularly since this method has accelerated implementation in its Hledger counterpart

Parameters:

  • args (Array<Object>)

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

Returns:



232
233
234
235
# File 'lib/rvgp/pta/ledger.rb', line 232

def newest_transaction(*args)
  args, opts = args_and_opts(*args)
  first_transaction(*args, opts.merge(sort: 'date', tail: 1))
end

#newest_transaction_date(*args) ⇒ Date

Returns the value of the ‘Time Period’ 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.



254
255
256
# File 'lib/rvgp/pta/ledger.rb', line 254

def newest_transaction_date(*args)
  Date.strptime ::Regexp.last_match(1), '%y-%b-%d' if /to ([^ ]+)/.match stats(*args)['Time period']
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 with the {head: 1} option passed to that method. This method may produce counterintutive results, if you override the sort: option.

Parameters:

  • args (Array<Object>)

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

Returns:



244
245
246
247
# File 'lib/rvgp/pta/ledger.rb', line 244

def oldest_transaction(*args)
  args, opts = args_and_opts(*args)
  first_transaction(*args, opts.merge(sort: 'date', head: 1))
end

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

Run the ‘ledger 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.

  • :empty (TrueClass, FalseClass) - If false, we’ll remove any accounts and totals, that have quantities of zero.

  • :translate_meta_accounts (TrueClass, FalseClass) - If true, we’ll convert accounts of name ‘<None>’ to nil, and ‘<Total>’ to :total. This is mostly to useful when trying to preserve uniform behaviors between pta adapters. (hledger seems to offer us nil, in cases where ledger offers us ‘<None>’)

Parameters:

  • args (Array<Object>)

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

Returns:



282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/rvgp/pta/ledger.rb', line 282

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

  pricer = opts.delete :pricer
  translate_meta_accounts = opts[:empty]

  # We stipulate, by default, a date sort. Mostly because it makes sense. But, also so
  # that this matches HLedger's default sort order
  RVGP::Pta::Ledger::Output::Register.new command('xml', *args, { sort: 'date' }.merge(opts)),
                                          monthly: (opts[:monthly] == true),
                                          empty: opts[:empty],
                                          pricer: pricer,
                                          translate_meta_accounts: translate_meta_accounts
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 ledger, 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 ledger, split into strings. In most cases, this could also be described as simply ‘an array of the filtered tags’.



192
193
194
195
196
197
198
199
200
201
202
# File 'lib/rvgp/pta/ledger.rb', line 192

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

  # The first arg, is the tag whose values we want. This is how hledger does it, and
  # we just copy that
  for_tag = args.shift unless args.empty?

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

  for_tag ? tags.map { |tag| ::Regexp.last_match(1) if /\A#{for_tag}: *(.*)/.match tag }.compact : tags
end