yattr_encrypted 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.mdown CHANGED
@@ -1,6 +1,6 @@
1
1
  # YattrEncrypted #
2
2
 
3
- Version: 0.1.4 (but you should check lib/yattr_encrypted/version.rb to be sure)
3
+ Version: 0.1.5 (but you should check lib/yattr_encrypted/version.rb to be sure)
4
4
 
5
5
  ## Applicability ##
6
6
 
@@ -139,9 +139,26 @@ Special processing for attribute values can be implemented by using the `:read_f
139
139
  A `:read_filter` is a proc (or lambda) which accepts a single argument. It is called on the
140
140
  value of the *attribute* immediately before being returned by the clear text attribute accessor.
141
141
 
142
+ **NOTE:** The read filter is called every time the *attribute* is read via the attribute
143
+ reader. It should be idempotent in the sense that read_filter(attribute) == read_filter(read_filter(attribute)).
144
+
145
+ **NOTE:** The read filter result is reflected in the instance variable which supports the
146
+ plain text version of the *attribute*, so it can be used to set the *attribute* to a default
147
+ value - such as an empty Hash.
148
+
149
+ yattr_encrypted :bag, :read_filter => lambda { |val| val.is_a?(Hash) ? val : {} }
150
+
142
151
  A `:write_filter` is a proc (or lambda) which accepts a single argument. It is called
143
152
  on the value passed to the *attribute* writer prior to any other action in the writer.
144
153
 
154
+ **NOTE:** the write filter is called on every value which is assigned to the *attribute*.
155
+
156
+ You can use a `:write_filter` to do some standard preprocessing on values. For example,
157
+ if you want to normalize some string of text to lower case, with uniform whitespace, you might
158
+ do something like:
159
+
160
+ yattr_encrypted :str_value, :write_filter => lambda { |val| val.to_s.sub(/\s+/, ' ').downcase.strip }
161
+
145
162
  ### Encription Initial Values ###
146
163
 
147
164
  As stated everywhere - random initial values are automatically generated for all fields.
@@ -76,7 +76,6 @@ module YattrEncrypted
76
76
 
77
77
  # iterate through attributes and create accessors, verify encryped accessors exist
78
78
  attributes.map { |x| x.to_sym }.each do |attribute|
79
- puts "yate_encrypted: #{attribute}"
80
79
  encrypted_attribute_name = [options[:prefix], attribute, options[:suffix]].join.to_sym
81
80
 
82
81
  # barf if reader and write doesn't exist for encrypted attribute
@@ -88,14 +87,17 @@ puts "yate_encrypted: #{attribute}"
88
87
  tmp =<<-XXX
89
88
  def #{attribute}
90
89
  options = yate_encrypted_attributes[:#{attribute}]
91
- unless @#{attribute} && !@#{attribute}.empty?
90
+ if @#{attribute}.nil? || @#{attribute}.empty?
92
91
  @#{attribute} = #{encrypted_attribute_name} ? \
93
92
  yate_decrypt(#{encrypted_attribute_name}, options[:key]) : \
94
93
  ''
94
+ @#{attribute} = options[:read_filter].call(@#{attribute}) if options[:read_filter]
95
95
  self.yate_checksums[:#{attribute}] = yate_attribute_hash_value(:#{attribute})
96
96
  self.yate_dirty[:#{attribute}] = true
97
+ elsif options[:read_filter]
98
+ @#{attribute} = options[:read_filter].call(@#{attribute})
97
99
  end
98
- options[:read_filter] ? options[:read_filter].call(@#{attribute}) : @#{attribute}
100
+ @#{attribute}
99
101
  end
100
102
  XXX
101
103
  class_eval(tmp)
@@ -1,3 +1,3 @@
1
1
  module YattrEncrypted
2
- VERSION = '0.1.4'
2
+ VERSION = '0.1.5'
3
3
  end
@@ -40,7 +40,7 @@ class SomeClass < ActiveRecord::Base
40
40
  yattr_encrypted :special_reader, :key => 'a honkin big key: honk honk honk honk honk',
41
41
  :read_filter => lambda { |val| val.strip }
42
42
  yattr_encrypted :special_writer, :key => 'a honkin big key: honk honk honk honk honk',
43
- :write_filter => lambda { |val| puts val; val.upcase }
43
+ :write_filter => lambda { |val| val.upcase }
44
44
  end
45
45
 
46
46
  class TestYattrEncrypted < MiniTest::Unit::TestCase
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yattr_encrypted
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-03-19 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: pry
16
- requirement: &2151769420 !ruby/object:Gem::Requirement
16
+ requirement: &2151769620 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *2151769420
24
+ version_requirements: *2151769620
25
25
  description: Generates yattr_accessors that encrypt and decrypt attributes transparently.
26
26
  Based on attr_encrypted by Sean Huber [https://github.com/shuber]
27
27
  email: mike@clove.com
@@ -56,7 +56,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
56
56
  version: '0'
57
57
  segments:
58
58
  - 0
59
- hash: 2285454077099666257
59
+ hash: 677455391031981630
60
60
  required_rubygems_version: !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements: