lib/huberry/class.rb in shuber-attr_encrypted-1.0.0 vs lib/huberry/class.rb in shuber-attr_encrypted-1.0.1

- old
+ new

@@ -24,16 +24,19 @@ # :key => The encryption key. This option may not be required if you're using a custom encryptor. If you pass # a symbol representing an instance method then the :key option will be replaced with the result of the # method before being passed to the encryptor. Proc objects are evaluated as well. Any other key types # will be passed directly to the encryptor. # - # :encode => If set to true, attributes will be base64 encoded as well as encrypted. This is useful if you're - # planning on storing the encrypted attributes in a database. Defaults to false unless you're using - # it with ActiveRecord. + # :encode => If set to true, attributes will be encoded as well as encrypted. This is useful if you're + # planning on storing the encrypted attributes in a database. The default encoding is 'm*' (base64), + # however this can be overwritten by setting the :encode option to some other encoding string instead of + # just 'true'. See http://www.ruby-doc.org/core/classes/Array.html#M002245 for more encoding directives. + # Defaults to false unless you're using it with ActiveRecord or DataMapper. # # :marshal => If set to true, attributes will be marshaled as well as encrypted. This is useful if you're planning - # on encrypting something other than a string. Defaults to false unless you're using it with ActiveRecord. + # on encrypting something other than a string. Defaults to false unless you're using it with ActiveRecord + # or DataMapper. # # :encryptor => The object to use for encrypting. Defaults to Huberry::Encryptor. # # :encrypt_method => The encrypt method name to call on the <tt>:encryptor</tt> object. Defaults to :encrypt. # @@ -73,34 +76,35 @@ :encrypt_method => :encrypt, :decrypt_method => :decrypt, :encode => false, :marshal => false }.merge(attr_encrypted_options).merge(attrs.last.is_a?(Hash) ? attrs.pop : {}) + options[:encode] = 'm*' if options[:encode] == true attrs.each do |attribute| encrypted_attribute_name = options[:attribute].nil? ? options[:prefix].to_s + attribute.to_s + options[:suffix].to_s : options[:attribute].to_s encrypted_attributes[attribute.to_s] = encrypted_attribute_name - attr_accessor encrypted_attribute_name.to_sym unless self.new.respond_to?(encrypted_attribute_name) + attr_accessor encrypted_attribute_name.to_sym unless instance_methods.include?(encrypted_attribute_name) define_class_method "encrypt_#{attribute}" do |value| if value.nil? encrypted_value = nil else value = Marshal.dump(value) if options[:marshal] encrypted_value = options[:encryptor].send options[:encrypt_method], options.merge(:value => value) - encrypted_value = [encrypted_value].pack('m*') if options[:encode] + encrypted_value = [encrypted_value].pack(options[:encode]) if options[:encode] end encrypted_value end define_class_method "decrypt_#{attribute}" do |encrypted_value| if encrypted_value.nil? decrypted_value = nil else - encrypted_value = encrypted_value.unpack('m*').to_s if options[:encode] + encrypted_value = encrypted_value.unpack(options[:encode]).to_s if options[:encode] decrypted_value = options[:encryptor].send(options[:decrypt_method], options.merge(:value => encrypted_value)) decrypted_value = Marshal.load(decrypted_value) if options[:marshal] end decrypted_value end @@ -108,10 +112,10 @@ define_method "#{attribute}" do value = instance_variable_get("@#{attribute}") encrypted_value = read_attribute(encrypted_attribute_name) original_key = options[:key] options[:key] = self.class.send :evaluate_attr_encrypted_key, options[:key], self - value = write_attribute(attribute, self.class.send("decrypt_#{attribute}".to_sym, encrypted_value)) if value.nil? && !encrypted_value.nil? + value = instance_variable_set("@#{attribute}", self.class.send("decrypt_#{attribute}".to_sym, encrypted_value)) if value.nil? && !encrypted_value.nil? options[:key] = original_key value end define_method "#{attribute}=" do |value| \ No newline at end of file