<commit_before>module Lucifer
  def self.included(base)
    base.extend ClassMethods
  end
  
  module ClassMethods
    def encrypt_attributes(options = {})
      return if self.included_modules.include? Lucifer::InstanceMethods
      __send__ :include, Lucifer::InstanceMethods
      
      cattr_accessor :encrypted_columns, :decrypted_columns, :key, :suffix, :key_file
      
      self.suffix   = options[:suffix]   || '_b'
      self.key_file = options[:key_file] || 'key.yml'
      
      self.encrypted_columns = columns.select{|col| col.type == :binary && col.name.ends_with?(suffix)}.collect(&:name)  
      self.decrypted_columns = encrypted_columns.collect{|col| col.chomp suffix }
      decrypted_columns.each { |col| attr_accessor col }
      
      before_save :encrypt_columns
      
      secret   = YAML.load_file(Rails.root + "/config/#{key_file}")[Rails.env].symbolize_keys
      self.key = EzCrypto::Key.with_password secret[:key], secret[:salt]
    end
    
    def encrypt(value)
      key.encrypt value
    end
    
    def decrypt(value)
      key.decrypt value
    end
  end
  
  
  module InstanceMethods
    def encrypt_columns
      self.class.decrypted_columns.each do |col|
        send "#{col}#{self.class.suffix}=", self.class.encrypt(eval(col))
      end
    end
    
    # Have to call it like this for performance reasons
    # http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html
    def after_find<commit_msg>Skip encryption if virutal attr is nil.<commit_after>module Lucifer
  def self.included(base)
    base.extend ClassMethods
  end
  
  module ClassMethods
    def encrypt_attributes(options = {})
      return if self.included_modules.include? Lucifer::InstanceMethods
      __send__ :include, Lucifer::InstanceMethods
      
      cattr_accessor :encrypted_columns, :decrypted_columns, :key, :suffix, :key_file
      
      self.suffix   = options[:suffix]   || '_b'
      self.key_file = options[:key_file] || 'key.yml'
      
      self.encrypted_columns = columns.select{|col| col.type == :binary && col.name.ends_with?(suffix)}.collect(&:name)  
      self.decrypted_columns = encrypted_columns.collect{|col| col.chomp suffix }
      decrypted_columns.each { |col| attr_accessor col }
      
      before_save :encrypt_columns
      
      secret   = YAML.load_file(Rails.root + "/config/#{key_file}")[Rails.env].symbolize_keys
      self.key = EzCrypto::Key.with_password secret[:key], secret[:salt]
    end
    
    def encrypt(value)
      key.encrypt value
    end
    
    def decrypt(value)
      key.decrypt value
    end
  end
  
  
  module InstanceMethods
    def encrypt_columns
      self.class.decrypted_columns.each do |col|
        value = eval col
        send "#{col}#{self.class.suffix}=", self.class.encrypt(value) unless value.nil?
      end
    end
    
    # Have to call it like this for performance reasons
    # http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html
    def after_find
{'fraction_non_alphanumeric': 0.10480349344978165, 'fraction_numerical': 0.0, 'mean_word_length': 2.9030144167758847, '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': '4636941', 'n_tokens_mistral': 954, 'n_tokens_neox': 929, 'n_words': 186}