Class: UsageCredits::Cost::Compound
- Defined in:
- lib/usage_credits/cost/compound.rb
Overview
Compound credit cost that adds multiple costs together Example: base cost + per MB cost
Instance Attribute Summary collapse
-
#costs ⇒ Object
readonly
Returns the value of attribute costs.
Attributes inherited from Base
Instance Method Summary collapse
- #+(other) ⇒ Object
- #calculate(params = {}) ⇒ Object
-
#initialize(*costs) ⇒ Compound
constructor
A new instance of Compound.
Methods inherited from Base
Constructor Details
#initialize(*costs) ⇒ Compound
Returns a new instance of Compound.
10 11 12 |
# File 'lib/usage_credits/cost/compound.rb', line 10 def initialize(*costs) @costs = costs.flatten end |
Instance Attribute Details
#costs ⇒ Object (readonly)
Returns the value of attribute costs.
8 9 10 |
# File 'lib/usage_credits/cost/compound.rb', line 8 def costs @costs end |
Instance Method Details
#+(other) ⇒ Object
19 20 21 22 23 24 25 26 |
# File 'lib/usage_credits/cost/compound.rb', line 19 def +(other) case other when Fixed, Variable self.class.new(costs + [other]) else raise ArgumentError, "Cannot add #{other.class} to #{self.class}" end end |
#calculate(params = {}) ⇒ Object
14 15 16 17 |
# File 'lib/usage_credits/cost/compound.rb', line 14 def calculate(params = {}) total = costs.sum { |cost| cost.calculate(params) } CreditCalculator.apply_rounding(total) end |