json_array_serializer 0.0.4 → 0.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.
- checksums.yaml +4 -4
- data/lib/json_array_serializer/version.rb +1 -1
- data/lib/json_array_serializer.rb +10 -4
- data/spec/json_array_serializer_spec.rb +6 -3
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b807cc16ab752f8e582a10fe768148aa866aaa2d
|
4
|
+
data.tar.gz: 92a2d1de692dc88602fbb4ac635f4e71bbf58146
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8e4ba0eb0f88360647930a607f93a580f9e783e4ff0b32d9e3f22a5b618af903a6e8d43fc0342fd4a7e1cd2d4f88f219e697c220d7c887659609bfafb5b9324d
|
7
|
+
data.tar.gz: c564a30d3fd9d904c9a05757d3a54a8a17cbf7c1b301a106332e2c077b9a7f738bca0255b1d4d169d507aecd6679035814e43e859a5ff072cfbff065b889f7f6
|
@@ -69,13 +69,19 @@ class JSONArraySerializer
|
|
69
69
|
# and dumps them either a array of JSON or JSON itself for the database.
|
70
70
|
#
|
71
71
|
def dump(data)
|
72
|
-
return (@allow_nil ? nil : []) if data.nil?
|
73
|
-
|
74
72
|
case @column_type
|
75
73
|
when :array
|
76
|
-
data.
|
74
|
+
if data.nil?
|
75
|
+
@allow_nil ? nil : []
|
76
|
+
else
|
77
|
+
data.to_a.map { |e| JSON.dump(e.to_h) }
|
78
|
+
end
|
77
79
|
when :string, :text
|
78
|
-
|
80
|
+
if data.nil?
|
81
|
+
@allow_nil ? nil : '[]'
|
82
|
+
else
|
83
|
+
JSON.dump(data.to_a.map { |e| e.to_h })
|
84
|
+
end
|
79
85
|
end
|
80
86
|
end
|
81
87
|
end
|
@@ -225,17 +225,20 @@ describe JSONArraySerializer do
|
|
225
225
|
end
|
226
226
|
|
227
227
|
context 'with allow_nil set to false' do
|
228
|
-
let(:
|
228
|
+
let(:text_serializer) { JSONArraySerializer.new(allow_nil: false) }
|
229
|
+
let(:array_serializer) { JSONArraySerializer.new(allow_nil: false, column_type: :array) }
|
229
230
|
|
230
231
|
describe '#load' do
|
231
232
|
it 'loads nil as []' do
|
232
|
-
expect(
|
233
|
+
expect(text_serializer.load(nil)).to eq([])
|
234
|
+
expect(array_serializer.load(nil)).to eq([])
|
233
235
|
end
|
234
236
|
end
|
235
237
|
|
236
238
|
describe '#dump' do
|
237
239
|
it 'dumps nil as []' do
|
238
|
-
expect(
|
240
|
+
expect(text_serializer.dump(nil)).to eq('[]')
|
241
|
+
expect(array_serializer.dump(nil)).to eq([])
|
239
242
|
end
|
240
243
|
end
|
241
244
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json_array_serializer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nathan Lilienthal
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-05-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|