Module: ActsAsCalculator::Apportionment
- Defined in:
- lib/acts_as_calculator/apportionment.rb
Defined Under Namespace
Classes: Share
Constant Summary collapse
- DEFAULT_PRECISION =
2- PROPORTIONAL =
lambda { |amount:, weights:, **| DivideProportionally.(amount:, weights:) }
- EQUAL =
lambda { |amount:, weights:, **| Array.new(weights.size) { amount / weights.size } }
- LARGEST_REMAINDER =
lambda { |amount:, weights:, precision:| DistributeRemainder.(amount:, weights:, precision:) }
Class Method Summary collapse
- .known_strategies ⇒ Object
- .register_strategy(name, strategy) ⇒ Object
- .split(amount:, among:, by: nil, strategy: :proportional, precision: DEFAULT_PRECISION) ⇒ Object
- .strategy(name) ⇒ Object
- .unregister_strategy(name) ⇒ Object
Class Method Details
.known_strategies ⇒ Object
44 45 46 |
# File 'lib/acts_as_calculator/apportionment.rb', line 44 def self.known_strategies registry.keys end |
.register_strategy(name, strategy) ⇒ Object
27 28 29 30 |
# File 'lib/acts_as_calculator/apportionment.rb', line 27 def self.register_strategy(name, strategy) registry[name.to_sym] = strategy self end |
.split(amount:, among:, by: nil, strategy: :proportional, precision: DEFAULT_PRECISION) ⇒ Object
23 24 25 |
# File 'lib/acts_as_calculator/apportionment.rb', line 23 def self.split(amount:, among:, by: nil, strategy: :proportional, precision: DEFAULT_PRECISION) ApportionAmount.(amount:, among:, by:, strategy:, precision:) end |
.strategy(name) ⇒ Object
37 38 39 40 41 42 |
# File 'lib/acts_as_calculator/apportionment.rb', line 37 def self.strategy(name) registry.fetch(name.to_sym) do raise UnknownStrategyError, "unknown apportionment strategy #{name.inspect} (known: #{known_strategies.join(", ")})" end end |
.unregister_strategy(name) ⇒ Object
32 33 34 35 |
# File 'lib/acts_as_calculator/apportionment.rb', line 32 def self.unregister_strategy(name) registry.delete(name.to_sym) self end |