Class: Sanctum::GetConfig::ConfigFile

Inherits:
Object
  • Object
show all
Defined in:
lib/sanctum/get_config/config_file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_file = nil) ⇒ ConfigFile



8
9
10
11
12
# File 'lib/sanctum/get_config/config_file.rb', line 8

def initialize(config_file=nil)
  raise "Please create or specify a config file. `sanctum config --help`" if config_file.nil?
  raise "Config file not found" unless File.file?(config_file)
  @config_file = config_file
end

Instance Attribute Details

#config_fileObject (readonly)

Returns the value of attribute config_file.



6
7
8
# File 'lib/sanctum/get_config/config_file.rb', line 6

def config_file
  @config_file
end

Instance Method Details

#deep_symbolize(obj) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/sanctum/get_config/config_file.rb', line 31

def deep_symbolize(obj)
  case obj
  when Hash
    obj.each_with_object({}) do |(k, v), hash|
      hash[k.to_sym] = deep_symbolize(v)
    end
  when Array
    obj.map { |el| deep_symbolize(el) }
  else
    obj
  end
end

#load_config_file(config_file) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/sanctum/get_config/config_file.rb', line 23

def load_config_file(config_file)
  config_hash = YAML.load_file(config_file)
  config_hash.compact!
  deep_symbolize(config_hash)
rescue
  raise "Please ensure your config file is formatted correctly. `sanctum config --help`"
end

#runObject



14
15
16
17
18
19
20
21
# File 'lib/sanctum/get_config/config_file.rb', line 14

def run
  config_hash = load_config_file(config_file)
  if config_hash.empty? || config_hash[:sync].nil?
    raise "Please specify at least one sync target in your config file: #{config_file}"
  else
    config_hash
  end
end