Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

fixed bugs in :read_filter & :write_filter; added better notes and

examples to README.mdown for the filters.
  • Loading branch information...
commit 48ce32b6c5af2ef4bbba2d66556d4d00a755713d 1 parent 46eca8f
Mike Howard authored
19 README.mdown
View
@@ -1,6 +1,6 @@
# YattrEncrypted #
-Version: 0.1.5.pre1 (but you should check lib/yattr_encrypted/version.rb to be sure)
+Version: 0.1.5 (but you should check lib/yattr_encrypted/version.rb to be sure)
## Applicability ##
@@ -139,9 +139,26 @@ Special processing for attribute values can be implemented by using the `:read_f
A `:read_filter` is a proc (or lambda) which accepts a single argument. It is called on the
value of the *attribute* immediately before being returned by the clear text attribute accessor.
+**NOTE:** The read filter is called every time the *attribute* is read via the attribute
+reader. It should be idempotent in the sense that read_filter(attribute) == read_filter(read_filter(attribute)).
+
+**NOTE:** The read filter result is reflected in the instance variable which supports the
+plain text version of the *attribute*, so it can be used to set the *attribute* to a default
+value - such as an empty Hash.
+
+ yattr_encrypted :bag, :read_filter => lambda { |val| val.is_a?(Hash) ? val : {} }
+
A `:write_filter` is a proc (or lambda) which accepts a single argument. It is called
on the value passed to the *attribute* writer prior to any other action in the writer.
+**NOTE:** the write filter is called on every value which is assigned to the *attribute*.
+
+You can use a `:write_filter` to do some standard preprocessing on values. For example,
+if you want to normalize some string of text to lower case, with uniform whitespace, you might
+do something like:
+
+ yattr_encrypted :str_value, :write_filter => lambda { |val| val.to_s.sub(/\s+/, ' ').downcase.strip }
+
### Encription Initial Values ###
As stated everywhere - random initial values are automatically generated for all fields.
7 lib/yattr_encrypted.rb
View
@@ -87,14 +87,17 @@ def yattr_encrypted(*attributes)
tmp =<<-XXX
def #{attribute}
options = yate_encrypted_attributes[:#{attribute}]
- unless @#{attribute} && !@#{attribute}.empty?
+ if @#{attribute}.nil? || @#{attribute}.empty?
@#{attribute} = #{encrypted_attribute_name} ? \
yate_decrypt(#{encrypted_attribute_name}, options[:key]) : \
''
+ @#{attribute} = options[:read_filter].call(@#{attribute}) if options[:read_filter]
self.yate_checksums[:#{attribute}] = yate_attribute_hash_value(:#{attribute})
self.yate_dirty[:#{attribute}] = true
+ elsif options[:read_filter]
+ @#{attribute} = options[:read_filter].call(@#{attribute})
end
- options[:read_filter] ? options[:read_filter].call(@#{attribute}) : @#{attribute}
+ @#{attribute}
end
XXX
class_eval(tmp)
2  lib/yattr_encrypted/version.rb
View
@@ -1,3 +1,3 @@
module YattrEncrypted
- VERSION = '0.1.5.pre1'
+ VERSION = '0.1.5'
end
Please sign in to comment.
Something went wrong with that request. Please try again.