activerecord-coders 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e0443f7ecbcf79a4de2f4ca7cf5ec8be43966529
4
- data.tar.gz: 2e7bc347184f9f47d873003977c7ef547ed8aafa
3
+ metadata.gz: 69ed667d4c5aa7f424697642e982a7d94565503d
4
+ data.tar.gz: e04d00fb744922fe51b00caefae6c049677dd696
5
5
  SHA512:
6
- metadata.gz: 408ac253ac427d05ec629998d57d9f0363f88eaacdbd2b040caa26f4d843fd58b7d851f18459f98f1c2b4395c07da9469c8ca4d060f77a656c39b7b9a3a8850e
7
- data.tar.gz: 227c693cbe4703be29f4f6e8a854f05135b9d3638ed8011d7c1bba886e64438b7979a9e39bc45e6ce0ee276daafda945b4fcc351b48509d6f66979c6670041c3
6
+ metadata.gz: a3cfc19ffe58017d176461ecc0f48c75e1119a4fec116567951d654cbe3916eab35815d602812a613ccbc79001563d8184fa232fb195240c477b21a64e8ac756
7
+ data.tar.gz: 4b278b6297a35f9a60bc8743d177c7485b49d67ea17111c3857f0c5f6ee233c1b3e758e01fe64b657245c071c796e41bf31a630371b9db054540b7edf69c98e4
@@ -26,6 +26,7 @@ Gem::Specification.new do |spec|
26
26
  # 3rd party libaries for coders
27
27
  spec.add_development_dependency "gibberish"
28
28
  spec.add_development_dependency "msgpack"
29
+ spec.add_development_dependency "oj"
29
30
 
30
31
  # Testing libraries
31
32
  spec.add_development_dependency "rspec"
@@ -9,14 +9,10 @@ class ActiveRecord::Coders::Compressors::Base
9
9
  end
10
10
 
11
11
  def dump(data)
12
- self.class.dump.call(data)
13
- rescue
14
- String.new
12
+ self.class.dump.call(data) if data
15
13
  end
16
14
 
17
15
  def load(data)
18
- self.class.load.call(data)
19
- rescue
20
- nil
16
+ self.class.load.call(data) if data
21
17
  end
22
18
  end
@@ -12,14 +12,10 @@ class ActiveRecord::Coders::Encryptors::AES
12
12
  end
13
13
 
14
14
  def dump(data)
15
- cipher.encrypt(data, options)
16
- rescue
17
- String.new
15
+ cipher.encrypt(data, options) if data && !data.empty?
18
16
  end
19
17
 
20
18
  def load(data)
21
- cipher.decrypt(data, options)
22
- rescue
23
- nil
19
+ cipher.decrypt(data, options) if data
24
20
  end
25
21
  end
@@ -3,5 +3,6 @@ module ActiveRecord::Coders::Serializers
3
3
  require_relative "serializers/marshal"
4
4
  require_relative "serializers/yaml"
5
5
  require_relative "serializers/json"
6
+ require_relative "serializers/oj"
6
7
  require_relative "serializers/message_pack"
7
8
  end
@@ -15,14 +15,10 @@ class ActiveRecord::Coders::Serializers::Base
15
15
  end
16
16
 
17
17
  def load(data)
18
- self.class.load.call(data, options)
19
- rescue
20
- nil
18
+ self.class.load.call(data, options) if data
21
19
  end
22
20
 
23
21
  def dump(data)
24
- self.class.dump.call(data, options)
25
- rescue
26
- String.new
22
+ self.class.dump.call(data, options) if data
27
23
  end
28
24
  end
@@ -0,0 +1,24 @@
1
+ module ActiveRecord::Coders::Serializers
2
+ class Oj < Base
3
+
4
+ serialize_with do |s|
5
+ s.load = ->(data, options){
6
+ case options[:mode]
7
+ when :strict
8
+ ::Oj.strict_load(data)
9
+ when :compat, :null, :object, nil
10
+ ::Oj.load(data)
11
+ end
12
+ }
13
+
14
+ s.dump = ->(data, options){
15
+ case options[:mode]
16
+ when :strict, :compat, :null, :object
17
+ ::Oj.dump(data, mode: options[:mode])
18
+ when nil
19
+ ::Oj.dump(data, mode: :object)
20
+ end
21
+ }
22
+ end
23
+ end
24
+ end
@@ -1,5 +1,5 @@
1
1
  module ActiveRecord
