statham 0.1.6 → 0.1.7
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/statham/serializer.rb +12 -2
- data/lib/statham/version.rb +1 -1
- data/spec/unit/serializer_spec.rb +16 -0
- 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: a5c2899badd766045e04f70169225c441f738a4b
|
4
|
+
data.tar.gz: b19c3a10438019f595937ee0f92339b47670c037
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9f5cf19e6cb5d8d9a1bba21fa4ef74132aee34aee330160c1d5b7994ca7f123d38a8951dbe54c8b45d29e371c5a8bc38de0b3614a56bb1cc14af1af92ae61560
|
7
|
+
data.tar.gz: 2437b25b6b71d38934e3d427b784019c5b74341b7eba9fb69099bac0868347656feb5c4f299efdec809dfc92ff129f214f2b42bcd3d91034fe117da9027f02d5
|
data/lib/statham/serializer.rb
CHANGED
@@ -47,7 +47,11 @@ module Statham
|
|
47
47
|
# Returns Hash containing serialized values.
|
48
48
|
def serialize_with_attributes(object)
|
49
49
|
@attribute_set.attributes.inject(object) do |serialized, (name, attribute)|
|
50
|
-
|
50
|
+
if attribute.default || object.has_key?(name)
|
51
|
+
serialized.merge name => attribute.serialize(object[name])
|
52
|
+
else
|
53
|
+
serialized
|
54
|
+
end
|
51
55
|
end
|
52
56
|
end
|
53
57
|
|
@@ -59,7 +63,13 @@ module Statham
|
|
59
63
|
# Returns Hash containing deserialized values.
|
60
64
|
def deserialize_with_attributes(object)
|
61
65
|
@attribute_set.attributes.inject(object) do |serialized, (name, attribute)|
|
62
|
-
|
66
|
+
if attribute.default
|
67
|
+
serialized.merge name => attribute.deserialize(object[name], fallback_to_default: !object.has_key?(name))
|
68
|
+
else
|
69
|
+
object.has_key?(name) ?
|
70
|
+
serialized.merge(name => attribute.deserialize(object[name])) :
|
71
|
+
serialized
|
72
|
+
end
|
63
73
|
end
|
64
74
|
end
|
65
75
|
end
|
data/lib/statham/version.rb
CHANGED
@@ -11,6 +11,14 @@ describe Statham::Serializer do
|
|
11
11
|
it { expect(ActiveSupport::JSON.decode(statham_column.dump(object))).to eq({ 'foo' => 'bar' }) }
|
12
12
|
|
13
13
|
context 'when attribute_set has attributes' do
|
14
|
+
before do
|
15
|
+
attribute_set.attribute :baz
|
16
|
+
end
|
17
|
+
|
18
|
+
it { expect(ActiveSupport::JSON.decode(statham_column.dump(object))).to eq({ 'foo' => 'bar' }) }
|
19
|
+
end
|
20
|
+
|
21
|
+
context 'when attribute_set has attributes with default value' do
|
14
22
|
let(:baz_default) { 'bazbaz' }
|
15
23
|
|
16
24
|
before do
|
@@ -43,5 +51,13 @@ describe Statham::Serializer do
|
|
43
51
|
it { expect(statham_column.load(object_json)[:foo]).to eq('bar') }
|
44
52
|
it { expect(statham_column.load(object_json)[:baz]).to be_nil }
|
45
53
|
end
|
54
|
+
|
55
|
+
context 'when no default value is set' do
|
56
|
+
let(:baz_default) { nil }
|
57
|
+
|
58
|
+
it { expect(statham_column.load(object_json)).to eq({ 'foo' => 'bar' }) }
|
59
|
+
it { expect(statham_column.load(object_json)[:foo]).to eq('bar') }
|
60
|
+
it { expect(statham_column.load(object_json)[:baz]).to be_nil }
|
61
|
+
end
|
46
62
|
end
|
47
63
|
end
|