Class: UsageCredits::Cost::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/usage_credits/cost/base.rb

Overview

Base class for all credit cost calculations. Subclasses implement different ways of calculating credit costs:

  • Fixed: Simple fixed amount

  • Variable: Amount based on units (MB, etc)

  • Compound: Multiple costs added together

Direct Known Subclasses

Compound, Fixed, Variable

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(amount) ⇒ Base

Returns a new instance of Base.



13
14
15
16
# File 'lib/usage_credits/cost/base.rb', line 13

def initialize(amount)
  validate_amount!(amount) unless amount.is_a?(Proc)
  @amount = amount
end

Instance Attribute Details

#amountObject (readonly)

Returns the value of attribute amount.



11
12
13
# File 'lib/usage_credits/cost/base.rb', line 11

def amount
  @amount
end

Instance Method Details

#+(other) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/usage_credits/cost/base.rb', line 18

def +(other)
  case other
  when Fixed, Variable
    Compound.new(self, other)
  else
    raise ArgumentError, "Cannot add #{other.class} to #{self.class}"
  end
end

#to_iObject



27
28
29
# File 'lib/usage_credits/cost/base.rb', line 27

def to_i
  calculate({})
end