Class: RVGP::Journal::Posting
- Inherits:
-
Object
- Object
- RVGP::Journal::Posting
- 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
Instance Attribute Summary collapse
-
#date ⇒ Date
readonly
The date this posting occurred.
-
#description ⇒ String
readonly
The first line of this posting.
-
#line_number ⇒ Integer
readonly
The line number, in a journal, that this posting was declared at.
-
#tags ⇒ Array<RVGP::Journal::Posting::Tag>
readonly
An array of tags, that apply to this posting.
-
#transfers ⇒ Array<RVGP::Journal::Posting::Transfer>
readonly
An array of transfers, that apply to this posting.
Instance Method Summary collapse
-
#initialize(date, description, opts = {}) ⇒ Posting
constructor
Create a posting, from constituent parts.
-
#to_ledger ⇒ String
Serializes this posting into a string, in the form that would be found in a PTA journal.
-
#valid? ⇒ TrueClass, FalseClass
Indicates whether or not this instance contains all required fields.
Constructor Details
#initialize(date, description, opts = {}) ⇒ Posting
Create a posting, from constituent parts
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
#date ⇒ Date (readonly)
The date this posting occurred
23 24 25 |
# File 'lib/rvgp/journal/posting.rb', line 23 def date @date end |
#description ⇒ String (readonly)
The first line of this posting
23 24 25 |
# File 'lib/rvgp/journal/posting.rb', line 23 def description @description end |
#line_number ⇒ Integer (readonly)
The line number, in a journal, that this posting was declared at.
23 24 25 |
# File 'lib/rvgp/journal/posting.rb', line 23 def line_number @line_number end |
#tags ⇒ Array<RVGP::Journal::Posting::Tag> (readonly)
An array of tags, that apply to this posting.
23 24 25 |
# File 'lib/rvgp/journal/posting.rb', line 23 def @tags end |
#transfers ⇒ Array<RVGP::Journal::Posting::Transfer> (readonly)
An array of transfers, that apply to this posting.
23 24 25 |
# File 'lib/rvgp/journal/posting.rb', line 23 def transfers @transfers end |
Instance Method Details
#to_ledger ⇒ String
Serializes this posting into a string, in the form that would be found in a PTA journal
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.account.length : 0 end.max lines = [[date, description].join(' ')] lines.insert(lines.length > 1 ? -2 : 1, format(' ; %s', .join(', '))) if && !.empty? lines += transfers.map do |transfer| [ if transfer.commodity || transfer.complex_commodity format(" %<account>-#{max_to_length}s %<commodity>s", account: transfer.account, commodity: (transfer.commodity || transfer.complex_commodity).to_s) else format(' %s', transfer.account) end, transfer. && !transfer..empty? ? transfer..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
101 102 103 104 |
# File 'lib/rvgp/journal/posting.rb', line 101 def valid? # Required fields: [date, description, transfers, transfers.any? { |t| t.account && (t.commodity || t.complex_commodity) }].all? end |