Module: Apportion

Includes:
Setup, Util
Defined in:
lib/apportion.rb,
lib/apportion/util.rb,
lib/apportion/setup.rb,
lib/apportion/version.rb,
lib/apportion/algorithm/equal_proportions.rb

Overview

Top level interface, or API, to distribute a quantity among recipients

Defined Under Namespace

Modules: Algorithm, Setup, Util

Constant Summary collapse

VERSION =
'0.0.1'

Class Method Summary collapse

Methods included from Util

file_read_json_to_hash, hash_values_sum, symbolize_keys

Methods included from Setup

initial_portions, validate

Class Method Details

.division(weights, size, options = {}) ⇒ Hash

Distributes a quantity among recipients by dividing proportionally according to relative weights using the 'equal proportions' algorithm

Examples:

division({a: 41, b: 32, c: 27}, 7)
# => {a: 3, b: 2, c: 2}

Options Hash (options):

  • :prior_portions (Hash)

    from a previous apportionment

  • :required_minimum (Integer)

    smallest portion for each recipient


24
25
26
27
28
29
# File 'lib/apportion.rb', line 24

def division(weights, size, options = {})
  portions = initial_portions(weights.keys, options)
  sum_portions = hash_values_sum(portions)
  validate(size, sum_portions)
  next_step(weights, portions, sum_portions, size)
end