<commit_before>class Configurable < ActiveRecord::Base

  after_save :invalidate_cache, if: -> { ConfigurableEngine::Engine.config.use_cache }

  validates_presence_of    :name
  validates_uniqueness_of  :name

  validate :type_of_value
  before_save :serialize_value

  def self.defaults
    @defaults ||= HashWithIndifferentAccess.new(
      YAML.load_file(
        Rails.root.join('config', 'configurable.yml')
      )
    )
  end

  def self.keys
    self.defaults.collect { |k,v| k.to_s }.sort
  end

  def self.[]=(key, value)
    exisiting = find_by_name(key)
    if exisiting
      exisiting.update_attribute(:value, value)
    else
      create {|c| c.name = key.to_s; c.value = value}
    end
  end

  def self.[](key)
    return defaults[key][:default] unless table_exists?

    if ConfigurableEngine::Engine.config.use_cache
      Rails.cache.fetch("configurable_engine:#{key}") {
        find_by_name(key).try(:value)
      }
    elsif val = find_by_name(key).try(:value)
      val
    else
      value ||= defaults[key][:default]
      parse_value key, value
    end
  end

  def value
    self.class.parse_value name, super
  end

  def self.parse_value key, value
    case defaults[key][:type]
    when 'boolean'
      [true, 1, "1", "t", "true"].include?(value)<commit_msg>if cacheing is enabled, still return default if val isn’t set (bug fix)<commit_after>class Configurable < ActiveRecord::Base

  after_save :invalidate_cache, if: -> { ConfigurableEngine::Engine.config.use_cache }

  validates_presence_of    :name
  validates_uniqueness_of  :name

  validate :type_of_value
  before_save :serialize_value

  def self.defaults
    @defaults ||= HashWithIndifferentAccess.new(
      YAML.load_file(
        Rails.root.join('config', 'configurable.yml')
      )
    )
  end

  def self.keys
    self.defaults.collect { |k,v| k.to_s }.sort
  end

  def self.[]=(key, value)
    exisiting = find_by_name(key)
    if exisiting
      exisiting.update_attribute(:value, value)
    else
      create {|c| c.name = key.to_s; c.value = value}
    end
  end

  def self.[](key)
    return parse_value key, defaults[key][:default] unless table_exists?

    val = if ConfigurableEngine::Engine.config.use_cache
      Rails.cache.fetch("configurable_engine:#{key}") {
        find_by_name(key).try(:value)
      }
    else
      find_by_name(key).try(:value)
    end

    val ||= parse_value key, defaults[key][:default]
  end

  def value
    self.class.parse_value name, super
  end

  def self.parse_value key, value
    case defaults[key][:type]
    when 'boolean'
      [true, 1, "1", "t", "true"].include?(value)
{'fraction_non_alphanumeric': 0.1191926884996192, 'fraction_numerical': 0.0015232292460015233, 'mean_word_length': 3.1632329635499206, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '2069488', 'n_tokens_mistral': 917, 'n_tokens_neox': 889, 'n_words': 188}