Class: MudratProjector::TransactionEntry

Inherits:
Object
  • Object
show all
Extended by:
Factory
Defined in:
lib/mudrat_projector/transaction_entry.rb

Direct Known Subclasses

PercentageTransactionEntry

Defined Under Namespace

Modules: Factory

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Factory

new, new_credit, new_debit

Constructor Details

#initialize(params = {}) ⇒ TransactionEntry

Returns a new instance of TransactionEntry.



46
47
48
49
50
51
# File 'lib/mudrat_projector/transaction_entry.rb', line 46

def initialize params = {}
  @account_id      = params.fetch :account_id
  @scalar          = params.fetch :scalar
  @credit_or_debit = params.fetch :credit_or_debit
  validate!
end

Instance Attribute Details

#account_idObject (readonly)

Returns the value of attribute account_id.



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

def 
  @account_id
end

#amountObject (readonly)

Returns the value of attribute amount.



44
45
46
# File 'lib/mudrat_projector/transaction_entry.rb', line 44

def amount
  @amount
end

Instance Method Details

#*(multiplier) ⇒ Object



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

def * multiplier
  return self if multiplier == 1
  self.class.new serialize.merge(scalar: scalar * multiplier)
end

#calculate(chart_of_accounts = nil) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/mudrat_projector/transaction_entry.rb', line 58

def calculate chart_of_accounts = nil
  @amount = calculate_amount chart_of_accounts
  return if chart_of_accounts.nil?
   = chart_of_accounts.fetch().type
  check = i(asset expense).include?() ? :debit? : :credit?
  @delta = send(check) ? amount : -amount
end

#calculate_amount(chart_of_accounts) ⇒ Object



66
67
68
# File 'lib/mudrat_projector/transaction_entry.rb', line 66

def calculate_amount chart_of_accounts
  @amount = scalar
end

#credit?Boolean

Returns:

  • (Boolean)


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

def credit?
  @credit_or_debit == :credit
end

#debit?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/mudrat_projector/transaction_entry.rb', line 74

def debit?
  @credit_or_debit == :debit
end

#inspectObject



78
79
80
# File 'lib/mudrat_projector/transaction_entry.rb', line 78

def inspect
  "#<#{self.class}: amount=#{fmt(scalar)}, account_id=#{account_id.inspect} type=#{@credit_or_debit.inspect}>"
end

#serializeObject



82
83
84
85
86
87
88
# File 'lib/mudrat_projector/transaction_entry.rb', line 82

def serialize
  {
    account_id:       ,
    scalar:           scalar,
    credit_or_debit:  @credit_or_debit,
  }
end

#validate!Object



90
91
92
93
94
95
96
97
# File 'lib/mudrat_projector/transaction_entry.rb', line 90

def validate!
  if scalar == 0
    raise ArgumentError, "You cannot supply a scalar of 0"
  end
  unless i(credit debit).include? @credit_or_debit
    raise ArgumentError, "Must supply :credit or :debit, not #{@credit_or_debit.inspect}"
  end
end