Module: Ask::TokenUsage::Rails::HasTokenWallet

Extended by:
ActiveSupport::Concern
Defined in:
lib/ask/token_usage/rails/concerns/has_token_wallet.rb

Overview

Include this concern in any ActiveRecord model to give it a token wallet backed by ask-token-usage-rails.

class User < ApplicationRecord
has_token_wallet
end

Adds:

user.token_balance        # Integer
user.token_wallet         # Ask::TokenUsage::TokenWallet (lazy-created)
user.token_transactions   # AR scope for the ledger
user.grant_tokens!(...)   # convenience
user.spend_tokens!(...)   # convenience
user.spend_tokens_on!(...)# convenience (activity name + block)

Instance Method Summary collapse

Instance Method Details

#ask_token_walletObject

Build an Ask::TokenUsage::Wallet PORO backed by this model's AR wallet — use for one-off operations outside the concern API.



46
47
48
# File 'lib/ask/token_usage/rails/concerns/has_token_wallet.rb', line 46

def ask_token_wallet
  Ask::TokenUsage.wallet_for(self)
end

#deduct_tokens!(amount, reason:, metadata: {}) ⇒ Object

Deduct amount tokens, raising InsufficientTokens on failure.



56
57
58
# File 'lib/ask/token_usage/rails/concerns/has_token_wallet.rb', line 56

def deduct_tokens!(amount, reason:, metadata: {})
  ask_token_wallet.deduct!(amount, reason: reason, metadata: )
end

#ensure_token_wallet!Object



35
36
37
38
# File 'lib/ask/token_usage/rails/concerns/has_token_wallet.rb', line 35

def ensure_token_wallet!
  # Force wallet row creation by probing balance through the AR store
  Ask::TokenUsage.wallet_for(self).balance
end

#grant_tokens!(amount, reason:, expires_at: nil, metadata: {}) ⇒ Object

Grant amount tokens.



51
52
53
# File 'lib/ask/token_usage/rails/concerns/has_token_wallet.rb', line 51

def grant_tokens!(amount, reason:, expires_at: nil, metadata: {})
  ask_token_wallet.grant!(amount, reason: reason, expires_at: expires_at, metadata: )
end

#has_tokens_for?(amount) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/ask/token_usage/rails/concerns/has_token_wallet.rb', line 79

def has_tokens_for?(amount)
  ask_token_wallet.has?(amount)
end

#spend_tokens!(amount, reason:, metadata: {}, &block) ⇒ Object

Spend an explicit amount of tokens, charging only on block success.



75
76
77
# File 'lib/ask/token_usage/rails/concerns/has_token_wallet.rb', line 75

def spend_tokens!(amount, reason:, metadata: {}, &block)
  ask_token_wallet.deduct!(amount, reason: reason, metadata: , &block)
end

#spend_tokens_on!(activity_name, params = {}, &block) ⇒ Object

Spend the estimated cost of activity with params, charging only when the block succeeds.



70
71
72
# File 'lib/ask/token_usage/rails/concerns/has_token_wallet.rb', line 70

def spend_tokens_on!(activity_name, params = {}, &block)
  ask_token_wallet.spend!(activity_name, params, &block)
end

#token_balanceObject



40
41
42
# File 'lib/ask/token_usage/rails/concerns/has_token_wallet.rb', line 40

def token_balance
  Ask::TokenUsage.wallet_for(self).balance
end

#token_usage_since(time) ⇒ Object



83
84
85
# File 'lib/ask/token_usage/rails/concerns/has_token_wallet.rb', line 83

def token_usage_since(time)
  ask_token_wallet.used_since(time)
end

#try_deduct_tokens!(amount, reason:, metadata: {}) ⇒ Object

Deduct amount tokens; returns false instead of raising on insufficient balance.



62
63
64
65
66
# File 'lib/ask/token_usage/rails/concerns/has_token_wallet.rb', line 62

def try_deduct_tokens!(amount, reason:, metadata: {})
  deduct_tokens!(amount, reason: reason, metadata: )
rescue Ask::TokenUsage::InsufficientTokens
  false
end