Module: ConfigFileReader

Defined in:
lib/teuton/project/configfile_reader.rb

Overview

Functions that read data from ConfigFile using formats YAML and JSON

  • read

  • read_yaml

  • read_json

Class Method Summary collapse

Class Method Details

.read(filepath) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/teuton/project/configfile_reader.rb', line 11

def self.read(filepath)
  unless File.exist?(filepath)
    data = {}
    data[:global] = {}
    data[:alias] = {}
    data[:cases] = [{ tt_members: 'anonymous' }]
    return data
  end
  return read_yaml(filepath) if File.extname(filepath) == '.yaml'

  return read_json(filepath) if File.extname(filepath) == '.json'

  raise "[ERROR] ConfigFileReader: #{filepath}"
end

.read_json(filepath) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/teuton/project/configfile_reader.rb', line 42

def self.read_json(filepath)
  data = JSON.parse(File.read(filepath), symbolize_names: true)
  data[:global] = data[:global] || {}
  data[:alias] = data[:alias] || {}
  data[:cases] = data[:cases] || []
  data
end

.read_yaml(filepath) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/teuton/project/configfile_reader.rb', line 26

def self.read_yaml(filepath)
  begin
    data = YAML.load(File.open(filepath))
  rescue StandardError => e
    puts "\n" + ('=' * 80)
    puts "[ERROR] ConfigFileReader#read <#{filepath}>"
    puts '        I suggest to revise file format!'
    puts "        #{e.message}\n" + ('=' * 80)
    raise "[ERROR] ConfigFileReader <#{e}>"
  end
  data[:global] = data[:global] || {}
  data[:alias] = data[:alias] || {}
  data[:cases] = data[:cases] || []
  data
end