diff --git a/lib/mongoid/encryptor.rb b/lib/mongoid/encryptor.rb index 8e53dcf..360eb09 100644 --- a/lib/mongoid/encryptor.rb +++ b/lib/mongoid/encryptor.rb @@ -32,51 +32,49 @@ def encrypts(*attrs) end end - module InstanceMethods #:nodoc: - # Returns decrypted value for key. - # - # @param [String] key - # @return [Object] - def read_attribute_for_validation(key) - v = read_attribute(key) - v.try(:encrypted?) ? v.decrypt : v - end + # Returns decrypted value for key. + # + # @param [String] key + # @return [Object] + def read_attribute_for_validation(key) + v = read_attribute(key) + v.try(:encrypted?) ? v.decrypt : v + end - private + private - # @param [String] attr_name - # @param [Class] cipher_class - # @param [Hash] options - def write_encrypted_attribute(attr_name, cipher_class, options) - value = read_attribute(attr_name.to_sym) - return if value.blank? or value.encrypted? + # @param [String] attr_name + # @param [Class] cipher_class + # @param [Hash] options + def write_encrypted_attribute(attr_name, cipher_class, options) + value = read_attribute(attr_name.to_sym) + return if value.blank? or value.encrypted? - cipher = instantiate_cipher(cipher_class, options) - value = cipher.encrypt(value) - value.cipher = cipher - send("#{attr_name}=", value) - end + cipher = instantiate_cipher(cipher_class, options) + value = cipher.encrypt(value) + value.cipher = cipher + send("#{attr_name}=", value) + end - # @param [String] attr_name - # @param [Class] cipher_class - # @param [Hash] options - # @return [String] - def read_encrypted_attribute(attr_name, cipher_class, options) - value = read_attribute(attr_name) + # @param [String] attr_name + # @param [Class] cipher_class + # @param [Hash] options + # @return [String] + def read_encrypted_attribute(attr_name, cipher_class, options) + value = read_attribute(attr_name) - unless value.blank? || value.encrypted? || attribute_changed?(attr_name) || new_record? - value.cipher = instantiate_cipher(cipher_class, options) - end - - value + unless value.blank? || value.encrypted? || attribute_changed?(attr_name) || new_record? + value.cipher = instantiate_cipher(cipher_class, options) end - # @param [Class] cipher_class - # @param [Hash] options - # @return [EncryptedStrings::Cipher] - def instantiate_cipher(cipher_class, options) - cipher_class.new(options.dup) - end + value + end + + # @param [Class] cipher_class + # @param [Hash] options + # @return [EncryptedStrings::Cipher] + def instantiate_cipher(cipher_class, options) + cipher_class.new(options.dup) end end