shuber-attr_encrypted 1.0.4 → 1.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG CHANGED
@@ -2,6 +2,10 @@
2
2
  * Remove read_attribute and write_attribute methods - unnecessary
3
3
  * ActiveRecord and DataMapper extensions are now "adapters"
4
4
  * Check for existing reader and writer methods separately before creating default ones for encrypted attributes
5
+ * Typo: should send(encrypted_attribute_name.to_sym) instead of send(encrypted_attribute_name.to_s)
6
+ * Add Sequel adapter
7
+ * Update DataMapper tests
8
+ * Update README
5
9
 
6
10
  2009-01-11 - Sean Huber (shuber@huberry.com)
7
11
  * Update README
data/README.markdown CHANGED
@@ -3,7 +3,7 @@ attr\_encrypted
3
3
 
4
4
  Generates attr\_accessors that encrypt and decrypt attributes transparently
5
5
 
6
- Works with ANY class including ActiveRecord and DataMapper
6
+ Works with ANY class including ActiveRecord, DataMapper, and Sequel
7
7
 
8
8
 
9
9
  Installation
@@ -261,18 +261,13 @@ Then you'll have these two class methods available for each attribute: `User.enc
261
261
  be useful when you're using ActiveRecord (see below).
262
262
 
263
263
 
264
- ### DataMapper ###
265
-
266
- For your convenience, the `:encode` and `:marshal` options are set to true by default since you'll be storing everything in a database.
267
-
268
-
269
264
  ### ActiveRecord ###
270
265
 
271
266
  If you're using this gem with ActiveRecord, you get a few extra features:
272
267
 
273
268
  #### Default options ####
274
269
 
275
- Just like the default options for DataMapper, the `:encode` and `:marshal` options are set to true by default since you'll be storing everything in a database.
270
+ For your convenience, the `:encode` and `:marshal` options are set to true by default since you'll be storing everything in a database.
276
271
 
277
272
 
278
273
  #### Dynamic find\_by\_ and scoped\_by\_ methods ####
@@ -294,6 +289,11 @@ The dynamic scope methods like `scoped_by_email_and_password` work the same way.
294
289
  NOTE: This only works if all records are encrypted with the same encryption key (per attribute).
295
290
 
296
291
 
292
+ ### DataMapper and Sequel ###
293
+
294
+ Just like the default options for ActiveRecord, the `:encode` and `:marshal` options are set to true by default since you'll be storing everything in a database.
295
+
296
+
297
297
  Contact
298
298
  -------
299
299
 
@@ -0,0 +1,19 @@
1
+ if defined?(Sequel)
2
+ module Huberry
3
+ module AttrEncrypted
4
+ module Adapters
5
+ module Sequel
6
+ protected
7
+ # Calls attr_encrypted with the options <tt>:encode</tt> and <tt>:marshal</tt> set to true
8
+ # unless they've already been specified
9
+ def attr_encrypted(*attrs)
10
+ options = { :encode => true, :marshal => true }.merge(attrs.last.is_a?(Hash) ? attrs.pop : {})
11
+ super *(attrs << options)
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+
18
+ Sequel::Model.extend Huberry::AttrEncrypted::Adapters::Sequel
19
+ end
@@ -130,7 +130,7 @@ module Huberry
130
130
 
131
131
  define_method "#{attribute}" do
132
132
  value = instance_variable_get("@#{attribute}")
133
- encrypted_value = send(encrypted_attribute_name.to_s)
133
+ encrypted_value = send(encrypted_attribute_name.to_sym)
134
134
  original_options = [:key, :if, :unless].inject({}) do |hash, option|
135
135
  hash[option] = options[option]
136
136
  options[option] = self.class.send :evaluate_attr_encrypted_option, options[option], self
@@ -29,19 +29,19 @@ class DataMapperTest < Test::Unit::TestCase
29
29
  end
30
30
 
31
31
  def test_should_encrypt_email
