activerecord-coders 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/active_record/coders/encryptors/aes.rb +7 -3
- data/lib/active_record/coders/serializers/base.rb +11 -5
- data/lib/active_record/coders/serializers/json.rb +2 -2
- data/lib/active_record/coders/serializers/marshal.rb +15 -2
- data/lib/active_record/coders/serializers/message_pack.rb +15 -2
- data/lib/active_record/coders/serializers/yaml.rb +2 -2
- data/lib/active_record/coders/version.rb +1 -1
- data/spec/lib/active_record/coders/serializers/marshal_spec.rb +54 -19
- data/spec/lib/active_record/coders/serializers/message_pack_spec.rb +56 -20
- data/spec/support/db/migrate/03_create_db_message_packs.rb +2 -1
- data/spec/support/db/migrate/05_create_db_marshals.rb +2 -1
- data/spec/support/models/db_aes.rb +2 -4
- data/spec/support/models/db_marshal.rb +4 -1
- data/spec/support/models/db_message_pack.rb +4 -1
- data/spec/support/models/db_pipeline.rb +2 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e0443f7ecbcf79a4de2f4ca7cf5ec8be43966529
|
4
|
+
data.tar.gz: 2e7bc347184f9f47d873003977c7ef547ed8aafa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 408ac253ac427d05ec629998d57d9f0363f88eaacdbd2b040caa26f4d843fd58b7d851f18459f98f1c2b4395c07da9469c8ca4d060f77a656c39b7b9a3a8850e
|
7
|
+
data.tar.gz: 227c693cbe4703be29f4f6e8a854f05135b9d3638ed8011d7c1bba886e64438b7979a9e39bc45e6ce0ee276daafda945b4fcc351b48509d6f66979c6670041c3
|
@@ -2,9 +2,13 @@ class ActiveRecord::Coders::Encryptors::AES
|
|
2
2
|
|
3
3
|
attr_reader :cipher, :options
|
4
4
|
|
5
|
-
def initialize(
|
6
|
-
|
7
|
-
|
5
|
+
def initialize(options = {})
|
6
|
+
password = options[:password]
|
7
|
+
key_length = options[:key_length] || 256
|
8
|
+
binary = !!options[:binary]
|
9
|
+
|
10
|
+
@cipher = ::Gibberish::AES.new(password, key_length)
|
11
|
+
@options = { binary: binary }
|
8
12
|
end
|
9
13
|
|
10
14
|
def dump(data)
|
@@ -8,15 +8,21 @@ class ActiveRecord::Coders::Serializers::Base
|
|
8
8
|
end
|
9
9
|
end
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
11
|
+
attr_reader :options
|
12
|
+
|
13
|
+
def initialize(options = {})
|
14
|
+
@options = options
|
15
15
|
end
|
16
16
|
|
17
17
|
def load(data)
|
18
|
-
self.class.load.call(data)
|
18
|
+
self.class.load.call(data, options)
|
19
19
|
rescue
|
20
20
|
nil
|
21
21
|
end
|
22
|
+
|
23
|
+
def dump(data)
|
24
|
+
self.class.dump.call(data, options)
|
25
|
+
rescue
|
26
|
+
String.new
|
27
|
+
end
|
22
28
|
end
|
@@ -2,8 +2,8 @@ module ActiveRecord::Coders::Serializers
|
|
2
2
|
class JSON < Base
|
3
3
|
|
4
4
|
serialize_with do |s|
|
5
|
-
s.load = ->(data){ ::JSON.parse(data) }
|
6
|
-
s.dump = ->(data){ ::JSON.generate(data) }
|
5
|
+
s.load = ->(data, options){ ::JSON.parse(data) }
|
6
|
+
s.dump = ->(data, options){ ::JSON.generate(data) }
|
7
7
|
end
|
8
8
|
end
|
9
9
|
end
|
@@ -2,8 +2,21 @@ module ActiveRecord::Coders::Serializers
|
|
2
2
|
class Marshal < Base
|
3
3
|
|
4
4
|
serialize_with do |s|
|
5
|
-
s.load = ->(data){
|
6
|
-
|
5
|
+
s.load = ->(data, options){
|
6
|
+
if !!options[:binary]
|
7
|
+
::Marshal.load(data)
|
8
|
+
else
|
9
|
+
::Marshal.load(::Base64.decode64(data))
|
10
|
+
end
|
11
|
+
}
|
12
|
+
|
13
|
+
s.dump = ->(data, options){
|
14
|
+
if !!options[:binary]
|
15
|
+
::Marshal.dump(data)
|
16
|
+
else
|
17
|
+
::Base64.encode64(::Marshal.dump(data))
|
18
|
+
end
|
19
|
+
}
|
7
20
|
end
|
8
21
|
end
|
9
22
|
end
|
@@ -2,8 +2,21 @@ module ActiveRecord::Coders::Serializers
|
|
2
2
|
class MessagePack < Base
|
3
3
|
|
4
4
|
serialize_with do |s|
|
5
|
-
s.load = ->(data){
|
6
|
-
|
5
|
+
s.load = ->(data, options){
|
6
|
+
if !!options[:binary]
|
7
|
+
::MessagePack.unpack(data)
|
8
|
+
else
|
9
|
+
::MessagePack.unpack(::Base64.decode64(data))
|
10
|
+
end
|
11
|
+
}
|
12
|
+
|
13
|
+
s.dump = ->(data, options){
|
14
|
+
if !!options[:binary]
|
15
|
+
::MessagePack.pack(data)
|
16
|
+
else
|
17
|
+
::Base64.encode64(::MessagePack.pack(data))
|
18
|
+
end
|
19
|
+
}
|
7
20
|
end
|
8
21
|
end
|
9
22
|
end
|
@@ -2,8 +2,8 @@ module ActiveRecord::Coders::Serializers
|
|
2
2
|
class YAML < Base
|
3
3
|
|
4
4
|
serialize_with do |s|
|
5
|
-
s.load = ->(data){ ::YAML.load(data) }
|
6
|
-
s.dump = ->(data){ ::YAML.dump(data) }
|
5
|
+
s.load = ->(data, options){ ::YAML.load(data) }
|
6
|
+
s.dump = ->(data, options){ ::YAML.dump(data) }
|
7
7
|
end
|
8
8
|
end
|
9
9
|
end
|
@@ -2,28 +2,63 @@ require "spec_helper"
|
|
2
2
|
|
3
3
|
describe ActiveRecord::Coders::Serializers::Marshal do
|
4
4
|
|
5
|
-
|
6
|
-
DbMarshal.create
|
7
|
-
end
|
5
|
+
context "binary" do
|
8
6
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
7
|
+
it "should create" do
|
8
|
+
DbMarshal.create
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should (de)serialize nil" do
|
12
|
+
marshal = DbMarshal.new
|
13
|
+
marshal.binary_data = nil
|
14
|
+
marshal.save
|
15
|
+
marshal.reload.binary_data.should == nil
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should (de)serialize" do
|
19
|
+
marshal = DbMarshal.new
|
20
|
+
marshal.binary_data = { some: "data", with: %w[a r r a y] }
|
21
|
+
marshal.save
|
22
|
+
marshal.reload.binary_data.should == { some: "data", with: %w[a r r a y] }
|
23
|
+
marshal.reload.instance_variable_get("@attributes")["binary_data"]
|
24
|
+
.value.bytesize.should == 70
|
25
|
+
end
|
15
26
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
27
|
+
it "should not serialize invalid data" do
|
28
|
+
marshal = DbMarshal.new
|
29
|
+
marshal.binary_data = proc {}
|
30
|
+
marshal.save
|
31
|
+
marshal.reload.binary_data.should == nil
|
32
|
+
end
|
21
33
|
end
|
22
34
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
35
|
+
context "text" do
|
36
|
+
|
37
|
+
it "should create" do
|
38
|
+
DbMarshal.create
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should (de)serialize nil" do
|
42
|
+
marshal = DbMarshal.new
|
43
|
+
marshal.text_data = nil
|
44
|
+
marshal.save
|
45
|
+
marshal.reload.text_data.should == nil
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should (de)serialize" do
|
49
|
+
marshal = DbMarshal.new
|
50
|
+
marshal.text_data = { some: "data", with: %w[a r r a y] }
|
51
|
+
marshal.save
|
52
|
+
marshal.reload.text_data.should == { some: "data", with: %w[a r r a y] }
|
53
|
+
marshal.reload.instance_variable_get("@attributes")["text_data"]
|
54
|
+
.value.bytesize.should == 98
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should not serialize invalid data" do
|
58
|
+
marshal = DbMarshal.new
|
59
|
+
marshal.text_data = proc {}
|
60
|
+
marshal.save
|
61
|
+
marshal.reload.text_data.should == nil
|
62
|
+
end
|
28
63
|
end
|
29
64
|
end
|
@@ -2,29 +2,65 @@ require "spec_helper"
|
|
2
2
|
|
3
3
|
describe ActiveRecord::Coders::Serializers::MessagePack do
|
4
4
|
|
5
|
-
|
6
|
-
DbMessagePack.create
|
7
|
-
end
|
5
|
+
context "binary" do
|
8
6
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
7
|
+
it "should create" do
|
8
|
+
DbMessagePack.create
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should (de)serialize nil" do
|
12
|
+
msgpack = DbMessagePack.new
|
13
|
+
msgpack.binary_data = nil
|
14
|
+
msgpack.save
|
15
|
+
msgpack.reload.binary_data.should == nil
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should (de)serialize" do
|
19
|
+
msgpack = DbMessagePack.new
|
20
|
+
msgpack.binary_data = { some: "data", with: %w[a r r a y] }
|
21
|
+
msgpack.save
|
22
|
+
msgpack.reload.binary_data.should ==
|
23
|
+
{ "some" => "data", "with" => %w[a r r a y] }
|
24
|
+
msgpack.reload.instance_variable_get("@attributes")["binary_data"]
|
25
|
+
.value.bytesize.should == 27
|
26
|
+
end
|
15
27
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
28
|
+
it "should not serialize invalid data" do
|
29
|
+
msgpack = DbMessagePack.new
|
30
|
+
msgpack.binary_data = proc {}
|
31
|
+
msgpack.save
|
32
|
+
msgpack.reload.binary_data.should == nil
|
33
|
+
end
|
22
34
|
end
|
23
35
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
36
|
+
context "text" do
|
37
|
+
|
38
|
+
it "should create" do
|
39
|
+
DbMessagePack.create
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should (de)serialize nil" do
|
43
|
+
msgpack = DbMessagePack.new
|
44
|
+
msgpack.text_data = nil
|
45
|
+
msgpack.save
|
46
|
+
msgpack.reload.text_data.should == nil
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should (de)serialize" do
|
50
|
+
msgpack = DbMessagePack.new
|
51
|
+
msgpack.text_data = { some: "data", with: %w[a r r a y] }
|
52
|
+
msgpack.save
|
53
|
+
msgpack.reload.text_data.should ==
|
54
|
+
{ "some" => "data", "with" => %w[a r r a y] }
|
55
|
+
msgpack.reload.instance_variable_get("@attributes")["text_data"]
|
56
|
+
.value.bytesize.should == 37
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should not serialize invalid data" do
|
60
|
+
msgpack = DbMessagePack.new
|
61
|
+
msgpack.text_data = proc {}
|
62
|
+
msgpack.save
|
63
|
+
msgpack.reload.text_data.should == nil
|
64
|
+
end
|
29
65
|
end
|
30
66
|
end
|
@@ -1,6 +1,4 @@
|
|
1
1
|
class DbAes < ActiveRecord::Base
|
2
|
-
serialize :text_data,
|
3
|
-
|
4
|
-
serialize :binary_data,
|
5
|
-
ActiveRecord::Coders::Encryptors::AES.new("secret")
|
2
|
+
serialize :text_data, ActiveRecord::Coders::Encryptors::AES.new(password: "secret")
|
3
|
+
serialize :binary_data, ActiveRecord::Coders::Encryptors::AES.new(password: "secret", key_length: 128, binary: true)
|
6
4
|
end
|
@@ -1,3 +1,6 @@
|
|
1
1
|
class DbMarshal < ActiveRecord::Base
|
2
|
-
serialize :
|
2
|
+
serialize :text_data,
|
3
|
+
ActiveRecord::Coders::Serializers::Marshal.new
|
4
|
+
serialize :binary_data,
|
5
|
+
ActiveRecord::Coders::Serializers::Marshal.new(binary: true)
|
3
6
|
end
|
@@ -1,3 +1,6 @@
|
|
1
1
|
class DbMessagePack < ActiveRecord::Base
|
2
|
-
serialize :
|
2
|
+
serialize :text_data,
|
3
|
+
ActiveRecord::Coders::Serializers::MessagePack.new
|
4
|
+
serialize :binary_data,
|
5
|
+
ActiveRecord::Coders::Serializers::MessagePack.new(binary: true)
|
3
6
|
end
|
@@ -3,12 +3,12 @@ class DbPipeline < ActiveRecord::Base
|
|
3
3
|
Coders::Pipeline.new(
|
4
4
|
Coders::Serializers::MessagePack.new,
|
5
5
|
Coders::Compressors::Gzip.new,
|
6
|
-
Coders::Encryptors::AES.new("secret")
|
6
|
+
Coders::Encryptors::AES.new(password: "secret")
|
7
7
|
)
|
8
8
|
|
9
9
|
serialize :se,
|
10
10
|
Coders::Pipeline.new(
|
11
11
|
Coders::Serializers::MessagePack.new,
|
12
|
-
Coders::Encryptors::AES.new("secret")
|
12
|
+
Coders::Encryptors::AES.new(password: "secret")
|
13
13
|
)
|
14
14
|
end
|