yattr_encrypted 0.1.5 → 0.1.6

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.5 (but you should check lib/yattr_encrypted/version.rb to be sure)
3
+ Version: 0.1.6 (but you should check lib/yattr_encrypted/version.rb to be sure)
4
4
 
5
5
  ## Applicability ##
6
6
 
@@ -53,6 +53,9 @@ random *iv* and including it in the encrypted data. See *openssl* documentation
53
53
  for details [OpenSSL::Cipher]
54
54
  * detects when fields are modified by actions other than assignment. This supports
55
55
  encrypting complex types - such as hashes and arrays. This is implemented by adding
56
+ * supports special field processing and initialization by use of `:read_filter` and
57
+ `:write_filter` options which define Proc's or methods which are run on during
58
+ read and write accessors for plaintext versions of the encrypted fields.
56
59
  a `before_save` calleback to the private method *yattr_update_encrypted_values*
57
60
  * Rails 3.1 & Rails 3.2 - doesn't pretend to support anything lower (but it might work)
58
61
 
@@ -131,24 +134,31 @@ and 'this is a very long secret key' is not.
131
134
 
132
135
  If you supply your own key, it can be a String or a Proc which returns a String.
133
136
 
134
- ### Special Attribute Processing ###
137
+ ### Special Attribute Processing: read\_filter and write\_filter ###
135
138
 
136
139
  Special processing for attribute values can be implemented by using the `:read_filter` and
137
140
  `:write_filter` options of the `yattr_encrypted` macro.
138
141
 
139
- A `:read_filter` is a proc (or lambda) which accepts a single argument. It is called on the
140
- value of the *attribute* immediately before being returned by the clear text attribute accessor.
142
+ Both options take *either* a Proc, lambda, or instance method name. In both cases, the callable
143
+ must take a single argument. The argument will be the plaintext value of the field or value
144
+ being assigned.
145
+
146
+ A `:read_filter` is called on the value of the *attribute* before being returned by the
147
+ clear text attribute accessor. The processed value is saved in the instance variable
148
+ used to support the plaintext version of the *attribute*.
141
149
 
142
150
  **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)).
151
+ reader. It should be idempotent in the sense that:
152
+
153
+ read_filter(attribute) == read_filter(read_filter(attribute)).
144
154
 
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
155
+ **NOTE:** Because the read filter result is saved in the instance variable which supports the
156
+ plain text version of the *attribute*, it can be used to set the *attribute* to a default
147
157
  value - such as an empty Hash.
148
158
 
149
159
  yattr_encrypted :bag, :read_filter => lambda { |val| val.is_a?(Hash) ? val : {} }
150
160
 
151
- A `:write_filter` is a proc (or lambda) which accepts a single argument. It is called
161
+ A `:write_filter` is a proc, lambda, or instance method which accepts a single argument. It is called
152
162
  on the value passed to the *attribute* writer prior to any other action in the writer.
153
163
 
154
164
  **NOTE:** the write filter is called on every value which is assigned to the *attribute*.
@@ -73,7 +73,7 @@ module YattrEncrypted
73
73
 
74
74
  # collect existing instance methods
75
75
  instance_methods_as_symbols = instance_methods.map { |method| method.to_sym }
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
79
  encrypted_attribute_name = [options[:prefix], attribute, options[:suffix]].join.to_sym
@@ -91,11 +91,19 @@ module YattrEncrypted
91
91
  @#{attribute} = #{encrypted_attribute_name} ? \