2
2
  module Coders
3
- VERSION = "0.0.2"
3
+ VERSION = "0.0.3"
4
4
  end
5
5
  end
@@ -13,11 +13,11 @@ describe ActiveRecord::Coders::Compressors::Gzip do
13
13
  gzip.reload.data.should == nil
14
14
  end
15
15
 
16
- it "should not compress invalid data" do
16
+ it "should raise exception when receiving invalid data" do
17
17
  gzip = DbGzip.new
18
18
  gzip.data = {}
19
- gzip.save
20
- gzip.reload.data.should == nil
19
+ expect { gzip.save }.to raise_error(TypeError)
20
+ expect { gzip.reload }.to raise_error(ActiveRecord::RecordNotFound)
21
21
  end
22
22
 
23
23
  it "should (de)compress" do
@@ -15,7 +15,7 @@ describe ActiveRecord::Coders::Encryptors::AES do
15
15
  aes256.reload.binary_data.should == nil
16
16
  end
17
17
 
18
- it "should handle empty string values" do
18
+ it "should treat empty string as nil" do
19
19
  aes256 = DbAes.new
20
20
  aes256.binary_data = ""
21
21
  aes256.save
@@ -44,7 +44,7 @@ describe ActiveRecord::Coders::Encryptors::AES do
44
44
  aes256.reload.text_data.should == nil
45
45
  end
46
46
 
47
- it "should handle empty string values" do
47
+ it "should treat empty string as nil" do
48
48
  aes256 = DbAes.new
49
49
  aes256.text_data = ""
50
50
  aes256.save
@@ -2,7 +2,7 @@ require "spec_helper"
2
2
 
3
3
  describe ActiveRecord::Coders::Pipeline do
4
4
 
5
- it "create a blank record" do
5
+ it "should create blank" do
6
6
  DbPipeline.create
7
7
  end
8
8
 
@@ -2,11 +2,11 @@ require "spec_helper"
2
2
 
3
3
  describe ActiveRecord::Coders::Serializers::JSON do
4
4
 
5
- it "should create" do
5
+ it "should create blank" do
6
6
  DbJSON.create
7
7
  end
8
8
 
9
- it "should (de)serialize nil" do
9
+ it "should handle nil values" do
10
10
  json = DbJSON.new
11
11
  json.data = nil
12
12
  json.save
@@ -20,10 +20,10 @@ describe ActiveRecord::Coders::Serializers::JSON do
20
20
  json.reload.data.should == { "some" => "data", "with" => %w[a r r a y] }
21
21
  end
22
22
 
23
- it "should not serialize invalid data" do
23
+ it "should raise exception when receiving invalid data" do
24
24
  json = DbJSON.new
25
25
  json.data = 1
26
- json.save
27
- json.reload.data.should == nil
26
+ expect { json.save }.to raise_error(JSON::GeneratorError)
27
+ expect { json.reload }.to raise_error(ActiveRecord::RecordNotFound)
28
28
  end
29
29
  end
@@ -4,11 +4,11 @@ describe ActiveRecord::Coders::Serializers::Marshal do
4
4
 
5
5
  context "binary" do
6
6
 
7
- it "should create" do
7
+ it "should create blank" do
8
8
  DbMarshal.create
9
9
  end
10
10
 
11
- it "should (de)serialize nil" do
11
+ it "should handle nil values" do
12
12
  marshal = DbMarshal.new
13
13
  marshal.binary_data = nil
14
14
  marshal.save
@@ -21,24 +21,24 @@ describe ActiveRecord::Coders::Serializers::Marshal do
21
21
  marshal.save
22
22
  marshal.reload.binary_data.should == { some: "data", with: %w[a r r a y] }
23
23
  marshal.reload.instance_variable_get("@attributes")["binary_data"]
24
- .value.bytesize.should == 70
24
+ .value.bytesize.should == 70
25
25
  end
26
26
 
27
- it "should not serialize invalid data" do
27
+ it "should raise exception when receiving invalid data" do
28
28
  marshal = DbMarshal.new
29
29
  marshal.binary_data = proc {}
30
- marshal.save
31
- marshal.reload.binary_data.should == nil
30
+ expect { marshal.save }.to raise_error(TypeError)
31
+ expect { marshal.reload }.to raise_error(ActiveRecord::RecordNotFound)
32
32
  end
33
33
  end
34
34
 
35
35
  context "text" do
36
36
 
