<commit_before># frozen_string_literal: true

module Dip
  class Environment
    VAR_REGEX = /\$[\{]?(?<var_name>[a-zA-Z_][a-zA-Z0-9_]*)[\}]?/
    SPECIAL_VARS = {"DIP_OS" => :find_dip_os}.freeze

    attr_reader :vars

    def initialize(default_vars)
      @vars = {}

      merge(default_vars)
    end

    def merge(new_vars)
      new_vars.each do |key, value|
        @vars[key] = ENV.fetch(key) { replace(value.to_s) }
      end
    end

    def [](name)
      vars.fetch(name) { ENV[name] }
    end

    def []=(key, value)
      @vars[key] = value
    end

    def interpolate(value)
      value.gsub(VAR_REGEX) do
        var_name = Regexp.last_match[:var_name]

        if SPECIAL_VARS.key?(var_name)
          self[var_name] || send(SPECIAL_VARS[var_name])
        else<commit_msg>Fix env merge<commit_after># frozen_string_literal: true

module Dip
  class Environment
    VAR_REGEX = /\$[\{]?(?<var_name>[a-zA-Z_][a-zA-Z0-9_]*)[\}]?/
    SPECIAL_VARS = {"DIP_OS" => :find_dip_os}.freeze

    attr_reader :vars

    def initialize(default_vars)
      @vars = {}

      merge(default_vars)
    end

    def merge(new_vars)
      new_vars.each do |key, value|
        key = key.to_s
        @vars[key] = ENV.fetch(key) { interpolate(value.to_s) }
      end
    end

    def [](name)
      vars.fetch(name) { ENV[name] }
    end

    def []=(key, value)
      @vars[key] = value
    end

    def interpolate(value)
      value.gsub(VAR_REGEX) do
        var_name = Regexp.last_match[:var_name]

        if SPECIAL_VARS.key?(var_name)
          self[var_name] || send(SPECIAL_VARS[var_name])
        else
{'fraction_non_alphanumeric': 0.1425914445133292, 'fraction_numerical': 0.0024798512089274642, 'mean_word_length': 2.470967741935484, 'pattern_counts': {'":': 0, '<': 5, '<?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': '1166942', 'n_tokens_mistral': 640, 'n_tokens_neox': 610, 'n_words': 107}