Module: Encryptor::ClassMethods
- Defined in:
- lib/encryptor.rb
Instance Method Summary collapse
-
#encrypted(attribute_name, encrypted_attribute_name = nil, key_name = nil) ⇒ Object
Preconditions => attribute_name - symbol, it is the name of the virtual attribute you want to create => (optional) encrypted_attribute_name - symbol, it is the name of the encrypted attribute => (optional) key_name - symbol, it is the name of the key Postconditions => #attribute_name instance method is created and handles decryption => #attribute_name= instance method is created and handles encryption.
Instance Method Details
#encrypted(attribute_name, encrypted_attribute_name = nil, key_name = nil) ⇒ Object
Preconditions
> attribute_name - symbol, it is the name of the virtual attribute you want to create
> (optional) encrypted_attribute_name - symbol, it is the name of the encrypted attribute
> (optional) key_name - symbol, it is the name of the key
Postconditions
> #attribute_name instance method is created and handles decryption
> #attribute_name= instance method is created and handles encryption
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/encryptor.rb', line 17 def encrypted(attribute_name, encrypted_attribute_name = nil, key_name = nil) encrypted_attribute_name = encrypted_attribute_name.present? ? encrypted_attribute_name : "encrypted_#{attribute_name}" key_name = key_name.present? ? key_name : "#{attribute_name}_key" class_eval do define_method :"#{attribute_name}" do decrypt(self.send(encrypted_attribute_name), self.send(key_name)) end define_method :"#{attribute_name}=" do |value_to_encrypt| encrypted_parts = encrypt(value_to_encrypt) key = encrypted_parts[:key] encrypted_attribute = encrypted_parts[:encrypted_attribute] self.send("#{key_name}=", key) self.send("#{encrypted_attribute_name}=", encrypted_attribute) end end end |