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' => 'db/fit.sqlite3' },
  'sync' => {
    'workers' => 3,
    'time_budget' => nil,
    'total_timeout' => nil,
  },
  'export' => {
    'json' => { 'path' => 'site/activities.json' },
    'svg' => { 'dir' => 'site/svg' },
    'csv' => { 'path' => 'site/workouts.csv' },
    'gpx' => { 'path' => 'site/gpx_out' },
  },
  'privacy' => {
    'start_end_range' => 200,
  },
  'system' => {
    'units' => 'metric',
    'log_level' => 'info',
    'timezone' => 'Asia/Shanghai',
  },
}.freeze
DEFAULT_PATH =
'config/config.yml'
KNOWN_TOP_KEYS =
%w[database sync export privacy system].freeze

Instance Method Summary collapse

Constructor Details

#initialize(path = nil) ⇒ Config

Returns a new instance of Config.



34
35
36
37
38
39
40
# File 'lib/git_fit/config.rb', line 34

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

Instance Method Details

#[](key) ⇒ Object



42
43
44
# File 'lib/git_fit/config.rb', line 42

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

#db_pathObject



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

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

#dig(*keys) ⇒ Object



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

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

#export_configObject



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

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

#privacy_configObject



55
56
57
# File 'lib/git_fit/config.rb', line 55

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

#sync_config(source) ⇒ Object



50
51
52
53
# File 'lib/git_fit/config.rb', line 50

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