Class: Kotoshu::Configuration
- Inherits:
-
Object
- Object
- Kotoshu::Configuration
- Defined in:
- lib/kotoshu/configuration.rb,
lib/kotoshu/configuration/builder.rb,
lib/kotoshu/configuration/resolver.rb
Overview
Configuration for Kotoshu spell checker.
This class manages configuration options for spell checking, including dictionary settings, suggestion limits, and language options.
Configuration priority: CLI > ENV > Programmatic > Defaults
Defined Under Namespace
Constant Summary collapse
- SCHEMA =
Configuration schema with ENV variable mappings.
Each key maps to a hash with:
-
:env - Environment variable name
-
:default - Default value (can be a proc for dynamic defaults)
-
:description - Human-readable description
-
:type - Expected type (for validation/conversion)
-
{ dictionary_path: { env: "KOTOSHU_DICTIONARIES_PATH", default: nil, description: "Path to dictionary file", type: String }, cache_path: { env: "KOTOSHU_CACHE_PATH", default: -> { default_cache_path }, description: "Path to cache directory (~/.cache/kotoshu)", type: String }, config_path: { env: "KOTOSHU_CONFIG_PATH", default: -> { default_config_path }, description: "Path to user config directory (~/.config/kotoshu)", type: String }, data_path: { env: "KOTOSHU_DATA_PATH", default: -> { default_data_path }, description: "Path to data directory (~/.local/share/kotoshu)", type: String }, dictionaries_url: { env: "KOTOSHU_DICTIONARIES_URL", default: "https://raw.githubusercontent.com/kotoshu/dictionaries/main", description: "Deprecated: use repos_base_url + dictionaries_pin via SourceRegistry", type: String }, models_url: { env: "KOTOSHU_MODELS_URL", default: "https://github.com/kotoshu/models-fasttext-onnx/raw/main", description: "Deprecated: use repos_base_url + models_pin via SourceRegistry", type: String }, repos_base_url: { env: "KOTOSHU_REPOS_BASE_URL", default: -> { Kotoshu::SourceRegistry::DEFAULT_BASE_URL }, description: "GitHub raw root for all kotoshu content repos", type: String }, dictionaries_pin: { env: "KOTOSHU_DICTIONARIES_PIN", default: "v1", description: "Branch/tag/commit pinned for kotoshu/dictionaries", type: String }, frequency_pin: { env: "KOTOSHU_FREQUENCY_PIN", default: "main", description: "Branch/tag/commit pinned for kotoshu/frequency-list-kelly", type: String }, models_pin: { env: "KOTOSHU_MODELS_PIN", default: "main", description: "Branch/tag/commit pinned for kotoshu/models-fasttext-onnx", type: String }, auto_download: { env: "KOTOSHU_AUTO_DOWNLOAD", default: true, description: "Automatically download missing dictionaries", type: :boolean }, cache_ttl: { env: "KOTOSHU_CACHE_TTL", default: 86_400, # 24 hours in seconds description: "Cache TTL in seconds", type: Integer }, max_cache_size: { env: "KOTOSHU_MAX_CACHE_SIZE", default: 1_073_741_824, # 1GB description: "Maximum cache size in bytes", type: Integer }, dictionary_type: { env: "KOTOSHU_DICTIONARY_TYPE", default: :unix_words, description: "Dictionary type (:unix_words, :plain_text, :hunspell, :cspell, :custom)", type: Symbol }, language: { env: "KOTOSHU_LANGUAGE", default: "en-US", description: "Language code (e.g., en-US, de-DE, ja-JP)", type: String }, locale: { env: "KOTOSHU_LOCALE", default: nil, description: "Locale (e.g., en, en_US, de_DE)", type: String }, max_suggestions: { env: "KOTOSHU_MAX_SUGGESTIONS", default: 10, description: "Maximum number of suggestions", type: Integer }, case_sensitive: { env: "KOTOSHU_CASE_SENSITIVE", default: false, description: "Enable case-sensitive lookups", type: :boolean }, verbose: { env: "KOTOSHU_VERBOSE", default: false, description: "Enable verbose output", type: :boolean }, encoding: { env: "KOTOSHU_ENCODING", default: "UTF-8", description: "Character encoding", type: String }, dictionaries_path: { env: "KOTOSHU_DICTIONARIES_PATH", default: nil, description: "Path to dictionaries directory (for grammar rules)", type: String }, offline: { env: "KOTOSHU_OFFLINE", default: false, description: "Use only cached resources; never download", type: :boolean }, resource_pin: { env: "KOTOSHU_RESOURCE_PIN", default: "main", description: "Branch/tag/commit pinned for resource downloads", type: String }, default_language: { env: "KOTOSHU_DEFAULT_LANGUAGE", default: "en", description: "Fallback language when detection is inconclusive", type: String } }.freeze
- DEFAULTS =
Default configuration values (legacy, for backward compatibility).
{ dictionary_path: nil, dictionary_type: :unix_words, language: "en-US", locale: nil, max_suggestions: 10, case_sensitive: false, verbose: false, suggestion_algorithms: nil, # Use defaults custom_words: [], encoding: "UTF-8", dictionaries_path: nil, # Path to dictionaries directory (for grammar rules) offline: false, default_language: "en", resource_pin: "main" }.freeze
Instance Attribute Summary collapse
-
#auto_download ⇒ Boolean
Whether to automatically download missing dictionaries.
-
#cache_path ⇒ String?
Path to cache directory.
-
#cache_ttl ⇒ Integer
Cache TTL in seconds.
-
#case_sensitive ⇒ Boolean
Whether lookups are case-sensitive.
-
#config_path ⇒ String?
Path to user config directory.
-
#custom_words ⇒ Array<String>
Custom words to add to dictionary.
-
#data_path ⇒ String?
Path to data directory (audit log, etc.).
-
#default_language ⇒ String
Fallback language when detection is inconclusive.
-
#dictionaries_path ⇒ String?
Path to dictionaries directory (for grammar rules).
-
#dictionaries_pin ⇒ String
Pin for kotoshu/dictionaries.
-
#dictionaries_url ⇒ String
Base URL for downloading dictionaries (deprecated).
-
#dictionary ⇒ Dictionary::Base
Load or get the dictionary.
-
#dictionary_path ⇒ String?
Path to the dictionary file.
-
#dictionary_type ⇒ Symbol
Dictionary type (:unix_words, :plain_text, :hunspell, :cspell, :custom).
-
#download_reporter ⇒ #start, ...
Optional progress reporter for downloads.
-
#encoding ⇒ String
Character encoding.
-
#frequency_pin ⇒ String
Pin for kotoshu/frequency-list-kelly.
-
#language ⇒ String
Language code (e.g., “en-US”, “en-GB”).
-
#locale ⇒ String?
Locale (e.g., “en”, “en_US”).
-
#max_cache_size ⇒ Integer
Maximum cache size in bytes.
-
#max_suggestions ⇒ Integer
Maximum number of suggestions to return.
-
#models_pin ⇒ String
Branch/tag/commit pinned for model downloads.
-
#models_url ⇒ String
Base URL for FastText ONNX models (deprecated).
-
#offline ⇒ Boolean
Whether to use only cached resources (no downloads).
-
#repos_base_url ⇒ String
GitHub raw root for all kotoshu content repos.
-
#resolver ⇒ Resolver
readonly
The configuration resolver.
-
#resource_pin ⇒ String
Branch/tag/commit pinned for resource downloads.
-
#suggestion_algorithms ⇒ Array<Class>?
Suggestion algorithms to use.
-
#verbose ⇒ Boolean
Whether to enable verbose output.
Class Method Summary collapse
-
.default ⇒ Configuration
Get the default configuration.
-
.default_cache_path ⇒ String
Get default cache path.
- .default_config_path ⇒ Object
- .default_data_path ⇒ Object
-
.instance ⇒ Configuration
Global configuration instance.
-
.reset ⇒ Configuration
Reset the global configuration.
Instance Method Summary collapse
-
#clone ⇒ Configuration
Clone the configuration.
-
#get(key) ⇒ Object
Get a configuration value using the resolver.
-
#initialize(*args, **kwargs, &block) {|_self| ... } ⇒ Configuration
constructor
Create a new configuration.
-
#key?(key) ⇒ Boolean
Check if a configuration key has a value set.
-
#load_dictionary ⇒ Dictionary::Base
Load the dictionary based on configuration.
-
#reset_dictionary ⇒ self
Reset the dictionary (force reload on next access).
-
#source_registry ⇒ Kotoshu::SourceRegistry
Build a SourceRegistry honoring this configuration’s base URL and per-repo pins.
-
#to_h ⇒ Hash
Convert to hash.
Constructor Details
#initialize(*args, **kwargs, &block) {|_self| ... } ⇒ Configuration
Create a new configuration.
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 |
# File 'lib/kotoshu/configuration.rb', line 322 def initialize(*args, **kwargs, &block) # Handle both positional hash and keyword arguments settings = args.first.is_a?(Hash) ? args.first : {} settings = settings.merge(kwargs) # Extract cli_options if provided = settings.delete(:cli_options) || {} # Build the resolver with settings as programmatic defaults @resolver = Resolver.new( env: settings[:env] || {}, programmatic: settings, cli: , defaults: DEFAULTS ) apply_defaults apply_resolver_values apply_explicit_settings(settings) yield self if block_given? end |
Instance Attribute Details
#auto_download ⇒ Boolean
Returns Whether to automatically download missing dictionaries.
288 289 290 |
# File 'lib/kotoshu/configuration.rb', line 288 def auto_download @auto_download end |
#cache_path ⇒ String?
Returns Path to cache directory.
255 256 257 |
# File 'lib/kotoshu/configuration.rb', line 255 def cache_path @cache_path end |
#cache_ttl ⇒ Integer
Returns Cache TTL in seconds.
291 292 293 |
# File 'lib/kotoshu/configuration.rb', line 291 def cache_ttl @cache_ttl end |
#case_sensitive ⇒ Boolean
Returns Whether lookups are case-sensitive.
225 226 227 |
# File 'lib/kotoshu/configuration.rb', line 225 def case_sensitive @case_sensitive end |
#config_path ⇒ String?
Returns Path to user config directory.
258 259 260 |
# File 'lib/kotoshu/configuration.rb', line 258 def config_path @config_path end |
#custom_words ⇒ Array<String>
Returns Custom words to add to dictionary.
234 235 236 |
# File 'lib/kotoshu/configuration.rb', line 234 def custom_words @custom_words end |
#data_path ⇒ String?
Returns Path to data directory (audit log, etc.).
261 262 263 |
# File 'lib/kotoshu/configuration.rb', line 261 def data_path @data_path end |
#default_language ⇒ String
Returns Fallback language when detection is inconclusive.
249 250 251 |
# File 'lib/kotoshu/configuration.rb', line 249 def default_language @default_language end |
#dictionaries_path ⇒ String?
Returns Path to dictionaries directory (for grammar rules).
243 244 245 |
# File 'lib/kotoshu/configuration.rb', line 243 def dictionaries_path @dictionaries_path end |
#dictionaries_pin ⇒ String
Returns Pin for kotoshu/dictionaries.
273 274 275 |
# File 'lib/kotoshu/configuration.rb', line 273 def dictionaries_pin @dictionaries_pin end |
#dictionaries_url ⇒ String
Returns Base URL for downloading dictionaries (deprecated).
264 265 266 |
# File 'lib/kotoshu/configuration.rb', line 264 def dictionaries_url @dictionaries_url end |
#dictionary ⇒ Dictionary::Base
Load or get the dictionary.
240 241 242 |
# File 'lib/kotoshu/configuration.rb', line 240 def dictionary @dictionary end |
#dictionary_path ⇒ String?
Returns Path to the dictionary file.
210 211 212 |
# File 'lib/kotoshu/configuration.rb', line 210 def dictionary_path @dictionary_path end |
#dictionary_type ⇒ Symbol
Returns Dictionary type (:unix_words, :plain_text, :hunspell, :cspell, :custom).
213 214 215 |
# File 'lib/kotoshu/configuration.rb', line 213 def dictionary_type @dictionary_type end |
#download_reporter ⇒ #start, ...
Returns Optional progress reporter for downloads. Typically set by the CLI (Cli::ProgressReporter) for human-facing setup runs; nil (silent) for programmatic API usage.
285 286 287 |
# File 'lib/kotoshu/configuration.rb', line 285 def download_reporter @download_reporter end |
#encoding ⇒ String
Returns Character encoding.
237 238 239 |
# File 'lib/kotoshu/configuration.rb', line 237 def encoding @encoding end |
#frequency_pin ⇒ String
Returns Pin for kotoshu/frequency-list-kelly.
276 277 278 |
# File 'lib/kotoshu/configuration.rb', line 276 def frequency_pin @frequency_pin end |
#language ⇒ String
Returns Language code (e.g., “en-US”, “en-GB”).
216 217 218 |
# File 'lib/kotoshu/configuration.rb', line 216 def language @language end |
#locale ⇒ String?
Returns Locale (e.g., “en”, “en_US”).
219 220 221 |
# File 'lib/kotoshu/configuration.rb', line 219 def locale @locale end |
#max_cache_size ⇒ Integer
Returns Maximum cache size in bytes.
294 295 296 |
# File 'lib/kotoshu/configuration.rb', line 294 def max_cache_size @max_cache_size end |
#max_suggestions ⇒ Integer
Returns Maximum number of suggestions to return.
222 223 224 |
# File 'lib/kotoshu/configuration.rb', line 222 def max_suggestions @max_suggestions end |
#models_pin ⇒ String
Returns Branch/tag/commit pinned for model downloads.
279 280 281 |
# File 'lib/kotoshu/configuration.rb', line 279 def models_pin @models_pin end |
#models_url ⇒ String
Returns Base URL for FastText ONNX models (deprecated).
267 268 269 |
# File 'lib/kotoshu/configuration.rb', line 267 def models_url @models_url end |
#offline ⇒ Boolean
Returns Whether to use only cached resources (no downloads).
246 247 248 |
# File 'lib/kotoshu/configuration.rb', line 246 def offline @offline end |
#repos_base_url ⇒ String
Returns GitHub raw root for all kotoshu content repos.
270 271 272 |
# File 'lib/kotoshu/configuration.rb', line 270 def repos_base_url @repos_base_url end |
#resolver ⇒ Resolver (readonly)
Returns The configuration resolver.
297 298 299 |
# File 'lib/kotoshu/configuration.rb', line 297 def resolver @resolver end |
#resource_pin ⇒ String
Returns Branch/tag/commit pinned for resource downloads.
252 253 254 |
# File 'lib/kotoshu/configuration.rb', line 252 def resource_pin @resource_pin end |
#suggestion_algorithms ⇒ Array<Class>?
Returns Suggestion algorithms to use.
231 232 233 |
# File 'lib/kotoshu/configuration.rb', line 231 def suggestion_algorithms @suggestion_algorithms end |
#verbose ⇒ Boolean
Returns Whether to enable verbose output.
228 229 230 |
# File 'lib/kotoshu/configuration.rb', line 228 def verbose @verbose end |
Class Method Details
.default ⇒ Configuration
Get the default configuration.
468 469 470 |
# File 'lib/kotoshu/configuration.rb', line 468 def self.default new(DEFAULTS.dup) end |
.default_cache_path ⇒ String
Get default cache path.
584 585 586 |
# File 'lib/kotoshu/configuration.rb', line 584 def self.default_cache_path Paths.cache_path end |
.default_config_path ⇒ Object
588 589 590 |
# File 'lib/kotoshu/configuration.rb', line 588 def self.default_config_path Paths.config_path end |
.default_data_path ⇒ Object
592 593 594 |
# File 'lib/kotoshu/configuration.rb', line 592 def self.default_data_path Paths.data_path end |
.instance ⇒ Configuration
Global configuration instance.
478 479 480 |
# File 'lib/kotoshu/configuration.rb', line 478 def self.instance @instance ||= default end |
.reset ⇒ Configuration
Reset the global configuration.
485 486 487 |
# File 'lib/kotoshu/configuration.rb', line 485 def self.reset @instance = default end |
Instance Method Details
#clone ⇒ Configuration
Clone the configuration.
458 459 460 |
# File 'lib/kotoshu/configuration.rb', line 458 def clone self.class.new(to_h) end |
#get(key) ⇒ Object
Get a configuration value using the resolver.
This respects the priority: CLI > ENV > Programmatic > Defaults
354 355 356 |
# File 'lib/kotoshu/configuration.rb', line 354 def get(key) @resolver.get(key) end |
#key?(key) ⇒ Boolean
Check if a configuration key has a value set.
362 363 364 |
# File 'lib/kotoshu/configuration.rb', line 362 def key?(key) @resolver.key?(key) end |
#load_dictionary ⇒ Dictionary::Base
Load the dictionary based on configuration.
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 |
# File 'lib/kotoshu/configuration.rb', line 378 def load_dictionary dict = case @dictionary_type when :unix_words load_unix_words_dictionary when :plain_text load_plain_text_dictionary when :custom load_custom_dictionary when :hunspell load_hunspell_dictionary when :cspell load_cspell_dictionary else raise ConfigurationError, "Unknown dictionary type: #{@dictionary_type}" end # Add custom words @custom_words.each do |word| dict.add_word(word) end dict end |
#reset_dictionary ⇒ self
Reset the dictionary (force reload on next access).
405 406 407 408 |
# File 'lib/kotoshu/configuration.rb', line 405 def reset_dictionary @dictionary = nil self end |
#source_registry ⇒ Kotoshu::SourceRegistry
Build a SourceRegistry honoring this configuration’s base URL and per-repo pins. Single source of truth for all resource URLs.
444 445 446 447 448 449 450 451 452 453 |
# File 'lib/kotoshu/configuration.rb', line 444 def source_registry Kotoshu::SourceRegistry.new( base_url: @repos_base_url, pins: { "dictionaries" => @dictionaries_pin, "frequency-list-kelly" => @frequency_pin, "models-fasttext-onnx" => @models_pin } ) end |
#to_h ⇒ Hash
Convert to hash.
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 |
# File 'lib/kotoshu/configuration.rb', line 413 def to_h { dictionary_path: @dictionary_path, dictionary_type: @dictionary_type, language: @language, locale: @locale, max_suggestions: @max_suggestions, case_sensitive: @case_sensitive, verbose: @verbose, suggestion_algorithms: @suggestion_algorithms&.map(&:name), custom_words: @custom_words, encoding: @encoding, dictionaries_path: @dictionaries_path, cache_path: @cache_path, config_path: @config_path, data_path: @data_path, dictionaries_url: @dictionaries_url, repos_base_url: @repos_base_url, dictionaries_pin: @dictionaries_pin, frequency_pin: @frequency_pin, models_pin: @models_pin, auto_download: @auto_download, cache_ttl: @cache_ttl, max_cache_size: @max_cache_size } end |