<commit_before>require "yaml"

DEFAULT_CONFIG = {
  "current_platform" => "prime",
}

module Mnd
  class LocalConfig
    INSTANCE = new
    DOT_FILE_PATH = ENV.fetch("HOME") + "/.mnd"

    def self.instance
      INSTANCE
    end

    def initialize
      if File.exists? dot_file
        @config = Hash(String, String).from_yaml(File.read(dot_file))
      else
        Mnd.display.error "Please run 'mnd setup' to initialize your configuration file before use"
        exit
      end
    end

    def current_platform
      @config["current_platform"]
    end

    def current_platform=(platform)
      @config["current_platform"] = platform
    end

    def root_path
      @config["root_path"]
    end

    def root_path=(root)
      @config["root_path"] = root
    end

    def persist!
      File.write dot_file, @config.to_yaml
    end

    private def dot_file
      DOT_FILE_PATH
    end
  end
end<commit_msg>Better ensuring of config<commit_after>require "yaml"

DEFAULT_CONFIG = {
  "current_platform" => "prime",
}

module Mnd
  class LocalConfig
    INSTANCE = new
    DOT_FILE_PATH = ENV.fetch("HOME") + "/.mnd"

    def self.instance
      INSTANCE
    end

    def initialize
      if File.exists? dot_file
        @config = Hash(String, String).from_yaml(File.read(dot_file))
      else
        @config = DEFAULT_CONFIG.dup
        persist!
      end
    end

    def current_platform
      @config["current_platform"]
    end

    def current_platform=(platform)
      @config["current_platform"] = platform
    end

    def root_path
      @config["root_path"]? || require_setup!
    end

    def root_path=(root)
      @config["root_path"] = root
    end

    def persist!
      File.write dot_file, @config.to_yaml
    end

    private def dot_file
      DOT_FILE_PATH
    end

    private def require_setup!
      Mnd.display.error "Please run 'mnd setup' to initialize your configuration file before use"
      exit
    end
  end
end
{'fraction_non_alphanumeric': 0.08644501278772379, 'fraction_numerical': 0.0, 'mean_word_length': 2.4558303886925796, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1843025', 'n_tokens_mistral': 628, 'n_tokens_neox': 603, 'n_words': 156}