class MySQLEncryptor include MySQLEncryption include Singleton def encrypt(encrypt_options) value_to_encrypt = encrypt_options[:value].nil? ? nil : encrypt_options[:type] == 'date' ? encrypt_date(encrypt_options[:value]) : encrypt_options[:value].to_s mysql_encrypt(value_to_encrypt, encrypt_options[:key]) end def decrypt(encrypt_options) decrypted_value = mysql_decrypt(encrypt_options[:value], encrypt_options[:key]) return decrypted_value if decrypted_value.nil? case encrypt_options[:type] when 'text' decrypted_value.force_encoding('utf-8') when 'date' Date.parse(decrypted_value) when 'datetime' DateTime.parse(decrypted_value) when 'binary' decrypted_value # no processing else raise "Invalid type specified for post-processing decrypted value" end end def encrypt_date(value) dt = value.is_a?(String) ? Date.safe_parse(value) : value dt.strftime("%Y%m%d") if dt.present? end end