Sha256: 511ca7bd62e1209c3f5999f038ff3f266adc0d454025b47a22d6ccc7e9cd3bbe

Contents?: true

Size: 1.29 KB

Versions: 12

Compression:

Stored size: 1.29 KB

Contents

# frozen_string_literal: true

require 'yaml'
require 'json/pure'

# Functions that read data from ConfigFile using formats YAML and JSON
# * read
# * read_yaml
# * read_json
module ConfigFileReader
  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

  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

  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
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
teuton-2.1.11 lib/teuton/project/configfile_reader.rb
teuton-2.1.10 lib/teuton/project/configfile_reader.rb
teuton-2.1.9 lib/teuton/project/configfile_reader.rb
teuton-2.1.8 lib/teuton/project/configfile_reader.rb
teuton-2.1.8dev1 lib/teuton/project/configfile_reader.rb
teuton-2.1.7 lib/teuton/project/configfile_reader.rb
teuton-2.1.6 lib/teuton/project/configfile_reader.rb
teuton-2.1.5dev lib/teuton/project/configfile_reader.rb
teuton-2.1.4 lib/teuton/project/configfile_reader.rb
teuton-2.1.3 lib/teuton/project/configfile_reader.rb
teuton-2.1.2 lib/teuton/project/configfile_reader.rb
teuton-2.1.1 lib/teuton/project/configfile_reader.rb