Module: AttrEncryption::InstanceMethods

Defined in:
lib/attr_encryption.rb

Instance Method Summary collapse

Instance Method Details

#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')


285
286
287
# File 'lib/attr_encryption.rb', line 285

def decrypt(attribute, encrypted_value)
  self.class.decrypt(attribute, encrypted_value, evaluated_attr_encrypted_options_for(attribute))
end

#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.encrypt(:email, '[email protected]')


304
305
306
# File 'lib/attr_encryption.rb', line 304

def encrypt(attribute, value)
  self.class.encrypt(attribute, value, evaluated_attr_encrypted_options_for(attribute))
end

#unencrypted_attributesObject



308
309
310
311
312
313
314
315
316
317
318
# File 'lib/attr_encryption.rb', line 308

def unencrypted_attributes
  attributes.each_with_object({}) do |a, new_hash|
    key = a.first
    value = a.last
    if key =~ /\A(.+)_enc\z/
      key = $1
      value = decrypt(key.to_sym, value)
    end
    new_hash[key] = value
  end   
end