Class: OllamaChat::OllamaChatConfig

Inherits:
Object
  • Object
show all
Includes:
ComplexConfig, FileUtils
Defined in:
lib/ollama_chat/ollama_chat_config.rb

Overview

A configuration class for managing OllamaChat settings and file paths.

This class handles the initialization and management of configuration files for the OllamaChat application. It provides methods for setting up default configurations, determining appropriate file paths for config and cache directories, and managing the loading and creation of configuration files based on XDG standards.

Examples:

Initializing with a custom configuration file

config = OllamaChat::OllamaChatConfig.new('/path/to/custom/config.yml')

Accessing default configuration paths

config = OllamaChat::OllamaChatConfig.new
config.default_config_path # => Path to the default configuration file
config.config_dir_path     # => Path to the configuration directory
config.cache_dir_path      # => Path to the cache directory
config.database_path       # => Path to the documents database file

Constant Summary collapse

DEFAULT_CONFIG_PATH =

Path to the default config

Pathname.new(__FILE__).dirname.
join('ollama_chat_config/default_config.yml')
DEFAULT_CONFIG =

Content of the default config

File.read(DEFAULT_CONFIG_PATH)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename = nil) ⇒ OllamaChatConfig

The initialize method sets up the configuration file path and ensures the cache directory exists. It attempts to load configuration from the specified filename or uses a default path. If the configuration file is missing and the default path is used, it creates the necessary directory structure and writes a default configuration file.

Parameters:

  • filename (String, nil) (defaults to: nil)

    the path to the configuration file



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ollama_chat/ollama_chat_config.rb', line 40

def initialize(filename = nil)
  @filename = filename || default_path
  unless File.directory?(cache_dir_path)
    mkdir_p cache_dir_path.to_s
  end
  @config = Provider.config(@filename, '⚙️')
  retried = false
rescue ConfigurationFileMissing
  if @filename == default_path && !retried
    retried = true
    mkdir_p config_dir_path.to_s
    File.secure_write(default_path, DEFAULT_CONFIG)
    retry
  else
    raise
  end
end

Instance Attribute Details

#configComplexConfig::Settings (readonly)

The config reader returns the configuration object for the chat instance.

Returns:

  • (ComplexConfig::Settings)

    the configuration object



64
65
66
# File 'lib/ollama_chat/ollama_chat_config.rb', line 64

def config
  @config
end

#filenameObject (readonly)

The filename reader returns the name of the file associated with this instance.



59
60
61
# File 'lib/ollama_chat/ollama_chat_config.rb', line 59

def filename
  @filename
end

Instance Method Details

#cache_dir_pathPathname

The cache_dir_path method returns the path to the ollama_chat cache directory within the XDG cache home directory.

Returns:

  • (Pathname)

    the pathname object representing the cache directory path



97
98
99
# File 'lib/ollama_chat/ollama_chat_config.rb', line 97

def cache_dir_path
  XDG.new.cache_home + 'ollama_chat'
end

#config_dir_pathPathname

The config_dir_path method returns the path to the ollama_chat configuration directory by combining the XDG config home directory with the ‘ollama_chat’ subdirectory.

directory

Returns:

  • (Pathname)

    the pathname object representing the configuration



89
90
91
# File 'lib/ollama_chat/ollama_chat_config.rb', line 89

def config_dir_path
  XDG.new.config_home + 'ollama_chat'
end

#database_pathPathname

The database_path method constructs the full path to the documents database file by joining the cache directory path with the filename ‘documents.db’.

Returns:

  • (Pathname)

    the full path to the documents database file



105
106
107
# File 'lib/ollama_chat/ollama_chat_config.rb', line 105

def database_path
  cache_dir_path + 'documents.db'
end

#default_config_pathString

The default_config_path method returns the path to the default configuration file.

Returns:

  • (String)

    the path to the default configuration file



70
71
72
# File 'lib/ollama_chat/ollama_chat_config.rb', line 70

def default_config_path
  DEFAULT_CONFIG_PATH
end

#default_pathPathname

The default_path method constructs the full path to the default configuration file.

config.yml file within the configuration directory

Returns:

  • (Pathname)

    a Pathname object representing the path to the



79
80
81
# File 'lib/ollama_chat/ollama_chat_config.rb', line 79

def default_path
  config_dir_path + 'config.yml'
end

#diff_toolString

The diff_tool method returns the preferred diff tool command. It checks for the DIFF_TOOL environment variable and falls back to ‘vimdiff’ if not set.

Returns:

  • (String)

    the command name of the diff tool to be used



114
115
116
# File 'lib/ollama_chat/ollama_chat_config.rb', line 114

def diff_tool
  ENV.fetch('DIFF_TOOL', 'vimdiff')
end