attr_encrypted 1.1.0 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,6 @@
1
1
  require 'eigenclass'
2
2
  require 'encryptor'
3
+ require 'ruby-debug'
3
4
 
4
5
  module AttrEncrypted
5
6
  def self.extended(base)
@@ -177,26 +178,32 @@ module AttrEncrypted
177
178
  end
178
179
 
179
180
  define_method "#{attribute}" do
180
- value = instance_variable_get("@#{attribute}")
181
- encrypted_value = send(encrypted_attribute_name.to_sym)
182
- original_options = [:key, :if, :unless].inject({}) do |hash, option|
183
- hash[option] = options[option]
184
- options[option] = self.class.send :evaluate_attr_encrypted_option, options[option], self
185
- hash
181
+ begin
182
+ value = instance_variable_get("@#{attribute}")
183
+ encrypted_value = send(encrypted_attribute_name.to_sym)
184
+ original_options = [:key, :if, :unless].inject({}) do |hash, option|
185
+ hash[option] = options[option]
186
+ options[option] = self.class.send :evaluate_attr_encrypted_option, options[option], self
187
+ hash
188
+ end
189
+ value = instance_variable_set("@#{attribute}", self.class.send("decrypt_#{attribute}".to_sym, encrypted_value)) if value.nil? && !encrypted_value.nil?
190
+ ensure
191
+ options.merge!(original_options)
186
192
  end
187
- value = instance_variable_set("@#{attribute}", self.class.send("decrypt_#{attribute}".to_sym, encrypted_value)) if value.nil? && !encrypted_value.nil?
188
- options.merge!(original_options)
189
193
  value
190
194
  end
191
195
 
192
196
  define_method "#{attribute}=" do |value|
193
- original_options = [:key, :if, :unless].inject({}) do |hash, option|
194
- hash[option] = options[option]
195
- options[option] = self.class.send :evaluate_attr_encrypted_option, options[option], self
196
- hash
197
+ begin
198
+ original_options = [:key, :if, :unless].inject({}) do |hash, option|
199
+ hash[option] = options[option]
200
+ options[option] = self.class.send :evaluate_attr_encrypted_option, options[option], self
201
+ hash
202
+ end
203
+ send("#{encrypted_attribute_name}=".to_sym, self.class.send("encrypt_#{attribute}".to_sym, value))
204
+ ensure
205
+ options.merge!(original_options)
197
206
  end
198
- send("#{encrypted_attribute_name}=".to_sym, self.class.send("encrypt_#{attribute}".to_sym, value))
199
- options.merge!(original_options)
200
207
  instance_variable_set("@#{attribute}", value)
201
208
  end
202
209
  end
@@ -1,4 +1,5 @@
1
1
  require File.dirname(__FILE__) + '/test_helper'
2
+ require 'mocha'
2
3
 
3
4
  class SillyEncryptor
4
5
  def self.silly_encrypt(options)
@@ -24,13 +25,16 @@ class User
24
25
  attr_encrypted :with_false_if, :key => 'secret key', :if => false
25
26
  attr_encrypted :with_true_unless, :key => 'secret key', :unless => true
26
27
  attr_encrypted :with_false_unless, :key => 'secret key', :unless => false
28
+ attr_encrypted :with_if_changed, :key => 'secret key', :if => :should_encrypt
27
29
 
28
30
  attr_encryptor :aliased, :key => 'secret_key'
29
31
 
30
32
  attr_accessor :salt
33
+ attr_accessor :should_encrypt
31
34
 
32
35
  def initialize
33
36
  self.salt = Time.now.to_i.to_s
37
+ self.should_encrypt = true
34
38
  end
35
39
  end
36
40
 
@@ -249,4 +253,20 @@ class AttrEncryptedTest < Test::Unit::TestCase
249
253
  assert User.encrypted_attributes.include?('aliased')
250
254
  end
251
255
 
256
+ def test_should_always_reset_options
257
+ @user = User.new
258
+ @user.with_if_changed = "encrypt_stuff"
259
+ @user.stubs(:instance_variable_get).returns(nil)
260
+ @user.stubs(:instance_variable_set).raises("BadStuff")
261
+ assert_raise RuntimeError do
262
+ @user.with_if_changed
263
+ end
264
+
265
+ @user = User.new
266
+ @user.should_encrypt = false
267
+ @user.with_if_changed = "not_encrypted_stuff"
268
+ assert_equal "not_encrypted_stuff", @user.with_if_changed
269
+ assert_equal "not_encrypted_stuff", @user.encrypted_with_if_changed
270
+ end
271
+
252
272
  end
metadata CHANGED
@@ -1,7 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: attr_encrypted
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 1
8
+ - 1
9
+ version: 1.1.1
5
10
  platform: ruby
6
11
  authors:
7
12
  - Sean Huber
@@ -9,29 +14,51 @@ autorequire:
9
14
  bindir: bin
10
15
  cert_chain: []
11
16
 
12
- date: 2010-01-28 00:00:00 -08:00
17
+ date: 2010-04-26 00:00:00 -07:00
13
18
  default_executable:
14
19
  dependencies:
15
20
  - !ruby/object:Gem::Dependency
16
21
  name: eigenclass
17
- type: :runtime
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
20
24
  requirements:
21
25
  - - ">="
22
26
  - !ruby/object:Gem::Version
27
+ segments:
28
+ - 1
29
+ - 1
30
+ - 1
23
31
  version: 1.1.1
24
- version:
32
+ type: :runtime
33
+ version_requirements: *id001
25
34
  - !ruby/object:Gem::Dependency
26
35
  name: encryptor
27
- type: :runtime
28
- version_requirement:
29
- version_requirements: !ruby/object:Gem::Requirement
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
30
38
  requirements:
31
39
  - - ">="
32
40
  - !ruby/object:Gem::Version
41
+ segments:
42
+ - 1
43
+ - 1
44
+ - 0
33
45
  version: 1.1.0
34
- version:
46
+ type: :runtime
47
+ version_requirements: *id002
48
+ - !ruby/object:Gem::Dependency
49
+ name: mocha
50
+ prerelease: false
51
+ requirement: &id003 !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ segments:
56
+ - 0
57
+ - 9
58
+ - 8
59
+ version: 0.9.8
60
+ type: :runtime
61
+ version_requirements: *id003
35
62
  description: Generates attr_accessors that encrypt and decrypt attributes transparently
36
63
  email: shuber@huberry.com
37
64
  executables: []
@@ -69,18 +96,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
69
96
  requirements:
70
97
  - - ">="
71
98
  - !ruby/object:Gem::Version
99
+ segments:
100
+ - 0
72
101
  version: "0"
73
- version:
74
102
  required_rubygems_version: !ruby/object:Gem::Requirement
75
103
  requirements:
76
104
  - - ">="
77
105
  - !ruby/object:Gem::Version
106
+ segments:
107
+ - 0
78
108
  version: "0"
79
- version:
80
109
  requirements: []
81
110
 
82
111
  rubyforge_project:
83
- rubygems_version: 1.3.5
112
+ rubygems_version: 1.3.6
84
113
  signing_key:
85
114
  specification_version: 3
86
115
  summary: Generates attr_accessors that encrypt and decrypt attributes transparently