37
- it "should create" do
37
+ it "should create blank" do
38
38
  DbMarshal.create
39
39
  end
40
40
 
41
- it "should (de)serialize nil" do
41
+ it "should handle nil values" do
42
42
  marshal = DbMarshal.new
43
43
  marshal.text_data = nil
44
44
  marshal.save
@@ -51,14 +51,14 @@ describe ActiveRecord::Coders::Serializers::Marshal do
51
51
  marshal.save
52
52
  marshal.reload.text_data.should == { some: "data", with: %w[a r r a y] }
53
53
  marshal.reload.instance_variable_get("@attributes")["text_data"]
54
- .value.bytesize.should == 98
54
+ .value.bytesize.should == 98
55
55
  end
56
56
 
57
- it "should not serialize invalid data" do
57
+ it "should raise exception when receiving invalid data" do
58
58
  marshal = DbMarshal.new
59
59
  marshal.text_data = proc {}
60
- marshal.save
61
- marshal.reload.text_data.should == nil
60
+ expect { marshal.save }.to raise_error(TypeError)
61
+ expect { marshal.reload }.to raise_error(ActiveRecord::RecordNotFound)
62
62
  end
63
63
  end
64
64
  end
@@ -4,11 +4,11 @@ describe ActiveRecord::Coders::Serializers::MessagePack do
4
4
 
5
5
  context "binary" do
6
6
 
7
- it "should create" do
7
+ it "should create blank" do
8
8
  DbMessagePack.create
9
9
  end
10
10
 
11
- it "should (de)serialize nil" do
11
+ it "should handle nil values" do
12
12
  msgpack = DbMessagePack.new
13
13
  msgpack.binary_data = nil
14
14
  msgpack.save
@@ -22,24 +22,24 @@ describe ActiveRecord::Coders::Serializers::MessagePack do
22
22
  msgpack.reload.binary_data.should ==
23
23
  { "some" => "data", "with" => %w[a r r a y] }
24
24
  msgpack.reload.instance_variable_get("@attributes")["binary_data"]
25
- .value.bytesize.should == 27
25
+ .value.bytesize.should == 27
26
26
  end
27
27
 
28
- it "should not serialize invalid data" do
28
+ it "should raise exception when receiving invalid data" do
29
29
  msgpack = DbMessagePack.new
30
30
  msgpack.binary_data = proc {}
31
- msgpack.save
32
- msgpack.reload.binary_data.should == nil
31
+ expect { msgpack.save }.to raise_error(NoMethodError)
32
+ expect { msgpack.reload }.to raise_error(ActiveRecord::RecordNotFound)
33
33
  end
34
34
  end
35
35
 
36
36
  context "text" do
37
37
 
38
- it "should create" do
38
+ it "should create blank" do
39
39
  DbMessagePack.create
40
40
  end
41
41
 
42
- it "should (de)serialize nil" do
42
+ it "should handle nil values" do
43
43
  msgpack = DbMessagePack.new
44
44
  msgpack.text_data = nil
45
45
  msgpack.save
@@ -53,14 +53,14 @@ describe ActiveRecord::Coders::Serializers::MessagePack do
53
53
  msgpack.reload.text_data.should ==
54
54
  { "some" => "data", "with" => %w[a r r a y] }
55
55
  msgpack.reload.instance_variable_get("@attributes")["text_data"]
56
- .value.bytesize.should == 37
56
+ .value.bytesize.should == 37
57
57
  end
58
58
 
59
- it "should not serialize invalid data" do
59
+ it "should raise exception when receiving invalid data" do
60
60
  msgpack = DbMessagePack.new
61
61
  msgpack.text_data = proc {}
62
- msgpack.save
63
- msgpack.reload.text_data.should == nil
62
+ expect { msgpack.save }.to raise_error(NoMethodError)
63
+ expect { msgpack.reload }.to raise_error(ActiveRecord::RecordNotFound)
64
64
  end
65
65
  end
66
66
  end
