Class: UsageCredits::Cost::Compound

Inherits:
Base
  • Object
show all
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

Attributes inherited from Base

#amount

Instance Method Summary collapse

Methods inherited from Base

#to_i

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

#costsObject (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