Sha256: 44700a2f239aa88b09aa5c5cd52061821a5a4705a69c2063d5719403a4e84c24

Contents?: true

Size: 361 Bytes

Versions: 12

Compression:

Stored size: 361 Bytes

Contents

class DivideByType
  def self.divide(ratios, amount)
    return {} if amount == 0
    ratios = { :no_division => 1 } if ratios == {}

    Hash[ ratios.each_key.zip(allocate(amount, ratios.each_value)) ]
  end

  private
  def self.allocate(amount, values)
    total = values.reduce(:+)
    amount.allocate( values.map { |amount| amount.to_d/total } )
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
guerrilla_patch-3.0.3 lib/guerrilla_patch/aggregate_by_type/divide_by_type.rb
guerrilla_patch-3.0.2 lib/guerrilla_patch/aggregate_by_type/divide_by_type.rb
guerrilla_patch-2.8.7 lib/guerrilla_patch/aggregate_by_type/divide_by_type.rb
guerrilla_patch-2.8.6 lib/guerrilla_patch/aggregate_by_type/divide_by_type.rb
guerrilla_patch-2.8.5 lib/guerrilla_patch/aggregate_by_type/divide_by_type.rb
guerrilla_patch-2.8.4 lib/guerrilla_patch/aggregate_by_type/divide_by_type.rb
guerrilla_patch-2.8.3 lib/guerrilla_patch/aggregate_by_type/divide_by_type.rb
guerrilla_patch-2.8.2 lib/guerrilla_patch/aggregate_by_type/divide_by_type.rb
guerrilla_patch-2.8.1 lib/guerrilla_patch/aggregate_by_type/divide_by_type.rb
guerrilla_patch-2.8.0 lib/guerrilla_patch/aggregate_by_type/divide_by_type.rb
guerrilla_patch-2.7.1 lib/guerrilla_patch/aggregate_by_type/divide_by_type.rb
guerrilla_patch-2.7.0 lib/guerrilla_patch/aggregate_by_type/divide_by_type.rb