class Commissioner::Calculator::Operator

Constants

OPERATIONS

Attributes

amount[R]
commission[R]
exchange_commission[R]
exchange_fee[R]
exchange_rate[R]
exchanger[R]
fee[R]
from[R]
to[R]

Public Class Methods

new(params) click to toggle source
# File lib/commissioner/calculator.rb, line 21
def initialize(params)
  @amount = params[:amount]
  @commission = params[:commission]
  @exchange_commission = params[:exchange_commission]
  @exchanger = params[:exchanger]
  @rounding_mode = params[:rounding_mode]
  @commission_action = params[:commission_action]
  @from = params[:from_currency]
  @to = params[:to_currency]
end

Public Instance Methods

apply_commission(commission, type) click to toggle source
# File lib/commissioner/calculator.rb, line 38
def apply_commission(commission, type)
  return 0 unless commission

  if @commission_action == :reduce
    fee = round(@amount.to_f * commission / 100, @amount.currency.to_s)
    @amount -= fee
  else
    fee = round(@amount.to_f * commission / (100 - commission), @amount.currency.to_s)
    @amount += fee
  end

  case type
  when :exchange
    @exchange_fee = fee
  else
    @fee = fee
  end
end
apply_order(order) click to toggle source
# File lib/commissioner/calculator.rb, line 32
def apply_order(order)
  order.each do |operation|
    OPERATIONS[operation]&.call(self)
  end
end
exchange() click to toggle source
# File lib/commissioner/calculator.rb, line 63
def exchange
  @amount, @exchange_rate = exchanger.call(from, to, @amount)
end
round(decimal, currency) click to toggle source
# File lib/commissioner/calculator.rb, line 57
def round(decimal, currency)
  Money.with_rounding_mode(@rounding_mode) do
    Money.from_amount(decimal, currency)
  end
end