@@ -0,0 +1,108 @@
1
+ require "spec_helper"
2
+
3
+ describe ActiveRecord::Coders::Serializers::Oj do
4
+
5
+ it "should create blank" do
6
+ DbOj.create
7
+ end
8
+
9
+ context "default, object" do
10
+
11
+ it "should handle nil values" do
12
+ oj = DbOj.new
13
+ oj.default_data = nil
14
+ oj.object_data = nil
15
+ oj.save
16
+ oj.reload.default_data.should == nil
17
+ oj.reload.object_data.should == nil
18
+ end
19
+
20
+ it "should (de)serialize" do
21
+ data = OpenStruct.new(
22
+ name: "Michael",
23
+ colors: %w[red white blue]
24
+ )
25
+
26
+ oj = DbOj.new
27
+ oj.default_data = data
28
+ oj.object_data = data
29
+ oj.save
30
+ oj.reload.default_data.should == data
31
+ oj.reload.object_data.should == data
32
+ end
33
+
34
+ it "should replace unknown types with JSON null values" do
35
+ oj = DbOj.new
36
+ oj.default_data = { "example" => 1, "test" => proc{} }
37
+ oj.object_data = { "example" => 1, "test" => proc{} }
38
+ oj.save
39
+ oj.reload.default_data.should == { "example" => 1, "test" => nil }
40
+ oj.reload.object_data.should == { "example" => 1, "test" => nil }
41
+ end
42
+ end
43
+
44
+ context "strict" do
45
+
46
+ it "should handle nil values" do
47
+ oj = DbOj.new
48
+ oj.strict_data = nil
49
+ oj.save
50
+ oj.reload.strict_data.should == nil
51
+ end
52
+
53
+ it "should (de)serialize" do
54
+ oj = DbOj.new
55
+ oj.strict_data = { "some" => "data", "with" => %w[a r r a y] }
56
+ oj.save
57
+ oj.reload.strict_data.should == { "some" => "data", "with" => %w[a r r a y] }
58
+ end
59
+
60
+ it "should raise exception when receiving invalid data" do
61
+ oj = DbOj.new
62
+ oj.strict_data = { some: "data", with: %w[a r r a y] }
63
+ expect { oj.save }.to raise_error(TypeError)
64
+ expect { oj.reload }.to raise_error(ActiveRecord::RecordNotFound)
65
+ end
66
+ end
67
+
68
+ context "compat" do
69
+
70
+ it "should handle nil values" do
71
+ oj = DbOj.new
72
+ oj.compat_data = nil
73
+ oj.save
74
+ oj.reload.compat_data.should == nil
75
+ end
76
+
77
+ it "should (de)serialize" do
78
+ oj = DbOj.new
79
+ oj.compat_data = { some: "data", with: %w[a r r a y] }
80
+ oj.save
81
+ oj.reload.compat_data.should == { "some" => "data", "with" => %w[a r r a y] }
82
+ end
83
+ end
84
+
85
+ context "null" do
86
+
87
+ it "should handle nil values" do
88
+ oj = DbOj.new
89
+ oj.null_data = nil
90
+ oj.save
91
+ oj.reload.null_data.should == nil
92
+ end
93
+
94
+ it "should (de)serialize" do
95
+ oj = DbOj.new
96
+ oj.null_data = { "some" => "data", "with" => %w[a r r a y] }
97
+ oj.save
98
+ oj.reload.null_data.should == { "some" => "data", "with" => %w[a r r a y] }
99
+ end
100
+
101
+ it "should replace unknown types with JSON null values" do
102
+ oj = DbOj.new
103
+ oj.null_data = { "example" => 1, "test" => proc{} }
104
+ oj.save
105
+ oj.reload.null_data.should == { "example" => 1, "test" => nil }
106
+ end
107
+ end
108
+ end
@@ -2,11 +2,11 @@ require "spec_helper"
2
2
 
3
3
  describe ActiveRecord::Coders::Serializers::YAML do
4
4
 
5
- it "should create" do
5
+ it "should create blank" do
6
6
  DbYAML.create
7
7
  end
8
8
 
9
- it "should (de)serialize nil" do
9
+ it "should handle nil values" do
10
10
  json = DbYAML.new
11
11
  json.data = nil
12
12
  json.save
@@ -20,10 +20,10 @@ describe ActiveRecord::Coders::Serializers::YAML do
20
20
  json.reload.data.should == { some: "data", with: %w[a r r a y] }
21
21
  end
22
22
 
23
- it "should deserialize invalid data" do
23
+ it "should raise exception when receiving invalid data" do
24
24
  yaml = DbYAML.new
25
25
  yaml.data = proc {}
26
- yaml.save
27
- yaml.reload.data.should == nil
26
+ expect { yaml.save }.to raise_error(TypeError)
27
+ expect { yaml.reload }.to raise_error(ActiveRecord::RecordNotFound)
28
28
  end
