Module: Ask::Tokens::Rails::HasTokenWallet
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/ask/tokens/rails/concerns/has_token_wallet.rb
Overview
Include this concern in any ActiveRecord model to give it a token wallet backed by ask-tokens-rails.
class User < ApplicationRecord
has_token_wallet
end
Adds:
user.token_balance # Integer
user.token_wallet # Ask::Tokens::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
-
#ask_token_wallet ⇒ Object
Build an Ask::Tokens::Wallet PORO backed by this model's AR wallet — use for one-off operations outside the concern API.
-
#deduct_tokens!(amount, reason:, metadata: {}) ⇒ Object
Deduct
amounttokens, raising InsufficientTokens on failure. - #ensure_token_wallet! ⇒ Object
-
#grant_tokens!(amount, reason:, expires_at: nil, metadata: {}) ⇒ Object
Grant
amounttokens. - #has_tokens_for?(amount) ⇒ Boolean
-
#spend_tokens!(amount, reason:, metadata: {}, &block) ⇒ Object
Spend an explicit
amountof tokens, charging only on block success. -
#spend_tokens_on!(activity_name, params = {}, &block) ⇒ Object
Spend the estimated cost of
activitywithparams, charging only when the block succeeds. - #token_balance ⇒ Object
- #tokens_since(time) ⇒ Object
-
#try_deduct_tokens!(amount, reason:, metadata: {}) ⇒ Object
Deduct
amounttokens; returns false instead of raising on insufficient balance.
Instance Method Details
#ask_token_wallet ⇒ Object
Build an Ask::Tokens::Wallet PORO backed by this model's AR wallet — use for one-off operations outside the concern API.
46 47 48 |
# File 'lib/ask/tokens/rails/concerns/has_token_wallet.rb', line 46 def ask_token_wallet Ask::Tokens.wallet_for(self) end |
#deduct_tokens!(amount, reason:, metadata: {}) ⇒ Object
Deduct amount tokens, raising InsufficientTokens on failure.
56 57 58 |
# File 'lib/ask/tokens/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/tokens/rails/concerns/has_token_wallet.rb', line 35 def ensure_token_wallet! # Force wallet row creation by probing balance through the AR store Ask::Tokens.wallet_for(self).balance end |
#grant_tokens!(amount, reason:, expires_at: nil, metadata: {}) ⇒ Object
Grant amount tokens.
51 52 53 |
# File 'lib/ask/tokens/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
79 80 81 |
# File 'lib/ask/tokens/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/tokens/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/tokens/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_balance ⇒ Object
40 41 42 |
# File 'lib/ask/tokens/rails/concerns/has_token_wallet.rb', line 40 def token_balance Ask::Tokens.wallet_for(self).balance end |
#tokens_since(time) ⇒ Object
83 84 85 |
# File 'lib/ask/tokens/rails/concerns/has_token_wallet.rb', line 83 def tokens_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/tokens/rails/concerns/has_token_wallet.rb', line 62 def try_deduct_tokens!(amount, reason:, metadata: {}) deduct_tokens!(amount, reason: reason, metadata: ) rescue Ask::Tokens::InsufficientTokens false end |