activerecord-coders 0.0.1 → 0.0.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5737128052b05970281b00c05dbb9d5bb6b2b568
4
- data.tar.gz: 45caf47aa40cc76d5ce2e261d56af9f806d6766f
3
+ metadata.gz: e0443f7ecbcf79a4de2f4ca7cf5ec8be43966529
4
+ data.tar.gz: 2e7bc347184f9f47d873003977c7ef547ed8aafa
5
5
  SHA512:
6
- metadata.gz: 5a57ddb9c08382bd4c8c2a66487d7a990572092ed01b4a394cecb35a9f10eb7bd1d6d5e7be17733201edbb437ad69176cf8387320bad05339b50466e8a9b416a
7
- data.tar.gz: 307ee3ce6ec0acdc7989a7b1e0cd5bfb8f7059e3eae108040cac04bd0ec9fad94cd5732e525b1bc66ed8b6dd4cead803a54690c75d262e6ce6b1e4eb6c1d3a5c
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(password, size = 256, options = {})
6
- @cipher = ::Gibberish::AES.new(password, size)
7
- @options = { binary: true }.merge(options)
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
- def dump(data)
12
- self.class.dump.call(data)
13
- rescue
14
- String.new
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){ ::Marshal.load(data) }
6
- s.dump = ->(data){ ::Marshal.dump(data) }
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){ ::MessagePack.unpack(data) }
6
- s.dump = ->(data){ ::MessagePack.pack(data) }
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
@@ -1,5 +1,5 @@
1
1
  module ActiveRecord
2
2
  module Coders
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
4
4
  end
5
5
  end
@@ -2,28 +2,63 @@ require "spec_helper"
2
2
 
3
3
  describe ActiveRecord::Coders::Serializers::Marshal do
4
4
 
5
- it "should create" do
6
- DbMarshal.create
7
- end
5
+ context "binary" do
8
6
 
9
- it "should (de)serialize nil" do
10
- marshal = DbMarshal.new
11
- marshal.data = nil
12
- marshal.save
13
- marshal.reload.data.should == nil
14
- end
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
- it "should (de)serialize" do
17
- marshal = DbMarshal.new
18
- marshal.data = { some: "data", with: %w[a r r a y] }
19
- marshal.save
20
- marshal.reload.data.should == { some: "data", with: %w[a r r a y] }
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
- it "should not serialize invalid data" do
24
- marshal = DbMarshal.new
25
- marshal.data = proc {}
26
- marshal.save
27
- marshal.reload.data.should == nil
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
- it "should create" do
6
- DbMessagePack.create
7
- end
5
+ context "binary" do
8
6
 
9
- it "should (de)serialize nil" do
10
- msgpack = DbMessagePack.new
11
- msgpack.data = nil
12
- msgpack.save
13
- msgpack.reload.data.should == nil
14
- end
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
- it "should (de)serialize" do
17
- msgpack = DbMessagePack.new
18
- msgpack.data = { some: "data", with: %w[a r r a y] }
19
- msgpack.save
20
- msgpack.reload.data.should ==
21
- { "some" => "data", "with" => %w[a r r a y] }
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
- it "should not serialize invalid data" do
25
- msgpack = DbMessagePack.new
26
- msgpack.data = proc {}
27
- msgpack.save
28
- msgpack.reload.data.should == nil
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
@@ -2,7 +2,8 @@ class CreateDbMessagePacks < ActiveRecord::Migration
2
2
 
3
3
  def change
4
4
  create_table :db_message_packs do |t|
5
- t.binary :data
5
+ t.text :text_data
6
+ t.binary :binary_data
6
7
  end
7
8
  end
8
9
  end
@@ -2,7 +2,8 @@ class CreateDbMarshals < ActiveRecord::Migration
2
2
 
3
3
  def change
4
4
  create_table :db_marshals do |t|
5
- t.binary :data
5
+ t.text :text_data
6
+ t.binary :binary_data
6
7
  end
7
8
  end
8
9
  end
@@ -1,6 +1,4 @@
1
1
  class DbAes < ActiveRecord::Base
2
- serialize :text_data,
3
- ActiveRecord::Coders::Encryptors::AES.new("secret", 256, binary: false)
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 :data, ActiveRecord::Coders::Serializers::Marshal.new
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 :data, ActiveRecord::Coders::Serializers::MessagePack.new
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-coders
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael van Rooijen