<commit_before>#
# @see SknSettings

module SknUtils

  class Configuration < NestedResult

    def initialize(params={})
      default_mode = ENV.fetch('RACK_ENV', 'development')
      @config_filename = params.is_a?(String) ? params : params.fetch(:config_filename, default_mode)
      @base_path = ENV.fetch('TEST_GEM', 'rails').eql?('gem') ? './spec/factories/' : './config/'
      load_config_basename!(@config_filename)
    end

    def load_config_basename!(conf)
      reset_from_empty!(load_config(conf), false) # enable dot notation via defined methods(true) vs method_missing(false)
      self
    end

    def config_path!(fpath)
      @base_path = fpath if File.exist?("#{fpath}settings.yml")
    end

    private

    def load_config(conf)
      yname   = "#{@base_path}settings.yml"
        return {} unless File.exist?(yname)
        f_base  = load_yml_with_erb(yname)

      yname   = "#{@base_path}settings/#{conf}.yml"
        f_env   = load_yml_with_erb(yname) if File.exist?(yname)
        f_base  = f_base.deep_merge!(f_env) unless (f_env.nil? || f_env.empty?)

      yname   = "#{@base_path}settings/#{conf}.local.yml"
        f_local = load_yml_with_erb(yname)  if File.exist?(yname)
        f_base  = f_base.deep_merge!(f_local) unless (f_local.nil? || f_local.empty?)

      f_base
    end

    def load_yml_with_erb(yml_file)
      erb = ERB.new(File.read(yml_file)).result
      erb.empty? ? {} : Psych.load(erb)
    rescue => e
      puts "#{self.class.name}##{__method__} Class: #{e.class}, Message: #{e.message}"
      {}
    end
  end

end<commit_msg>WIP Upgrading Configuraiton to 90 percent of Config/RBConfig features.<commit_after>#
# @see SknSettings
#
# Filepath Targets:
# -------------------------------------------------
# <prepend-somefile>
# config/settings.yml
# config/settings/#{environment}.yml
# config/environments/#{environment}.yml
#
# config/settings.local.yml
# config/settings/#{environment}.local.yml
# config/environments/#{environment}.local.yml
# <append-somefile>
# -------------------------------------------------

module SknUtils

  class Configuration < NestedResult

    # "filename-only"
    # {config_filename: "filename-only"}
    def initialize(params={})
      @default_mode = ENV.fetch('RACK_ENV', 'development')
      @config_filename = params.is_a?(String) ? params : params.fetch(:config_filename, @default_mode)
      test_mode = ENV.fetch('TEST_GEM', 'rails').eql?('gem')
      @base_path = config_path!( test_mode ? './spec/factories' : './config' )
      load_config_basename!(@config_filename)
    end

    def load_config_basename!(conf=@default_mode)
      reset_from_empty!(load_config(conf), false) # enable dot notation via defined methods(true) vs method_missing(false)
      self
    end
    alias_method :reload!, :load_config_basename!

    def config_path!(fpath)
      @base_path = fpath if File.exist?("#{fpath}settings.yml")
      @base_path = "#{fpath}/" if File.exist?("#{fpath}/settings.yml")
    end

    def load_and_set_settings(ordered_list_of_files)
      reset_from_empty!( load_ordered_list( ordered_list_of_files ) )
    end
    alias_method :reload_from_files, :load_and_set_settings

    # Config.setting_files("/path/to/config_root", "your_project_environment")
    def setting_files(config_root, env_name) # returns a file array
      config_path!(config_root)
      @default_mode = env_name
      configuration_files()
    end


    def add_source!(file_path_or_hash) # load last
      return {} if File.exist.eql?( file_path_or_hash )
      ordered_list_of_files = configuration_files().push( file_path_or_hash )  # or unshift
      load_and_set_settings(ordered_list_of_files)

      # Settings.add_source!({some_secret: ENV['some_secret']})
    end

    def prepend_source!(prepend_fpath) # load first
      return {} if File.exist.eql?( prepend_fpath )
      ordered_list_of_files = configuration_files().unshift(prepend_path)  # or unshift
      load_and_set_settings(ordered_list_of_files)
    end

    private

    def configuration_files(default=@default_mode)
      @_last_filelist = [
          "#{@base_path}settings.yml",
          "#{@base_path}settings/#{default}.yml",
          "#{@base_path}environments/#{default}.yml",
          "#{@base_path}settings.local.yml",
          "#{@base_path}settings/#{default}.local.yml",
          "#{@base_path}environments/#{default}.local.yml"
      ]
    end

    def load_ordered_list(filelist)
      yname = filelist.shift
        return {} unless File.exist?( yname )
        f_base  = load_yml_with_erb(yname)

      filelist.each do |filepath|
        next unless File.exist?(filepath)
        f_env   = load_yml_with_erb(filepath)
        f_base  = f_base.deep_merge!(f_env) unless (f_env.nil? || f_env.empty?)
      end

      f_base
    end

    def load_config(conf)
      yname   = "#{@base_path}settings.yml"
        return {} unless File.exist?(yname)
        f_base  = load_yml_with_erb(yname)

     do_this = [
         "#{@base_path}settings.yml",
         "#{@base_path}settings/#{conf}.yml",
         "#{@base_path}settings/environments/#{conf}.yml",
         "#{@base_path}settings.local.yml",
         "#{@base_path}settings/#{conf}.local.yml",
         "#{@base_path}settings/environments/#{conf}.local.yml"
     ]

      load_ordered_list( do_this )
    end

    def load_yml_with_erb(yml_file)
      erb = ERB.new(File.read(yml_file)).result
      erb.empty? ? {} : Psych.load(erb)
    rescue => e
      puts "#{self.class.name}##{__method__} Class: #{e.class}, Message: #{e.message}"
      {}
    end
  end

end
{'fraction_non_alphanumeric': 0.15100071479628305, 'fraction_numerical': 0.00035739814152966406, 'mean_word_length': 3.0883856829802774, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '6621281', 'n_tokens_mistral': 1944, 'n_tokens_neox': 1896, 'n_words': 300}