Class: Fontist::Indexes::BaseFontCollectionIndex
- Inherits:
-
Object
- Object
- Fontist::Indexes::BaseFontCollectionIndex
- Includes:
- Singleton
- Defined in:
- lib/fontist/indexes/base_font_collection_index.rb
Overview
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.
Direct Known Subclasses
Class Method Summary collapse
-
.reset_cache ⇒ void
Class method to reset the singleton instance cache.
Instance Method Summary collapse
-
#add_font(_path) ⇒ void
Adds a font to the index.
-
#collection ⇒ SystemIndexFontCollection
Get the collection, initializing it lazily if nil.
-
#disable_read_only_mode ⇒ self
Disable read-only mode, restoring normal index_changed? checks.
-
#find(font_name, style = nil) ⇒ Array<SystemIndexFont>?
Finds fonts by name and optional style.
-
#font_exists?(path) ⇒ Boolean
Checks if a font exists at the given path.
-
#fonts ⇒ Array<SystemIndexFont>
Returns all fonts in the index.
-
#initialize ⇒ BaseFontCollectionIndex
constructor
A new instance of BaseFontCollectionIndex.
-
#read_only_mode ⇒ self
Enable read-only mode for operations that don't need index rebuilding This is used during manifest compilation to avoid expensive index checks.
-
#rebuild(verbose: false) ⇒ void
Rebuilds the entire index.
-
#remove_font(path) ⇒ void
Removes a font from the index.
-
#reset_cache ⇒ void
Resets the singleton instance cache.
Constructor Details
#initialize ⇒ BaseFontCollectionIndex
Returns a new instance of BaseFontCollectionIndex.
28 29 30 |
# File 'lib/fontist/indexes/base_font_collection_index.rb', line 28 def initialize # Don't initialize collection here - use lazy initialization via collection method end |
Class Method Details
.reset_cache ⇒ void
This method returns an undefined value.
Class method to reset the singleton instance cache
24 25 26 |
# File 'lib/fontist/indexes/base_font_collection_index.rb', line 24 def self.reset_cache instance.reset_cache end |
Instance Method Details
#add_font(_path) ⇒ void
This method returns an undefined value.
Adds a font to the index
Triggers index rebuild to ensure font is included
101 102 103 104 105 106 107 |
# File 'lib/fontist/indexes/base_font_collection_index.rb', line 101 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 |
#collection ⇒ SystemIndexFontCollection
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.
On cold start (no on-disk index file), eagerly build the index once
so that fonts reflects an authoritative scan. This lets callers in
read-only mode trust the cached fonts without re-running build on
every lookup.
52 53 54 55 56 57 58 59 |
# File 'lib/fontist/indexes/base_font_collection_index.rb', line 52 def collection @collection ||= SystemIndexFontCollection.from_file( path: index_path, paths_loader: -> { font_paths }, ).tap do |coll| coll.build unless File.exist?(index_path) end end |
#disable_read_only_mode ⇒ self
Disable read-only mode, restoring normal index_changed? checks.
73 74 75 76 |
# File 'lib/fontist/indexes/base_font_collection_index.rb', line 73 def disable_read_only_mode collection.disable_read_only_mode self end |
#find(font_name, style = nil) ⇒ Array<SystemIndexFont>?
Finds fonts by name and optional style
37 38 39 |
# File 'lib/fontist/indexes/base_font_collection_index.rb', line 37 def find(font_name, style = nil) collection.find(font_name, style) end |
#font_exists?(path) ⇒ Boolean
Checks if a font exists at the given path
89 90 91 92 93 |
# File 'lib/fontist/indexes/base_font_collection_index.rb', line 89 def font_exists?(path) return false if collection.fonts.nil? collection.fonts.any? { |f| f.path == path } end |
#fonts ⇒ Array<SystemIndexFont>
Returns all fonts in the index
81 82 83 |
# File 'lib/fontist/indexes/base_font_collection_index.rb', line 81 def fonts collection.fonts || [] end |
#read_only_mode ⇒ self
Enable read-only mode for operations that don't need index rebuilding This is used during manifest compilation to avoid expensive index checks
65 66 67 68 |
# File 'lib/fontist/indexes/base_font_collection_index.rb', line 65 def read_only_mode collection.read_only_mode self end |
#rebuild(verbose: false) ⇒ void
This method returns an undefined value.
Rebuilds the entire index
Scans all font directories and updates the index file
128 129 130 |
# File 'lib/fontist/indexes/base_font_collection_index.rb', line 128 def rebuild(verbose: false) collection.rebuild(verbose: verbose) end |
#remove_font(path) ⇒ void
This method returns an undefined value.
Removes a font from the index
Updates index file immediately
115 116 117 118 119 120 |
# File 'lib/fontist/indexes/base_font_collection_index.rb', line 115 def remove_font(path) return if collection.fonts.nil? collection.fonts.delete_if { |f| f.path == path } collection.to_file(index_path) end |
#reset_cache ⇒ void
This method returns an undefined value.
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
138 139 140 |
# File 'lib/fontist/indexes/base_font_collection_index.rb', line 138 def reset_cache @collection = nil end |