Module: Chronicle::ETL::Config

Extended by:
Config
Included in:
Config
Defined in:
lib/chronicle/etl/config.rb

Overview

Utility methods to read, write, and access config files

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#xdg_environmentObject

Returns the value of attribute xdg_environment.



11
12
13
# File 'lib/chronicle/etl/config.rb', line 11

def xdg_environment
  @xdg_environment
end

Instance Method Details

#available_configs(type) ⇒ Object

Returns all configs available for a given type



55
56
57
58
59
# File 'lib/chronicle/etl/config.rb', line 55

def available_configs(type)
  Dir.glob(File.join(config_pathname_for_type(type), '*.yml')).map do |filename|
    File.basename(filename, '.*')
  end
end

#available_jobsObject

Returns all jobs available in ~/.config/chronicle/etl/jobs/*.yml



48
49
50
51
52
# File 'lib/chronicle/etl/config.rb', line 48

def available_jobs
  Dir.glob(File.join(config_pathname_for_type('jobs'), '*.yml')).map do |filename|
    File.basename(filename, '.*')
  end
end

#config_pathnameObject



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

def config_pathname
  base = Pathname.new(xdg_config.config_home)
  base.join('chronicle', 'etl')
end

#config_pathname_for_type(type) ⇒ Object



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

def config_pathname_for_type(type)
  config_pathname.join(type)
end

#exists?(type, identifier) ⇒ Boolean

Whether a config exists for a given type and identifier

Returns:

  • (Boolean)


41
42
43
44
45
# File 'lib/chronicle/etl/config.rb', line 41

def exists?(type, identifier)
  base = config_pathname_for_type(type)
  path = base.join("#{identifier}.yml")
  path.exist?
end

#load(type, identifier) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/chronicle/etl/config.rb', line 13

def load(type, identifier)
  base = config_pathname_for_type(type)
  path = base.join("#{identifier}.yml")
  return {} unless path.exist?

  YAML.safe_load_file(path, symbolize_names: true, permitted_classes: [Symbol, Date, Time])
end

#path(type, identifier) ⇒ Object

Returns path for a given config type and identifier



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

def path(type, identifier)
  base = config_pathname_for_type(type)
  base.join("#{identifier}.yml")
end

#read_job(job_name) ⇒ Object

Load a job definition from job config directory



62
63
64
65
66
# File 'lib/chronicle/etl/config.rb', line 62

def read_job(job_name)
  definition = load('jobs', job_name)
  definition[:name] ||= job_name
  definition
end

#write(type, identifier, data) ⇒ Object

Writes a hash as a yml config file



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/chronicle/etl/config.rb', line 22

def write(type, identifier, data)
  base = config_pathname_for_type(type)
  path = base.join("#{identifier}.yml")

  data.deep_stringify_keys!
  FileUtils.mkdir_p(File.dirname(path))
  File.open(path, 'w', 0o600) do |f|
    # Ruby likes to add --- separators when writing yaml files
    f << data.to_yaml.gsub(/^-+\n/, '')
  end
end

#xdg_configObject



77
78
79
80
81
82
83
84
# File 'lib/chronicle/etl/config.rb', line 77

def xdg_config
  # Only used for overriding ENV['HOME'] for XDG-related specs
  if @xdg_environment
    XDG::Environment.new(environment: @xdg_environment)
  else
    XDG::Environment.new
  end
end