<commit_before>require "hashie"
require "yaml"
require "erb"

require "easy_settings_version"

class EasySettings < Hashie::Mash
  include EasySettingsVersion

  class SourceFileNotExist < StandardError; end

  DEFAULT_FILES = %w(settings.yml config/settings.yml)

  class << self
    attr_accessor :source_hash, :source_file, :namespace

    def instance
      @instance ||= new(namespace ? _source[namespace] : _source)
    end

    def load!
      instance
      true
    end

    def reload!
      @instance = nil
      load!
    end

    def [](key)
      instance[key]
    end

    def []=(key, val)
      instance[key] = val
    end

    private

    def _source
      return source_hash.to_hash if source_hash
      return _source_from_file if source_file
      _source_from_default_file
    end

    def _load_file(file)
      content = File.read(file)
      content.empty? ? nil : YAML.load(ERB.new(content).result).to_hash
    end

    def _source_from_file
      unless FileTest.exist?(source_file)
        raise(SourceFileNotExist, "Your source file '#{source_file}' does not exist.")
      end
      _load_file(source_file)
    end

    def _source_from_default_file
      DEFAULT_FILES.each do |f|
        path = File.expand_path(f, Dir.pwd)
        return _load_file(path) if FileTest.exist?(path)
      end
      nil
    end

    def method_missing(method_name, *args, &blk)
      instance.send(method_name, *args, &blk)
    end
  end

  def assign_property(name, value)
    super
    self[name]
  end

  def method_missing(method_name, *args, &blk)
    return self.[](method_name, &blk) if key?(method_name)
    name, suffix = method_suffix(method_name)
    case suffix
    when '='
      assign_property(name, args.first)
    when '?'
      !!self[name]
    when '!'<commit_msg>Fix behavior when there is no source file<commit_after>require "hashie"
require "yaml"
require "erb"

require "easy_settings_version"

class EasySettings < Hashie::Mash
  include EasySettingsVersion

  class SourceFileNotExist < StandardError; end

  DEFAULT_FILES = %w(settings.yml config/settings.yml)

  class << self
    attr_accessor :source_hash, :source_file, :namespace

    def instance
      @instance ||= new(namespace ? _source[namespace] : _source)
    end

    def load!
      instance
      true
    end

    def reload!
      @instance = nil
      load!
    end

    def [](key)
      instance[key]
    end

    def []=(key, val)
      instance[key] = val
    end

    private

    def _source
      return source_hash.to_hash if source_hash
      return _source_from_file if source_file
      _source_from_default_file
    end

    def _load_file(file)
      content = File.read(file)
      content.empty? ? nil : YAML.load(ERB.new(content).result).to_hash
    end

    def _source_from_file
      unless FileTest.exist?(source_file)
        raise(SourceFileNotExist, "Your source file '#{source_file}' does not exist.")
      end
      _load_file(source_file)
    end

    def _source_from_default_file
      DEFAULT_FILES.each do |f|
        path = File.expand_path(f, Dir.pwd)
        return _load_file(path) if FileTest.exist?(path)
      end
      {}
    end

    def method_missing(method_name, *args, &blk)
      instance.send(method_name, *args, &blk)
    end
  end

  def assign_property(name, value)
    super
    self[name]
  end

  def method_missing(method_name, *args, &blk)
    return self.[](method_name, &blk) if key?(method_name)
    name, suffix = method_suffix(method_name)
    case suffix
    when '='
      assign_property(name, args.first)
    when '?'
      !!self[name]
    when '!'
{'fraction_non_alphanumeric': 0.09223971278652306, 'fraction_numerical': 0.0, 'mean_word_length': 2.749482401656315, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 3, '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': '2317022', 'n_tokens_mistral': 1208, 'n_tokens_neox': 1163, 'n_words': 286}