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' },
  },
  'privacy' => {
    'start_end_range' => 200,
  },
  'system' => {
    'units' => 'metric',
    'log_level' => 'info',
    'timezone' => 'Asia/Shanghai',
  },
}.freeze
KNOWN_TOP_KEYS =
%w[database sync export privacy system].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = nil) ⇒ Config

Returns a new instance of Config.



40
41
42
43
44
45
46
47
# File 'lib/git_fit/config.rb', line 40

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.



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

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

Instance Method Details

#[](key) ⇒ Object



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

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

#db_pathObject



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

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

#dig(*keys) ⇒ Object



53
54
55
# File 'lib/git_fit/config.rb', line 53

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

#export_configObject



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

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

#privacy_configObject



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

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

#sync_config(source) ⇒ Object



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

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