Module: HasEncryptedField::ClassMethods

Defined in:
lib/has_encrypted_field/accessors.rb,
lib/has_encrypted_field/query_methods.rb

Overview

ClassMethods

These methods are added to classes that include the HasEncryptedField module.

Instance Method Summary collapse

Instance Method Details

#has_encrypted_field(attribute) ⇒ Object

rubocop:disable Naming/PredicateName



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/has_encrypted_field/accessors.rb', line 12

def has_encrypted_field(attribute)
  class_eval(<<~ACCESSORS, __FILE__, __LINE__ + 1)
    def decrypted_#{attribute}
      HasEncryptedField.encryptor.decrypt_and_verify(self[:#{attribute}])
    end

    def #{attribute}=(value)
      self[:#{attribute}] = HasEncryptedField.encryptor.encrypt_and_sign(value)
    end
  ACCESSORS

  HasEncryptedField.register(to_s, attribute)
end

#pluck_decrypted(*attributes) ⇒ Object



11
12
13
# File 'lib/has_encrypted_field/query_methods.rb', line 11

def pluck_decrypted(*attributes)
  attributes.length == 1 ? decrypt_simple(attributes) : decrypt_nested(attributes)
end