lib/huberry/attr_encrypted/class.rb in shuber-attr_encrypted-1.0.3 vs lib/huberry/attr_encrypted/class.rb in shuber-attr_encrypted-1.0.4

- old
+ new

@@ -87,18 +87,19 @@ :marshal => false, :if => true, :unless => 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 instance_methods.include?(encrypted_attribute_name) - + + attr_reader encrypted_attribute_name.to_sym unless instance_methods.include?(encrypted_attribute_name) + attr_writer encrypted_attribute_name.to_sym unless instance_methods.include?("#{encrypted_attribute_name}=") + define_class_method "encrypt_#{attribute}" do |value| if options[:if] && !options[:unless] if value.nil? encrypted_value = nil else @@ -109,11 +110,11 @@ encrypted_value else value end end - + define_class_method "decrypt_#{attribute}" do |encrypted_value| if options[:if] && !options[:unless] if encrypted_value.nil? decrypted_value = nil else @@ -124,36 +125,36 @@ decrypted_value else encrypted_value end end - + define_method "#{attribute}" do value = instance_variable_get("@#{attribute}") - encrypted_value = read_attribute(encrypted_attribute_name) + encrypted_value = send(encrypted_attribute_name.to_s) original_options = [:key, :if, :unless].inject({}) do |hash, option| hash[option] = options[option] options[option] = self.class.send :evaluate_attr_encrypted_option, options[option], self hash end value = instance_variable_set("@#{attribute}", self.class.send("decrypt_#{attribute}".to_sym, encrypted_value)) if value.nil? && !encrypted_value.nil? options.merge!(original_options) value end - + define_method "#{attribute}=" do |value| original_options = [:key, :if, :unless].inject({}) do |hash, option| hash[option] = options[option] options[option] = self.class.send :evaluate_attr_encrypted_option, options[option], self hash end - write_attribute(encrypted_attribute_name, self.class.send("encrypt_#{attribute}".to_sym, value)) + send("#{encrypted_attribute_name}=".to_sym, self.class.send("encrypt_#{attribute}".to_sym, value)) options.merge!(original_options) instance_variable_set("@#{attribute}", value) end end end - + # Evaluates an option specified as a symbol representing an instance method or a proc # If the option is not a symbol or proc then the original option is returned def evaluate_attr_encrypted_option(option, object) if option.is_a?(Symbol) && object.respond_to?(option) object.send(option) \ No newline at end of file