Module: AttrEncrypted::InstanceMethods

Defined in:
lib/attr_encrypted.rb

Instance Method Summary collapse

Instance Method Details

#attr_encrypted_decrypt(attribute, encrypted_value) ⇒ Object

Decrypts a value for the attribute specified using options evaluated in the current object’s scope

Example

class User
  attr_accessor :secret_key
  attr_encrypted :email, key: :secret_key

  def initialize(secret_key)
    self.secret_key = secret_key
  end
end

@user = User.new('some-secret-key')
@user.decrypt(:email, 'SOME_ENCRYPTED_EMAIL_STRING')


328
329
330
331
332
# File 'lib/attr_encrypted.rb', line 328

def attr_encrypted_decrypt(attribute, encrypted_value)
  attr_encrypted_encrypted_attributes[attribute.to_sym][:operation] = :decrypting
  attr_encrypted_encrypted_attributes[attribute.to_sym][:value_present] = self.class.not_empty?(encrypted_value)
  self.class.attr_encrypted_decrypt(attribute, encrypted_value, evaluated_attr_encrypted_options_for(attribute))
end

#attr_encrypted_encrypt(attribute, value) ⇒ Object

Encrypts a value for the attribute specified using options evaluated in the current object’s scope

Example

class User
  attr_accessor :secret_key
  attr_encrypted :email, key: :secret_key

  def initialize(secret_key)
    self.secret_key = secret_key
  end
end

@user = User.new('some-secret-key')
@user.attr_encrypted_encrypt(:email, '[email protected]')


349
350
351
352
353
# File 'lib/attr_encrypted.rb', line 349

def attr_encrypted_encrypt(attribute, value)
  attr_encrypted_encrypted_attributes[attribute.to_sym][:operation] = :encrypting
  attr_encrypted_encrypted_attributes[attribute.to_sym][:value_present] = self.class.not_empty?(value)
  self.class.attr_encrypted_encrypt(attribute, value, evaluated_attr_encrypted_options_for(attribute))
end

#attr_encrypted_encrypted_attributesObject

Copies the class level hash of encrypted attributes with virtual attribute names as keys and their corresponding options as values to the instance



358
359
360
361
362
363
364
# File 'lib/attr_encrypted.rb', line 358

def attr_encrypted_encrypted_attributes
  @attr_encrypted_encrypted_attributes ||= begin
    duplicated= {}
    self.class.attr_encrypted_encrypted_attributes.map { |key, value| duplicated[key] = value.dup }
    duplicated
  end
end