module EncryptAttr::Base::ClassMethods

Public Instance Methods

attr_encrypt(*args, secret_token: EncryptAttr.secret_token, encryptor: EncryptAttr.encryptor)
Alias for: encrypt_attr
attr_encrypted(*args, secret_token: EncryptAttr.secret_token, encryptor: EncryptAttr.encryptor)
Alias for: encrypt_attr
attr_vault(*args, secret_token: EncryptAttr.secret_token, encryptor: EncryptAttr.encryptor)
Alias for: encrypt_attr
encrypt_attr(*args, secret_token: EncryptAttr.secret_token, encryptor: EncryptAttr.encryptor) click to toggle source
# File lib/encrypt_attr/base.rb, line 30
def encrypt_attr(*args, secret_token: EncryptAttr.secret_token, encryptor: EncryptAttr.encryptor)
  encryptor.validate_secret_token(secret_token) if encryptor.respond_to?(:validate_secret_token)

  args.each do |attribute|
    define_encrypted_attribute(attribute, secret_token, encryptor)
  end
end
encrypt_attribute(*args, secret_token: EncryptAttr.secret_token, encryptor: EncryptAttr.encryptor)
Alias for: encrypt_attr
encrypted_attr(*args, secret_token: EncryptAttr.secret_token, encryptor: EncryptAttr.encryptor)
Alias for: encrypt_attr
encrypted_attribute(*args, secret_token: EncryptAttr.secret_token, encryptor: EncryptAttr.encryptor)
Alias for: encrypt_attr

Private Instance Methods

define_encrypted_attribute(attribute, secret_token, encryptor) click to toggle source
# File lib/encrypt_attr/base.rb, line 47
def define_encrypted_attribute(attribute, secret_token, encryptor)
  define_method attribute do
    value = instance_variable_get("@#{attribute}")
    encrypted_value = send("encrypted_#{attribute}") unless value
    value = encryptor.decrypt(secret_token, encrypted_value) if encrypted_value
    instance_variable_set("@#{attribute}", value)
  end

  define_method "#{attribute}=" do |value|
    instance_variable_set("@#{attribute}", value)
    send("encrypted_#{attribute}=", nil)
    send("encrypted_#{attribute}=", encryptor.encrypt(secret_token, value)) if value && value != ""
  end
end