Class: MudratProjector::Projector

Inherits:
Object
  • Object
show all
Extended by:
Forwardable, BankerRounding
Defined in:
lib/mudrat_projector/projector.rb

Constant Summary collapse

AccountDoesNotExist =
Class.new StandardError
AccountExists =
Class.new ArgumentError
BalanceError =
Class.new StandardError
InvalidAccount =
Class.new ArgumentError
InvalidTransaction =
Class.new StandardError

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from BankerRounding

with_banker_rounding

Constructor Details

#initialize(params = {}) ⇒ Projector

Returns a new instance of Projector.



14
15
16
17
18
19
# File 'lib/mudrat_projector/projector.rb', line 14

def initialize params = {}
  @chart        = params.fetch :chart, ChartOfAccounts.new
  @from         = params.fetch :from, ABSOLUTE_START
  @transactions = []
  @validator    = Validator.new projector: self, chart: @chart
end

Instance Attribute Details

#fromObject (readonly)

Returns the value of attribute from.



12
13
14
# File 'lib/mudrat_projector/projector.rb', line 12

def from
  @from
end

Instance Method Details

#account_exists?(account_id) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/mudrat_projector/projector.rb', line 30

def  
  @chart.exists? 
end

#accounts=(accounts) ⇒ Object



24
25
26
27
28
# File 'lib/mudrat_projector/projector.rb', line 24

def accounts= accounts
  accounts.each do |, params|
     , params
  end
end

#add_account(account_id, **params) ⇒ Object



34
35
36
37
# File 'lib/mudrat_projector/projector.rb', line 34

def  , **params
  validate_account! , params
  @chart. , params
end

#add_transaction(params) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/mudrat_projector/projector.rb', line 39

def add_transaction params
  if params.is_a? Transaction
    transaction = params
  else
    klass = params.has_key?(:schedule) ? ScheduledTransaction : Transaction
    transaction = klass.new params
    validate_transaction! transaction
  end
  @transactions.push transaction
end

#balanced?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/mudrat_projector/projector.rb', line 50

def balanced?
  balance.zero?
end

#net_worthObject



54
55
56
# File 'lib/mudrat_projector/projector.rb', line 54

def net_worth
  @chart.net_worth.round(2).to_f
end

#project(to: end_of_projection, build_next: false, &block) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/mudrat_projector/projector.rb', line 58

def project to: end_of_projection, build_next: false, &block
  must_be_balanced!
  projection = Projection.new range: (from..to), chart: @chart
  handler = TransactionHandler.new projection: projection
  if build_next
    handler.next_projector = self.class.new from: to + 1, chart: @chart
  end
  @transactions.each do |transaction| handler << transaction; end
  projection.project! &block
  handler.next_projector
end

#transactions=(transactions) ⇒ Object



70
71
72
# File 'lib/mudrat_projector/projector.rb', line 70

def transactions= transactions
  transactions.each do |transaction| add_transaction transaction; end
end