<commit_before>require 'thread_attr_accessor'
require 'active_support/core_ext/module/delegation'

require_relative "config/version"
require_relative "config/builder"
require_relative 'config/scoped_config'
require_relative 'config/chain_container'

module Moltrio
  module Config
    extend self

    def configure(&block)
      @configuration_block = block
      self
    end

    extend ThreadAttrAccessor
    thread_attr_accessor :current_namespace, inherit: true, private: true
    thread_attr_accessor :cached_containers, inherit: true, private: true

    def enable_caching
      self.cached_containers ||= {}
    end

    def disable_caching
      self.cached_containers = nil
    end

    def on_namespace(namespace)
      if block_given?
        prev_namespace = current_namespace
        self.current_namespace = namespace

        if cached_containers
          cached_containers.delete(:namespaced)
        end

        begin
          yield
        ensure
          self.current_namespace = prev_namespace
        end

        self
      else
        ChainContainer.new(chains_on_namespace(namespace))
      end
    end

    delegate :[], :[]=, :has_key?, :fetch, :scoped, to: :default_chain
    delegate :available_namespaces, to: :root_container
    delegate :chain, to: :namespaced_container

    def default_chain
      chain(:default)
    end

  private

    def root_container
      return cached_containers[:root] if cached_containers && cached_containers[:root]
      container = ChainContainer.new(root_chains)

      if cached_containers
        cached_containers[:root] = container
      end

      container
    end

    def namespaced_container
      return cached_containers[:namespaced] if cached_containers && cached_containers[:namespaced]

      if current_namespace
        container = ChainContainer.new(chains_on_namespace(current_namespace))
      else
        container = root_container
      end

      if cached_containers
        cached_containers[:namespaced_chains] = chains
      end

      container
    end

    def root_chains
      Builder.run(&@configuration_block).chains
    end

    def chains_on_namespace(namespace)
      root_chains.map { |chain_name, chain|
        [chain_name, chain.on_namespace(current_namespace)]
      }
    end
  end
end<commit_msg>Thinko<commit_after>require 'thread_attr_accessor'
require 'active_support/core_ext/module/delegation'

require_relative "config/version"
require_relative "config/builder"
require_relative 'config/scoped_config'
require_relative 'config/chain_container'

module Moltrio
  module Config
    extend self

    def configure(&block)
      @configuration_block = block
      self
    end

    extend ThreadAttrAccessor
    thread_attr_accessor :current_namespace, inherit: true, private: true
    thread_attr_accessor :cached_containers, inherit: true, private: true

    def enable_caching
      self.cached_containers ||= {}
    end

    def disable_caching
      self.cached_containers = nil
    end

    def on_namespace(namespace)
      if block_given?
        prev_namespace = current_namespace
        self.current_namespace = namespace

        if cached_containers
          cached_containers.delete(:namespaced)
        end

        begin
          yield
        ensure
          self.current_namespace = prev_namespace
        end

        self
      else
        ChainContainer.new(chains_on_namespace(namespace))
      end
    end

    delegate :[], :[]=, :has_key?, :fetch, :scoped, to: :default_chain
    delegate :available_namespaces, to: :root_container
    delegate :chain, to: :namespaced_container

    def default_chain
      chain(:default)
    end

  private

    def root_container
      return cached_containers[:root] if cached_containers && cached_containers[:root]
      container = ChainContainer.new(root_chains)

      if cached_containers
        cached_containers[:root] = container
      end

      container
    end

    def namespaced_container
      return cached_containers[:namespaced] if cached_containers && cached_containers[:namespaced]

      if current_namespace
        container = ChainContainer.new(chains_on_namespace(current_namespace))
      else
        container = root_container
      end

      if cached_containers
        cached_containers[:namespaced_chains] = chains
      end

      container
    end

    def root_chains
      Builder.run(&@configuration_block).chains
    end

    def chains_on_namespace(namespace)
      root_chains.map { |chain_name, chain|
        [chain_name, chain.on_namespace(namespace)]
      }
    end
  end
end
{'fraction_non_alphanumeric': 0.06340933767643865, 'fraction_numerical': 0.0, 'mean_word_length': 2.933390264730999, 'pattern_counts': {'":': 0, '<': 3, '<?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': '3788356', 'n_tokens_mistral': 1311, 'n_tokens_neox': 1245, 'n_words': 281}