Class: Numeric

Inherits:
Object
  • Object
show all
Defined in:
lib/usage_credits/core_ext/numeric.rb

Overview

This is what allows us to write things like 3.credits in the DSL Then the actual cost gets calculated in the UsageCredits::Cost classes (Cost::Base, Cost::Fixed, Cost::Variable, Cost::Compound, etc.)

Instance Method Summary collapse

Instance Method Details

#centsObject Also known as: cent



46
47
48
# File 'lib/usage_credits/core_ext/numeric.rb', line 46

def cents
  self
end

#creditsObject Also known as: credit

Raises:

  • (ArgumentError)


9
10
11
12
13
# File 'lib/usage_credits/core_ext/numeric.rb', line 9

def credits
  raise ArgumentError, "Credit amount must be a whole number (decimals are not allowed)" unless self == self.to_i
  raise ArgumentError, "Credit amount cannot be negative" if self.negative?
  UsageCredits::Cost::Fixed.new(self.to_i)
end

#credits_per(unit) ⇒ Object Also known as: credit_per

Raises:

  • (ArgumentError)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/usage_credits/core_ext/numeric.rb', line 16

def credits_per(unit)
  raise ArgumentError, "Credit cost rate must be a whole number (decimals are not allowed)" unless self == self.to_i

  # Convert common units to their base unit
  unit = case unit.to_s.downcase
         when "mb", "megabyte", "megabytes"
           :mb
         when "kb", "kilobyte", "kilobytes"
           :kb
         when "gb", "gigabyte", "gigabytes"
           :gb
         when "unit", "units"
           :units
         else
           unit.to_sym
         end

  UsageCredits::Cost::Variable.new(self, unit)
end

#dollarsObject Also known as: dollar, euro, euros, pound, pounds



37
38
39
# File 'lib/usage_credits/core_ext/numeric.rb', line 37

def dollars
  self * 100 # Convert to cents for payment processors
end