Class: MudratProjector::Projector
- Inherits:
-
Object
- Object
- MudratProjector::Projector
- 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
-
#from ⇒ Object
readonly
Returns the value of attribute from.
Instance Method Summary collapse
- #account_exists?(account_id) ⇒ Boolean
- #accounts=(accounts) ⇒ Object
- #add_account(account_id, **params) ⇒ Object
- #add_transaction(params) ⇒ Object
- #balanced? ⇒ Boolean
-
#initialize(params = {}) ⇒ Projector
constructor
A new instance of Projector.
- #net_worth ⇒ Object
- #project(to: end_of_projection, build_next: false, &block) ⇒ Object
- #transactions=(transactions) ⇒ Object
Methods included from BankerRounding
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
#from ⇒ Object (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
30 31 32 |
# File 'lib/mudrat_projector/projector.rb', line 30 def account_exists? account_id @chart.exists? account_id end |
#accounts=(accounts) ⇒ Object
24 25 26 27 28 |
# File 'lib/mudrat_projector/projector.rb', line 24 def accounts= accounts accounts.each do |account_id, params| add_account account_id, params end end |
#add_account(account_id, **params) ⇒ Object
34 35 36 37 |
# File 'lib/mudrat_projector/projector.rb', line 34 def add_account account_id, **params validate_account! account_id, params @chart.add_account account_id, 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
50 51 52 |
# File 'lib/mudrat_projector/projector.rb', line 50 def balanced? balance.zero? end |
#net_worth ⇒ Object
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 |