<commit_before>module Configurable
  extend ActiveSupport::Concern

  included do
    cattr_accessor :host, :user_id, :application_id
  end

  module ClassMethods
    def configure(key, options = {})
      @fields ||= {}
      @fields[key] = options
      cattr_accessor key
    end

    def config_file=(file_name)
      @config_file = "config/#{file_name}.yml"
    end

    def api_prefix=(prefix)
      @api_prefix = prefix.upcase
    end

    def load_configuration
      @fields.each do |key, options|
        field_key = options[:key] || key
        value = env_vars["#{@api_prefix}_#{field_key.upcase}"]
        value ||= config_from_file[field_key]
        value = value.to_i if options[:type] == :integer

        self.public_send("#{key}=", value)
      end
    end

    def config_from_file
      @config_from_file ||=
        begin
          config = YAML.safe_load(
            ERB.new(File.read(Rails.root.join(@config_file))).result
          )
          config[Rails.env].symbolize_keys
        rescue Errno::ENOENT, NoMethodError
          {}
        end
    end

    def env_vars
      ENV
    end
  end
end
<commit_msg>load from upcase ENV var names (#3370)<commit_after>module Configurable
  extend ActiveSupport::Concern

  included do
    cattr_accessor :host, :user_id, :application_id
  end

  module ClassMethods
    def configure(key, options = {})
      @fields ||= {}
      @fields[key] = options
      cattr_accessor key
    end

    def config_file=(file_name)
      @config_file = "config/#{file_name}.yml"
    end

    def api_prefix=(prefix)
      @api_prefix = prefix.upcase
    end

    def load_configuration
      @fields.each do |key, options|
        field_key = options[:key] || key
        env_key = "#{@api_prefix}_#{field_key}".upcase
        value = env_vars[env_key]
        value ||= config_from_file[field_key]
        value = value.to_i if options[:type] == :integer

        self.public_send("#{key}=", value)
      end
    end

    def config_from_file
      @config_from_file ||=
        begin
          config = YAML.safe_load(
            ERB.new(File.read(Rails.root.join(@config_file))).result
          )
          config[Rails.env].symbolize_keys
        rescue Errno::ENOENT, NoMethodError
          {}
        end
    end

    def env_vars
      ENV
    end
  end
end

{'fraction_non_alphanumeric': 0.10485603781693167, 'fraction_numerical': 0.0017189514396218307, 'mean_word_length': 2.288135593220339, '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': '308858', 'n_tokens_mistral': 763, 'n_tokens_neox': 741, 'n_words': 154}