<commit_before>class Settings < ActiveRecord::Base
  @cache = {}

  def self.method_missing(method, *args)
    #get or set a variable with the variable as the called method
    method_name = method.to_s
    
    if method_name.include? '='
      #set a value for a variable
      var_name = method_name.gsub('=', '')
      value = args.first
      self[var_name] = value
    else
      #retrieve a value
      self[method_name]
    end
  end

  def self.destroy(var_name)
    return @cache.delete(var_name.to_s) if delete_all(['var = ?', var_name.to_s]) #variable exists, destroy row and cache
    raise "Setting variable \"#{var_name}\" not found"
  end

  def self.all
    #retrieve all settings as a hash
    vars = find(:all, :select => 'var, value')
    
    result = {}
    vars.each do |record|
      result[record.var] = record.value
    end
    @cache = result
    result.with_indifferent_access
  end
  
  def self.reload
    #reload all settings form the db
    self.all
    self
  end

  def self.[](var_name)
    #retrieve a setting
    var_name = var_name.to_s
    
    return @cache[var_name] if @cache[var_name] #return cached value
    
    if var = find(:first, :conditions => ['var = ?', var_name])
      @cache[var_name] = var.value
      var.value
    else
      nil
    end
  end

  def self.[]=(var_name, value)
    #set a value to a var name
    var_name = var_name.to_s
    
    if (update_all(['value = ?',value], ['var = ?',var_name]) > 0) || create(:var => var_name, :value => value)
      @cache[var_name] = value 
    end
  end
end<commit_msg>Made Settings plugin Rails 1.1 compatible.<commit_after>class Settings < ActiveRecord::Base
  @cache = {}

  def self.method_missing(method, *args)
    #get or set a variable with the variable as the called method
    method_name = method.to_s
    
    if method_name.include? '='
      #set a value for a variable
      var_name = method_name.gsub('=', '')
      value = args.first
      self[var_name] = value
    else
      #retrieve a value
      self[method_name]
    end
  end

  def self.destroy(var_name)
    return @cache.delete(var_name.to_s) if delete_all(['var = ?', var_name.to_s]) #variable exists, destroy row and cache
    raise "Setting variable \"#{var_name}\" not found"
  end

  def self.all
    #retrieve all settings as a hash
    vars = find(:all, :select => 'var, value')
    
    result = {}
    vars.each do |record|
      result[record.var] = record.value
    end
    @cache = result
    result.with_indifferent_access
  end
  
  def self.reload
    #reload all settings form the db
    self.all
    self
  end

  def self.[](var_name)
    #retrieve a setting
    var_name = var_name.to_s
    
    return @cache[var_name] if @cache[var_name] #return cached value
    
    if var = find(:first, :conditions => ['var = ?', var_name])
      @cache[var_name] = var.value
      var.value
    else
      nil
    end
  end

  def self.[]=(var_name, value)
    #set a value to a var name
    if self[var_name] != value
      var_name = var_name.to_s
      
      record = Settings.find(:first, :conditions => ['var = ?', var_name]) || Settings.new(:var => var_name)
      record.value = value
      record.save
      
      @cache[var_name] = value
    end
  end
end
{'fraction_non_alphanumeric': 0.11233885819521179, 'fraction_numerical': 0.0009208103130755065, 'mean_word_length': 2.5308775731310944, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 12, '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': '5157196', 'n_tokens_mistral': 1093, 'n_tokens_neox': 1077, 'n_words': 309}