Class: MudratProjector::ScheduledTransaction

Inherits:
Transaction
  • Object
show all
Defined in:
lib/mudrat_projector/scheduled_transaction.rb

Instance Attribute Summary collapse

Attributes inherited from Transaction

#credits, #date, #debits

Instance Method Summary collapse

Methods inherited from Transaction

#balanced?, #each

Constructor Details

#initialize(params = {}) ⇒ ScheduledTransaction

Returns a new instance of ScheduledTransaction.



5
6
7
8
# File 'lib/mudrat_projector/scheduled_transaction.rb', line 5

def initialize params = {}
  @schedule = Schedule.new params.fetch :schedule
  super params
end

Instance Attribute Details

#scheduleObject (readonly)

Returns the value of attribute schedule.



3
4
5
# File 'lib/mudrat_projector/scheduled_transaction.rb', line 3

def schedule
  @schedule
end

Instance Method Details

#build_transactions(intervals) ⇒ Object



10
11
12
13
14
# File 'lib/mudrat_projector/scheduled_transaction.rb', line 10

def build_transactions intervals
  intervals.map do |date, prorate|
    clone_transaction new_date: date, multiplier: prorate
  end
end

#clone_transaction(new_date: date, new_schedule: nil, multiplier: 1) ⇒ Object



16
17
18
19
20
21
# File 'lib/mudrat_projector/scheduled_transaction.rb', line 16

def clone_transaction new_date: date, new_schedule: nil, multiplier: 1
  params = build_cloned_tranasction_params new_date, multiplier
  return Transaction.new(params) if new_schedule.nil?
  params[:schedule] = new_schedule
  ScheduledTransaction.new params
end

#multiply(entries, by: 1) ⇒ Object



23
24
25
# File 'lib/mudrat_projector/scheduled_transaction.rb', line 23

def multiply entries, by: 1
  entries.map do |entry| entry * by; end
end

#slice(slice_date) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/mudrat_projector/scheduled_transaction.rb', line 27

def slice slice_date
  intervals, next_schedule = schedule.slice(date..slice_date)
  if next_schedule
    leftover = clone_transaction new_date: slice_date + 1, new_schedule: next_schedule
  end
  [build_transactions(intervals), leftover]
end