92
92
  yate_decrypt(#{encrypted_attribute_name}, options[:key]) : \
93
93
  ''
94
- @#{attribute} = options[:read_filter].call(@#{attribute}) if options[:read_filter]
94
+ if options[:read_filter].respond_to? :call
95
+ @#{attribute} = options[:read_filter].call(@#{attribute})
96
+ elsif options[:read_filter]
97
+ @#{attribute} = self.send options[:read_filter].to_sym, @#{attribute}
98
+ end
95
99
  self.yate_checksums[:#{attribute}] = yate_attribute_hash_value(:#{attribute})
96
100
  self.yate_dirty[:#{attribute}] = true
97
101
  elsif options[:read_filter]
98
- @#{attribute} = options[:read_filter].call(@#{attribute})
102
+ if options[:read_filter].respond_to? :call
103
+ @#{attribute} = options[:read_filter].call(@#{attribute})
104
+ else
105
+ @#{attribute} = self.send options[:read_filter].to_sym, @#{attribute}
106
+ end
99
107
  end
100
108
  @#{attribute}
101
109
  end
@@ -105,7 +113,11 @@ module YattrEncrypted
105
113
  tmp =<<-XXX
106
114
  def #{attribute}= value
107
115
  options = yate_encrypted_attributes[:#{attribute}]
108
- value = options[:write_filter].call(value) if options[:write_filter]
116
+ if options[:write_filter].respond_to? :call
117
+ value = options[:write_filter].call(value)
118
+ elsif options[:write_filter]
119
+ value = self.send options[:write_filter], value
120
+ end
109
121
  @#{attribute} = value
110
122
  self.#{encrypted_attribute_name} = yate_encrypt(value, options[:key])
111
123
  self.yate_checksums[:#{attribute}] = yate_attribute_hash_value(:#{attribute})
@@ -1,3 +1,3 @@
1
1
  module YattrEncrypted
2
- VERSION = '0.1.5'
2
+ VERSION = '0.1.6'
3
3
  end
@@ -35,12 +35,19 @@ module ActiveRecord
35
35
  end
36
36
 
37
37
  class SomeClass < ActiveRecord::Base
38
- attr_accessor :field_encrypted, :special_reader_encrypted, :special_writer_encrypted
38
+ attr_accessor :field_encrypted, :special_reader_encrypted, :special_writer_encrypted,
39
+ :method_filtered_encrypted
39
40
  yattr_encrypted :field, :key => 'a honkin big key: honk honk honk honk honk'
40
41
  yattr_encrypted :special_reader, :key => 'a honkin big key: honk honk honk honk honk',
41
42
  :read_filter => lambda { |val| val.strip }
42
43
  yattr_encrypted :special_writer, :key => 'a honkin big key: honk honk honk honk honk',
43
44
  :write_filter => lambda { |val| val.upcase }
45
+ yattr_encrypted :method_filtered, :key => 'a honkin big key: honk honk honk honk honk',
46
+ :read_filter => 'foo'
47
+
48
+ def foo attr
49
+ 'foo filtered'
50
+ end
44
51
  end
45
52
 
46
53
  class TestYattrEncrypted < MiniTest::Unit::TestCase
@@ -94,4 +101,12 @@ class TestYattrEncrypted < MiniTest::Unit::TestCase
94
101
  "@special_writer should be upcased"
95
102
  assert_equal value.upcase, @sc.special_writer, "@sc.special_writer should be upcased"
96
103
  end
104
+
105
+ def test_method_filtered
106
+ value = 'a string with leading and trailing white space'
107
+ @sc.method_filtered = value
108
+ assert_equal value, @sc.instance_variable_get(:@method_filtered),
109
+ "@method_filtered should be '#{value}'"
110
+ assert_equal 'foo filtered', @sc.method_filtered, "@sc.method_filtered should be 'foo filtered"
111
+ end
97
112
  end
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.5
4
+ version: 0.1.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-03-19 00:00:00.000000000Z
12
+ date: 2012-03-20 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: pry
16
- requirement: &2151769620 !ruby/object:Gem::Requirement
16
+ requirement: &2157537060 !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: *2151769620
24
+ version_requirements: *2157537060
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: 677455391031981630
59
+ hash: -1485568723198089794
60
60
  required_rubygems_version: !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements: