Sha256: 4c65befe02f5a0435fc5ab3ef2f6b95addd0afe3fa55a90a1a3a112f1760f0e1
Contents?: true
Size: 1.38 KB
Versions: 1
Compression:
Stored size: 1.38 KB
Contents
# frozen_string_literal: true # # Copyright (c) 2019-present, Blue Marble Payroll, LLC # # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. # module Proforma class PrawnRenderer module Util # This class can balance out an un-even set of values. class BalancedSet class << self def calculate(values, sum, round: 2) free_spots = values.count(&:nil?) if free_spots.positive? available_value = sum - values.map(&:to_f).reduce(0, :+) new_values = divide(free_spots, available_value, round) values = values.map { |v| v || new_values.shift } end balance(values, sum, round) end private def balance(values, sum, round) difference = sum - values.map(&:to_f).reduce(0, :+) return values if difference.zero? diff_spots = divide(values.length, difference, round) values.map.with_index { |value, index| value + diff_spots[index] } end def divide(count, sum, round) spot_value = (sum / count.to_f).round(round) remainder = sum - (spot_value * count) Array.new(count) { |i| i.zero? ? spot_value + remainder : spot_value } end end end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
proforma-prawn-renderer-1.1.0 | lib/proforma/prawn_renderer/util/balanced_set.rb |