Module: Solace::Tokens
- Defined in:
- lib/solace/tokens.rb,
lib/solace/tokens/token.rb
Overview
Represents a Solana token with its metadata.
A tokens object encapsulates the symbol and associated metadata for a Solana token. It provides dynamic access to metadata attributes via method calls. This class is used within the Solace::Tokens module to represent individual tokens loaded from a YAML configuration file.
Defined Under Namespace
Classes: Token
Class Method Summary collapse
-
.all ⇒ Array<Solace::Tokens::Token>
Get all loaded tokens.
-
.clear! ⇒ void
Clear loaded tokens.
-
.fetch(symbol) ⇒ Solace::Tokens::Token?
Fetch a token by its symbol.
-
.load(path:, network:) ⇒ void
Load tokens from a YAML file for a specific network.
-
.where(criteria = {}) ⇒ Array<Solace::Tokens::Token>
Query tokens based on criteria.
Class Method Details
.all ⇒ Array<Solace::Tokens::Token>
Get all loaded tokens
60 61 62 |
# File 'lib/solace/tokens.rb', line 60 def self.all @registry.values end |
.clear! ⇒ void
This method returns an undefined value.
Clear loaded tokens
52 53 54 55 |
# File 'lib/solace/tokens.rb', line 52 def self.clear! (constants - [:Token]).each { |c| remove_const(c) } @registry = {} end |
.fetch(symbol) ⇒ Solace::Tokens::Token?
Fetch a token by its symbol
68 69 70 |
# File 'lib/solace/tokens.rb', line 68 def self.fetch(symbol) @registry[symbol.to_s] end |
.load(path:, network:) ⇒ void
This method returns an undefined value.
Load tokens from a YAML file for a specific network
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/solace/tokens.rb', line 31 def self.load(path:, network:) data = YAML.load_file(path) tokens = data.fetch(network.to_s) do raise ArgumentError, "Network '#{network}' not found in config" end # Clear previous constants and registry clear! @registry = {} tokens.each do |symbol, attrs| token = Solace::Tokens::Token.new(symbol, attrs) const_set(symbol, token) @registry[symbol.to_s] = token end end |
.where(criteria = {}) ⇒ Array<Solace::Tokens::Token>
Query tokens based on criteria
76 77 78 79 80 81 82 83 84 |
# File 'lib/solace/tokens.rb', line 76 def self.where(criteria = {}) return all if criteria.empty? normalized = criteria.transform_keys(&:to_sym) all.select do |token| normalized.all? { |k, v| token.[k] == v } end end |