Class: GitFit::Config

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

Overview

三优先级: env var > YAML > DEFAULTS

Constant Summary collapse

DEFAULTS =
{
  'database' => { 'path' => 'data/db/git-fit.db' },
  'sync' => {
    'workers' => 3,
    'time_budget' => nil,
    'total_timeout' => nil,
  },
  'export' => {
    'json' => { 'path' => 'site/activities.json' },
    'stats' => { 'path' => 'site/stats.json' },
    'svg' => { 'dir' => 'site/svg' },
    'csv' => { 'path' => 'site/workouts.csv' },
    'gpx' => { 'path' => 'site/gpx_out' },
    'ai' => { 'path' => 'site/insights.json' },
  },
  'privacy' => {
    'start_end_range' => 200,
  },
  'ai' => {
    'enabled' => false,
    'endpoint' => nil, # OpenAI-compatible base URL, e.g. https://open.bigmodel.cn/api/paas/v4
    'model' => nil,
    'api_key_env' => 'AI_API_KEY',
    'language' => 'zh',
    'timeout' => 60,
    'style' => '',
    'prompts' => {}, # scenario => template override; multi-line, intentionally not env-overridable
  },
  'system' => {
    'units' => 'metric',
    'log_level' => 'info',
    'timezone' => 'Asia/Shanghai',
  },
}.freeze
KNOWN_TOP_KEYS =
%w[database sync export privacy ai system].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = nil) ⇒ Config

Returns a new instance of Config.



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

def initialize(path = nil)
  @data = deep_dup(DEFAULTS)
  load_file(path) if path
  default = self.class.default_path
  load_file(default) if !path && File.exist?(default)
  load_env
  validate!
end

Class Method Details

.default_pathObject

Default config file path: GIT_FIT_CONFIG_PATH env override, else root config.yml.



46
47
48
49
# File 'lib/git_fit/config.rb', line 46

def self.default_path
  env = ENV['GIT_FIT_CONFIG_PATH'].to_s.strip
  env.empty? ? 'config.yml' : env
end

Instance Method Details

#[](key) ⇒ Object



60
61
62
# File 'lib/git_fit/config.rb', line 60

def [](key)
  @data[key]
end

#ai_configObject



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

def ai_config
  @data['ai'] || {}
end

#db_pathObject



85
86
87
# File 'lib/git_fit/config.rb', line 85

def db_path
  @data.dig('database', 'path') || DEFAULTS['database']['path']
end

#dig(*keys) ⇒ Object



64
65
66
# File 'lib/git_fit/config.rb', line 64

def dig(*keys)
  @data.dig(*keys)
end

#export_configObject



77
78
79
# File 'lib/git_fit/config.rb', line 77

def export_config
  @data['export'] || {}
end

#privacy_configObject



73
74
75
# File 'lib/git_fit/config.rb', line 73

def privacy_config
  @data['privacy'] || {}
end

#sync_config(source) ⇒ Object



68
69
70
71
# File 'lib/git_fit/config.rb', line 68

def sync_config(source)
  val = @data.dig('sync', source)
  val.is_a?(Hash) ? val : {}
end