Class: Ace::Support::Models::Atoms::ProviderConfigWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/ace/support/models/atoms/provider_config_writer.rb

Overview

Writes provider configuration files, preserving structure Updates only the models: section while keeping other fields intact

Class Method Summary collapse

Class Method Details

.backup(path) ⇒ String

Create a backup of a config file

Parameters:

  • path (String) —

    Path to config file

Returns:

  • (String) —

    Path to backup file



60
61
62
63
64
65
66
67
# File 'lib/ace/support/models/atoms/provider_config_writer.rb', line 60

def backup(path)
  return nil unless File.exist?(path)

  timestamp = Time.now.strftime("%Y%m%d_%H%M%S")
  backup_path = "#{path}.backup.#{timestamp}"
  FileUtils.cp(path, backup_path)
  backup_path
end

.replace(path, config, expected:) ⇒ Object

Replace config and provenance together; reject edits made after preview.

Raises:



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ace/support/models/atoms/provider_config_writer.rb', line 17

def replace(path, config, expected:)
  raise ConfigError, "Config changed during sync: #{path}" unless read_config(path) == expected

  content = format_config(config)
  Tempfile.create([".provider-sync-", ".yml"], File.dirname(path)) do |file|
    file.chmod(File.stat(path).mode & 0o777)
    file.write(content)
    file.flush
    file.fsync
    raise ConfigError, "Config changed during sync: #{path}" unless read_config(path) == expected

    backup(path)
    File.rename(file.path, path)
  end
  true
end

.update_last_synced(path, date = Date.today) ⇒ Boolean

Update the last_synced field in a provider config file

Parameters:

  • path (String) —

    Path to config file

  • date (Date) (defaults to: Date.today) —

    Date to set (defaults to today)

Returns:

  • (Boolean) —

    true on success

Raises:



74
75
76
77
78
79
# File 'lib/ace/support/models/atoms/provider_config_writer.rb', line 74

def update_last_synced(path, date = Date.today)
  config = read_config(path)
  config["last_synced"] = date
  write(path, config)
  true
end

.update_models(path, models) ⇒ Boolean

Update the models list in a provider config file

Parameters:

  • path (String) —

    Path to config file

  • models (Array<String>) —

    New list of model IDs

Returns:

  • (Boolean) —

    true on success

Raises:



39
40
41
42
43
44
# File 'lib/ace/support/models/atoms/provider_config_writer.rb', line 39

def update_models(path, models)
  config = read_config(path)
  config["models"] = Array(models)
  write(path, config)
  true
end

.update_models_and_sync_date(path, models, date = Date.today, limits: nil) ⇒ Boolean

Update both models and last_synced in one operation

Parameters:

  • path (String) —

    Path to config file

  • models (Array<String>) —

    New list of model IDs

  • date (Date) (defaults to: Date.today) —

    Date to set for last_synced

Returns:

  • (Boolean) —

    true on success



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/ace/support/models/atoms/provider_config_writer.rb', line 86

def update_models_and_sync_date(path, models, date = Date.today, limits: nil)
  config = read_config(path)
  config["models"] = Array(models)
  config["last_synced"] = date

  unless limits.nil?
    normalized_limits = normalize_limits(limits)
    if normalized_limits.empty?
      config.delete("limits")
    else
      config["limits"] = normalized_limits
    end
    config.delete("context_limit")
  end

  write(path, config)
  true
end

.write(path, config) ⇒ Boolean

Write a complete config file

Parameters:

  • path (String) —

    Path to config file

  • config (Hash) —

    Config hash

Returns:

  • (Boolean) —

    true on success



50
51
52
53
54
55
# File 'lib/ace/support/models/atoms/provider_config_writer.rb', line 50

def write(path, config)
  ensure_directory(File.dirname(path))
  content = format_config(config)
  write_file(path, content)
  true
end