32
- @person = Client.new :email => 'test@example.com'
33
- assert @person.save
34
- assert_not_nil @person.encrypted_email
35
- assert_not_equal @person.email, @person.encrypted_email
36
- assert_equal @person.email, Client.first.email
32
+ @client = Client.new :email => 'test@example.com'
33
+ assert @client.save
34
+ assert_not_nil @client.encrypted_email
35
+ assert_not_equal @client.email, @client.encrypted_email
36
+ assert_equal @client.email, Client.first.email
37
37
  end
38
38
 
39
39
  def test_should_marshal_and_encrypt_credentials
40
- @person = Client.new
41
- assert @person.save
42
- assert_not_nil @person.encrypted_credentials
43
- assert_not_equal @person.credentials, @person.encrypted_credentials
44
- assert_equal @person.credentials, Client.first.credentials
40
+ @client = Client.new
41
+ assert @client.save
42
+ assert_not_nil @client.encrypted_credentials
43
+ assert_not_equal @client.credentials, @client.encrypted_credentials
44
+ assert_equal @client.credentials, Client.first.credentials
45
45
  assert Client.first.credentials.is_a?(Hash)
46
46
  end
47
47
 
@@ -0,0 +1,46 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ DB = Sequel.sqlite
4
+
5
+ DB.create_table :humans do
6
+ primary_key :id
7
+ column :encrypted_email, :string
8
+ column :password, :string
9
+ column :encrypted_credentials, :string
10
+ column :salt, :string
11
+ end
12
+
13
+ class Human < Sequel::Model(:humans)
14
+ attr_encrypted :email, :key => 'a secret key'
15
+ attr_encrypted :credentials, :key => Proc.new { |human| Huberry::Encryptor.encrypt(:value => human.salt, :key => 'some private key') }
16
+
17
+ def after_initialize(attrs = {})
18
+ self.salt ||= Digest::SHA1.hexdigest((Time.now.to_i * rand(5)).to_s)
19
+ self.credentials ||= { :username => 'example', :password => 'test' }
20
+ end
21
+ end
22
+
23
+ class SequelTest < Test::Unit::TestCase
24
+
25
+ def setup
26
+ Human.all.each(&:destroy)
27
+ end
28
+
29
+ def test_should_encrypt_email
30
+ @human = Human.new :email => 'test@example.com'
31
+ assert @human.save
32
+ assert_not_nil @human.encrypted_email
33
+ assert_not_equal @human.email, @human.encrypted_email
34
+ assert_equal @human.email, Human.first.email
35
+ end
36
+
37
+ def test_should_marshal_and_encrypt_credentials
38
+ @human = Human.new
39
+ assert @human.save
40
+ assert_not_nil @human.encrypted_credentials
41
+ assert_not_equal @human.credentials, @human.encrypted_credentials
42
+ assert_equal @human.credentials, Human.first.credentials
43
+ assert Human.first.credentials.is_a?(Hash)
44
+ end
45
+
46
+ end
data/test/test_helper.rb CHANGED
@@ -6,10 +6,12 @@ gem 'shuber-eigenclass', '>= 1.0.1'
6
6
  gem 'shuber-encryptor'
7
7
  gem 'activerecord'
8
8
  gem 'datamapper'
9
+ gem 'sequel'
9
10
 
10
11
  require 'eigenclass'
11
12
  require 'encryptor'
12
13
  require 'active_record'
13
14
  require 'datamapper'
15
+ require 'sequel'
14
16
 
15
17
  require File.dirname(__FILE__) + '/../lib/attr_encrypted'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shuber-attr_encrypted
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.4
4
+ version: 1.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sean Huber
@@ -43,6 +43,7 @@ files:
43
43
  - lib/attr_encrypted.rb
44
44
  - lib/huberry/attr_encrypted/adapters/active_record.rb
45
45
  - lib/huberry/attr_encrypted/adapters/data_mapper.rb
46
+ - lib/huberry/attr_encrypted/adapters/sequel.rb
46
47
  - lib/huberry/attr_encrypted/class.rb
47
48
  - lib/huberry/attr_encrypted/object.rb
48
49
  - MIT-LICENSE
@@ -82,3 +83,4 @@ test_files:
82
83
  - test/active_record_test.rb
83
84
  - test/attr_encrypted_test.rb
84
85
  - test/data_mapper_test.rb
86
+ - test/sequel_test.rb