Class: UsageCredits::Cost::Fixed

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

Overview

Fixed credit cost (e.g., always costs 10 credits)

Instance Attribute Summary collapse

Attributes inherited from Base

#amount

Instance Method Summary collapse

Methods inherited from Base

#+, #to_i

Constructor Details

#initialize(amount) ⇒ Fixed

Returns a new instance of Fixed.



9
10
11
12
# File 'lib/usage_credits/cost/fixed.rb', line 9

def initialize(amount)
  @amount = amount
  @period = nil  # Will default to 1.month in CreditSubscriptionPlan
end

Instance Attribute Details

#periodObject (readonly)

Returns the value of attribute period.



7
8
9
# File 'lib/usage_credits/cost/fixed.rb', line 7

def period
  @period
end

Instance Method Details

#calculate(params = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/usage_credits/cost/fixed.rb', line 14

def calculate(params = {})
  value = amount.is_a?(Proc) ? amount.call(params) : amount
  case value
  when UsageCredits::Cost::Fixed
    value.calculate(params)
  else
    validate_amount!(value)
    value.to_i
  end
end

#every(period = nil) ⇒ self

Set the recurring period for subscription plans

Parameters:

  • period (Symbol, ActiveSupport::Duration, nil) (defaults to: nil)

    The period (e.g., :month, 2.months, 15.days)

Returns:

  • (self)


28
29
30
31
# File 'lib/usage_credits/cost/fixed.rb', line 28

def every(period = nil)
  @period = period  # nil will default to 1.month in CreditSubscriptionPlan
  self
end