class Fontist::Indexes::BaseFontCollectionIndex
Base class for font collection indexes
Provides common functionality for managing font indexes using SystemIndexFontCollection with different path loaders.
Subclasses must implement:
-
index_path: Returns the path where the index file should be stored
-
font_paths: Returns an array of font file paths to index
## Singleton Pattern
All index classes use Singleton to ensure consistent state across the application and enable caching.
Public Class Methods
Source
# File lib/fontist/indexes/base_font_collection_index.rb, line 29 def initialize # Don't initialize collection here - use lazy initialization via collection method end
Source
# File lib/fontist/indexes/base_font_collection_index.rb, line 25 def self.reset_cache instance.reset_cache end
Class method to reset the singleton instance cache
@return [void]
Public Instance Methods
Source
# File lib/fontist/indexes/base_font_collection_index.rb, line 87 def add_font(_path) # Reset verification flag to force re-check collection.reset_verification! # Force rebuild to include new font collection.build(forced: true, verbose: false) end
Adds a font to the index
Triggers index rebuild to ensure font is included
@param path [String] Full path to installed font file @return [void]
Source
# File lib/fontist/indexes/base_font_collection_index.rb, line 48 def collection @collection ||= SystemIndexFontCollection.from_file( path: index_path, paths_loader: -> { font_paths }, ) end
Get the collection, initializing it lazily if nil
Uses lazy initialization so that the collection is created with fresh font_paths each time it’s accessed after reset_cache
@return [SystemIndexFontCollection] The collection object
Source
# File lib/fontist/indexes/base_font_collection_index.rb, line 38 def find(font_name, style = nil) collection.find(font_name, style) end
Finds fonts by name and optional style
@param font_name [String] Font family name @param style [String, nil] Optional style (e.g., “Regular”, “Bold”) @return [Array<SystemIndexFont>, nil] Found fonts or nil
Source
# File lib/fontist/indexes/base_font_collection_index.rb, line 75 def font_exists?(path) return false if collection.fonts.nil? collection.fonts.any? { |f| f.path == path } end
Checks if a font exists at the given path
@param path [String] Full path to font file @return [Boolean] true if font exists in index
Source
# File lib/fontist/indexes/base_font_collection_index.rb, line 67 def fonts collection.fonts || [] end
Returns all fonts in the index
@return [Array<SystemIndexFont>] All indexed fonts
Source
# File lib/fontist/indexes/base_font_collection_index.rb, line 59 def read_only_mode collection.read_only_mode self end
Enable read-only mode for operations that don’t need index rebuilding This is used during manifest compilation to avoid expensive index checks
@return [self] Returns self for chaining
Source
# File lib/fontist/indexes/base_font_collection_index.rb, line 114 def rebuild(verbose: false) collection.rebuild(verbose: verbose) end
Rebuilds the entire index
Scans all font directories and updates the index file
@param verbose [Boolean] Show detailed progress information @return [void]
Source
# File lib/fontist/indexes/base_font_collection_index.rb, line 101 def remove_font(path) return if collection.fonts.nil? collection.fonts.delete_if { |f| f.path == path } collection.to_file(index_path) end
Removes a font from the index
Updates index file immediately
@param path [String] Full path to font file to remove @return [void]
Source
# File lib/fontist/indexes/base_font_collection_index.rb, line 124 def reset_cache @collection = nil end
Resets the singleton instance cache
Sets @collection to nil so that it will be re-initialized with fresh font_paths on next access via the collection method
@return [void]