<commit_before>module Yamler
  class Template
    def optional_yaml(path)
      path = File.extname(path) == '' ? "#{path}.yml" : path
      require_yaml(path) if File.exists?(path)
    end
  end
end

require 'yamler'

class Configuration
  def self.load_yaml(path, opts = {})
    yml = Yamler.load(path)

    config = build_inheritance_chain yml, opts[:hash].to_s, opts[:inherit].to_s unless opts[:hash].nil? or opts[:inherit].nil?
    apply_overrides yml, config, opts[:override_with].to_s unless opts[:override_with].nil?

    config
  end

  def self.build_inheritance_chain(everything, selector, inheritance_attr)
    selection = everything[selector]

    if not selection[inheritance_attr].nil?
      puts "Settings for '#{selector}' inherit '#{selection[inheritance_attr]}' via attribute '#{inheritance_attr}'"

      inherited = build_inheritance_chain everything, selection[inheritance_attr], inheritance_attr
      selection = inherited.recursive_merge(selection)
      selection.delete(inheritance_attr)
    end

    selection
  end

  def self.apply_overrides(everything, config, selector)
    overrides = everything[selector] 

    return if overrides.nil?

    puts "Applying configuration overrides from #{selector}"
    config.recursive_merge(overrides)
  end
end

class Hash
  def recursive_merge(h)
    self.merge!(h) do |key, _old, _new|
      if _old.class == Hash
        _old.recursive_merge(_new)
      else
        _new
      end
    end
  end
end

config = Configuration.load_yaml("config/local_environment.yml")

APP_CONFIG = config.merge(
  Configuration.load_yaml("config/applications/#{config.application_name}.yml", :hash => Rails.env, :inherit => :default_to)
<commit_msg>working on local config solution<commit_after>module Yamler
  class Template
    def optional_yaml(path)
      path = File.extname(path) == '' ? "#{path}.yml" : path
      require_yaml(path) if File.exists?(path)
    end
  end
end

require 'yamler'

class Configuration
  def self.load_yaml(path, opts = {})
    yml = Yamler.load(path)

    config = build_inheritance_chain yml, opts[:hash].to_s, opts[:inherit].to_s unless opts[:hash].nil? or opts[:inherit].nil?
    apply_overrides yml, config, opts[:override_with].to_s unless opts[:override_with].nil?

    config
  end

  def self.build_inheritance_chain(everything, selector, inheritance_attr)
    selection = everything[selector]

    if not selection[inheritance_attr].nil?
      puts "Settings for '#{selector}' inherit '#{selection[inheritance_attr]}' via attribute '#{inheritance_attr}'"

      inherited = build_inheritance_chain everything, selection[inheritance_attr], inheritance_attr
      selection = inherited.recursive_merge(selection)
      selection.delete(inheritance_attr)
    end

    selection
  end

  def self.apply_overrides(everything, config, selector)
    overrides = everything[selector] 

    return if overrides.nil?

    puts "Applying configuration overrides from #{selector}"
    config.recursive_merge(overrides)
  end
end

class Hash
  def recursive_merge(h)
    self.merge!(h) do |key, _old, _new|
      if _old.class == Hash
        _old.recursive_merge(_new)
      else
        _new
      end
    end
  end
end

config = Configuration.load_yaml("config/local_environment.yml")

APP_CONFIG = config.merge(
  Configuration.load_yaml("config/applications/#{config.application_name}.yml", :hash => Rails.env, :inherit => :default_to))

{'fraction_non_alphanumeric': 0.10653823701109165, 'fraction_numerical': 0.0, 'mean_word_length': 4.047128129602356, 'pattern_counts': {'":': 0, '<': 3, '<?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': '4324151', 'n_tokens_mistral': 1071, 'n_tokens_neox': 1042, 'n_words': 238}