Class: VertexClient::Utils::AdjustmentAllocator

Inherits:
Object
  • Object
show all
Defined in:
lib/vertex_client/utils/adjustment_allocator.rb

Instance Method Summary collapse

Constructor Details

#initialize(adjustment, weights) ⇒ AdjustmentAllocator

Returns a new instance of AdjustmentAllocator.



4
5
6
7
# File 'lib/vertex_client/utils/adjustment_allocator.rb', line 4

def initialize(adjustment, weights)
  @adjustment = adjustment
  @weights    = weights
end

Instance Method Details

#allocateObject



9
10
11
12
13
14
15
16
# File 'lib/vertex_client/utils/adjustment_allocator.rb', line 9

def allocate
  return [] if weights.empty? && @adjustment.zero?
  validate!
  allocations, remainders = allocations_with_remainders.transpose
  remaining_adjustment    = adjustment_in_cents - allocations.sum
  deserving_indices       = remainders.each_with_index.sort_by { |remainder, i| [remainder, i] }.map { |_r, i| i }.last(remaining_adjustment)
  allocations.each_with_index.map { |allocation, i| deserving_indices.include?(i) ? cents_to_dollars(allocation + 1) : cents_to_dollars(allocation) }
end