Class: RVGP::Fakers::FakeJournal

Inherits:
Faker::Base
  • Object
show all
Extended by:
FakerHelpers
Defined in:
lib/rvgp/fakers/fake_journal.rb

Overview

Contains faker implementation(s) that produce pta journals

Class Method Summary collapse

Class Method Details

.basic_cash(from: ::Date.today, to: from + 9, sum: '$ 100.00'.to_commodity, post_count: 10, postings: []) ⇒ RVGP::Journal

Generates a basic journal, that credits/debits from a Cash account

Parameters:

  • from (Date) (defaults to: ::Date.today)

    The date to start generated postings from

  • to (Date) (defaults to: from + 9)

    The date to end generated postings

  • sum (RVGP::Journal::Commodity) (defaults to: '$ 100.00'.to_commodity)

    The amount that all postings in the generated journal, will add up to

  • post_count (Numeric) (defaults to: 10)

    The number of postings to generate, in this journal

  • postings (Array) (defaults to: [])

    An array of RVGP::Journal::Posting objects, for inclusion in the output

Returns:

  • (RVGP::Journal)

    A fake journal, conforming to the provided params

Raises:

  • (StandardError)


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rvgp/fakers/fake_journal.rb', line 23

def basic_cash(from: ::Date.today,
               to: from + 9,
               sum: '$ 100.00'.to_commodity,
               post_count: 10,
               postings: [])

  raise StandardError unless sum.is_a?(RVGP::Journal::Commodity)

  amount_increment = (sum / post_count).floor sum.precision
  running_sum = nil

  generated_postings = entries_over_date_range(from, to, post_count).map.with_index do |date, i|
    post_amount = i + 1 == post_count ? (sum - running_sum) : amount_increment

    running_sum = running_sum.nil? ? post_amount : (running_sum + post_amount)

    simple_posting date, post_amount
  end
  RVGP::Journal.new((postings + generated_postings).sort_by(&:date))
end