Class: ActsAsCalculator::ApportionAmount

Inherits:
Object
  • Object
show all
Defined in:
lib/acts_as_calculator/apportion_amount.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(amount:, among:, by: nil, strategy: :proportional, precision: Apportionment::DEFAULT_PRECISION) ⇒ ApportionAmount

Returns a new instance of ApportionAmount.



11
12
13
14
15
16
17
18
# File 'lib/acts_as_calculator/apportion_amount.rb', line 11

def initialize(amount:, among:, by: nil, strategy: :proportional,
               precision: Apportionment::DEFAULT_PRECISION)
  @amount = amount
  @among = among
  @by = by
  @strategy = strategy
  @precision = precision
end

Class Method Details

.callObject



7
8
9
# File 'lib/acts_as_calculator/apportion_amount.rb', line 7

def self.call(...)
  new(...).call
end

Instance Method Details

#callObject

Raises:



20
21
22
23
24
25
26
27
28
# File 'lib/acts_as_calculator/apportion_amount.rb', line 20

def call
  raise ApportionmentError, "cannot apportion among an empty collection" if members.empty?

  shares = Apportionment.strategy(strategy).call(amount: total, weights:, precision:)

  members.zip(weights, shares).map do |member, weight, share|
    Apportionment::Share.new(member:, weight:, amount: share)
  end
end