Module: UsageCredits::CreditCalculator

Defined in:
lib/usage_credits/helpers/credit_calculator.rb

Overview

Centralized credit calculations used throughout the gem. This ensures consistent rounding and credit math everywhere.

Class Method Summary collapse

Class Method Details

.apply_rounding(amount) ⇒ Object

Apply the configured rounding strategy to a credit amount Always defaults to ceiling to ensure we never undercharge



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/usage_credits/helpers/credit_calculator.rb', line 11

def apply_rounding(amount)
  case UsageCredits.configuration.rounding_strategy
  when :round
    amount.round
  when :floor
    amount.floor
  when :ceil
    amount.ceil
  else
    amount.ceil # Default to ceiling to never undercharge
  end
end

.credits_to_money(credits, exchange_rate) ⇒ Object

Convert credits to a monetary amount



30
31
32
# File 'lib/usage_credits/helpers/credit_calculator.rb', line 30

def credits_to_money(credits, exchange_rate)
  apply_rounding(credits * 100.0 / exchange_rate)
end

.money_to_credits(cents, exchange_rate) ⇒ Object

Convert a monetary amount to credits



25
26
27
# File 'lib/usage_credits/helpers/credit_calculator.rb', line 25

def money_to_credits(cents, exchange_rate)
  apply_rounding(cents * exchange_rate / 100.0)
end