29
29
  end
@@ -0,0 +1,10 @@
1
+ class CreateDbOjs < ActiveRecord::Migration
2
+
3
+ def change
4
+ create_table :db_ojs do |t|
5
+ [:default, :object, :strict, :compat, :null].each do |type|
6
+ t.text :"#{type}_data"
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,9 @@
1
+ class DbOj < ActiveRecord::Base
2
+ serialize :default_data,
3
+ ActiveRecord::Coders::Serializers::Oj.new
4
+
5
+ [:object, :strict, :compat, :null].each do |type|
6
+ serialize :"#{type}_data",
7
+ ActiveRecord::Coders::Serializers::Oj.new(mode: type)
8
+ end
9
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-coders
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael van Rooijen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-31 00:00:00.000000000 Z
11
+ date: 2014-04-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -94,6 +94,20 @@ dependencies:
94
94
  - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: oj
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
97
111
  - !ruby/object:Gem::Dependency
98
112
  name: rspec
99
113
  requirement: !ruby/object:Gem::Requirement
@@ -176,6 +190,7 @@ files:
176
190
  - lib/active_record/coders/serializers/json.rb
177
191
  - lib/active_record/coders/serializers/marshal.rb
178
192
  - lib/active_record/coders/serializers/message_pack.rb
193
+ - lib/active_record/coders/serializers/oj.rb
179
194
  - lib/active_record/coders/serializers/yaml.rb
180
195
  - lib/active_record/coders/version.rb
181
196
  - lib/activerecord-coders.rb
@@ -186,6 +201,7 @@ files:
186
201
  - spec/lib/active_record/coders/serializers/json_spec.rb
187
202
  - spec/lib/active_record/coders/serializers/marshal_spec.rb
188
203
  - spec/lib/active_record/coders/serializers/message_pack_spec.rb
204
+ - spec/lib/active_record/coders/serializers/oj_spec.rb
189
205
  - spec/lib/active_record/coders/serializers/yaml_spec.rb
190
206
  - spec/spec_helper.rb
191
207
  - spec/support/db/migrate/01_create_db_gzips.rb
@@ -195,12 +211,14 @@ files:
195
211
  - spec/support/db/migrate/05_create_db_marshals.rb
196
212
  - spec/support/db/migrate/06_create_db_aes.rb
197
213
  - spec/support/db/migrate/07_create_db_pipelines.rb
214
+ - spec/support/db/migrate/08_create_db_ojs.rb
198
215
  - spec/support/fixtures/lipsum.txt
199
216
  - spec/support/models/db_aes.rb
200
217
  - spec/support/models/db_gzip.rb
201
218
  - spec/support/models/db_json.rb
202
219
  - spec/support/models/db_marshal.rb
203
220
  - spec/support/models/db_message_pack.rb
221
+ - spec/support/models/db_oj.rb
204
222
  - spec/support/models/db_pipeline.rb
205
223
  - spec/support/models/db_yaml.rb
206
224
  homepage: http://michael.vanrooijen.io
@@ -236,6 +254,7 @@ test_files:
236
254
  - spec/lib/active_record/coders/serializers/json_spec.rb
237
255
  - spec/lib/active_record/coders/serializers/marshal_spec.rb
238
256
  - spec/lib/active_record/coders/serializers/message_pack_spec.rb
257
+ - spec/lib/active_record/coders/serializers/oj_spec.rb
239
258
  - spec/lib/active_record/coders/serializers/yaml_spec.rb
240
259
  - spec/spec_helper.rb
241
260
  - spec/support/db/migrate/01_create_db_gzips.rb
@@ -245,11 +264,13 @@ test_files:
245
264
  - spec/support/db/migrate/05_create_db_marshals.rb
246
265
  - spec/support/db/migrate/06_create_db_aes.rb
247
266
  - spec/support/db/migrate/07_create_db_pipelines.rb
267
+ - spec/support/db/migrate/08_create_db_ojs.rb
248
268
  - spec/support/fixtures/lipsum.txt
249
269
  - spec/support/models/db_aes.rb
250
270
  - spec/support/models/db_gzip.rb
251
271
  - spec/support/models/db_json.rb
252
272
  - spec/support/models/db_marshal.rb
253
273
  - spec/support/models/db_message_pack.rb
274
+ - spec/support/models/db_oj.rb
254
275
  - spec/support/models/db_pipeline.rb
255
276
  - spec/support/models/db_yaml.rb