Class: WritersRoom::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/writers_room/config.rb

Constant Summary collapse

DEFAULT_CONFIG =
{
  "provider" => "ollama",
  "model_name" => "gpt-oss",
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = nil) ⇒ Config



15
16
17
18
# File 'lib/writers_room/config.rb', line 15

def initialize(path = nil)
  @path = path || default_config_path
  @data = load_config
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



8
9
10
# File 'lib/writers_room/config.rb', line 8

def data
  @data
end

#pathObject (readonly)

Returns the value of attribute path.



8
9
10
# File 'lib/writers_room/config.rb', line 8

def path
  @path
end

Class Method Details

.create_project(project_path, options = {}) ⇒ WritersRoom::Config

Create a new project configuration



51
52
53
54
55
56
57
58
59
60
# File 'lib/writers_room/config.rb', line 51

def self.create_project(project_path, options = {})
  FileUtils.mkdir_p(project_path)

  config_path = File.join(project_path, "config.yml")
  config_data = DEFAULT_CONFIG.merge(options.transform_keys(&:to_s))

  config = new(config_path)
  config.save(config_data)
  config
end

Instance Method Details

#get(key) ⇒ Object

Get configuration value



66
67
68
# File 'lib/writers_room/config.rb', line 66

def get(key)
  @data[key.to_s]
end

#load_configHash

Load configuration from file



23
24
25
26
27
28
29
30
# File 'lib/writers_room/config.rb', line 23

def load_config
  return DEFAULT_CONFIG.dup unless File.exist?(path)

  YAML.load_file(path) || DEFAULT_CONFIG.dup
rescue StandardError => e
  warn "Error loading config from #{path}: #{e.message}"
  DEFAULT_CONFIG.dup
end

#model_nameString

Get model name



88
89
90
# File 'lib/writers_room/config.rb', line 88

def model_name
  get("model_name")
end

#providerString

Get provider



81
82
83
# File 'lib/writers_room/config.rb', line 81

def provider
  get("provider")
end

#save(config_data = @data) ⇒ Boolean

Save configuration to file



36
37
38
39
40
41
42
43
44
# File 'lib/writers_room/config.rb', line 36

def save(config_data = @data)
  FileUtils.mkdir_p(File.dirname(path))
  File.write(path, YAML.dump(config_data))
  @data = config_data
  true
rescue StandardError => e
  warn "Error saving config to #{path}: #{e.message}"
  false
end

#set(key, value) ⇒ Object

Set configuration value



74
75
76
# File 'lib/writers_room/config.rb', line 74

def set(key, value)
  @data[key.to_s] = value
end