Class: RVGP::Journal::Posting

Inherits:
Object
  • Object
show all
Defined in:
lib/rvgp/journal/posting.rb

Overview

This class represents a single posting, in a PTA journal. A posting is typically of the following form: “‘ 2020-02-10 Frozen Chicken from the Local Supermarket

Personal:Expenses:Food:Groceries    $ 50.00
Cash

“‘ This is a simple example. There are a good number of permutations under which posting components s appear. Nonetheless, a posting is typically comprised of a date, a description, and a number of RVGP::Journal::Posting::Transfer lines, indented below these fields. This object represents the parsed format, of a post, traveling around the RVGP codebase.

Defined Under Namespace

Classes: Tag, Transfer

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(date, description, opts = {}) ⇒ Posting

Create a posting, from constituent parts

Parameters:

  • date (Date)

    see #date

  • description (String)
  • opts (Hash) (defaults to: {})

    Additional parts of this Posting

Options Hash (opts):



91
92
93
94
95
96
97
# File 'lib/rvgp/journal/posting.rb', line 91

def initialize(date, description, opts = {})
  @line_number = opts[:line_number]
  @date = date
  @description = description
  @transfers = opts.key?(:transfers) ? opts[:transfers] : []
  @tags = opts.key?(:tags) ? opts[:tags] : []
end

Instance Attribute Details

#dateDate (readonly)

The date this posting occurred

Returns:

  • (Date)

    the current value of date



23
24
25
# File 'lib/rvgp/journal/posting.rb', line 23

def date
  @date
end

#descriptionString (readonly)

The first line of this posting

Returns:

  • (String)

    the current value of description



23
24
25
# File 'lib/rvgp/journal/posting.rb', line 23

def description
  @description
end

#line_numberInteger (readonly)

The line number, in a journal, that this posting was declared at.

Returns:

  • (Integer)

    the current value of line_number



23
24
25
# File 'lib/rvgp/journal/posting.rb', line 23

def line_number
  @line_number
end

#tagsArray<RVGP::Journal::Posting::Tag> (readonly)

An array of tags, that apply to this posting.

Returns:



23
24
25
# File 'lib/rvgp/journal/posting.rb', line 23

def tags
  @tags
end

#transfersArray<RVGP::Journal::Posting::Transfer> (readonly)

An array of transfers, that apply to this posting.

Returns:



23
24
25
# File 'lib/rvgp/journal/posting.rb', line 23

def transfers
  @transfers
end

Instance Method Details

#to_ledgerString

Serializes this posting into a string, in the form that would be found in a PTA journal

Returns:

  • (String)

    The PTA journal representation of this posting



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/rvgp/journal/posting.rb', line 108

def to_ledger
  max_to_length = transfers.map do |transfer|
    transfer.commodity || transfer.complex_commodity ? transfer..length : 0
  end.max

  lines = [[date, description].join(' ')]
  lines.insert(lines.length > 1 ? -2 : 1, format('  ; %s', tags.join(', '))) if tags && !tags.empty?
  lines += transfers.map do |transfer|
    [
      if transfer.commodity || transfer.complex_commodity
        format("  %<account>-#{max_to_length}s    %<commodity>s",
               account: transfer.,
               commodity: (transfer.commodity || transfer.complex_commodity).to_s)
      else
        format('  %s', transfer.)
      end,
      transfer.tags && !transfer.tags.empty? ? transfer.tags.map { |tag| format('  ; %s', tag) } : nil
    ].compact.flatten.join("\n")
  end
  lines.join("\n")
end

#valid?TrueClass, FalseClass

Indicates whether or not this instance contains all required fields

Returns:

  • (TrueClass, FalseClass)

    whether or not we’re valid



101
102
103
104
# File 'lib/rvgp/journal/posting.rb', line 101

def valid?
  # Required fields:
  [date, description, transfers, transfers.any? { |t| t. && (t.commodity || t.complex_commodity) }].all?
end