Class: SymmetricEncryption::ActiveRecord::EncryptedAttribute

Inherits:
ActiveModel::Type::String
  • Object
show all
Defined in:
lib/symmetric_encryption/active_record/encrypted_attribute.rb

Instance Method Summary collapse

Constructor Details

#initialize(random_iv: true, compress: false, type: :string) ⇒ EncryptedAttribute

Returns a new instance of EncryptedAttribute.



4
5
6
7
8
# File 'lib/symmetric_encryption/active_record/encrypted_attribute.rb', line 4

def initialize(random_iv: true, compress: false, type: :string)
  @random_iv      = random_iv
  @compress       = compress
  @encrypted_type = type
end

Instance Method Details

#changed_in_place?(raw_old_value, new_value) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/symmetric_encryption/active_record/encrypted_attribute.rb', line 27

def changed_in_place?(raw_old_value, new_value)
  deserialize(raw_old_value) != new_value
end

#deserialize(value) ⇒ Object



10
11
12
13
14
# File 'lib/symmetric_encryption/active_record/encrypted_attribute.rb', line 10

def deserialize(value)
  return if value.nil?

  SymmetricEncryption.decrypt(value, type: encrypted_type)
end

#serialize(value) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/symmetric_encryption/active_record/encrypted_attribute.rb', line 16

def serialize(value)
  return if value.nil?

  SymmetricEncryption.encrypt(
    value,
    type:      encrypted_type,
    compress:  compress,
    random_iv: random_iv
  )
end