iso-deserializer 0.8.0 → 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/deserializer/attribute/association.rb +4 -0
- data/lib/deserializer/attribute/attribute.rb +4 -0
- data/lib/deserializer/attribute/has_many_association.rb +1 -1
- data/lib/deserializer/attribute/has_one_association.rb +1 -1
- data/lib/deserializer/attribute/nested_association.rb +1 -1
- data/lib/deserializer/base.rb +4 -1
- data/lib/deserializer/version.rb +1 -1
- data/test/lib/deserializers.rb +23 -0
- data/test/unit/deserializer_test.rb +34 -8
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 68606f4b9f309cdfab8577c3e9e13870f95abfe1f3941b875cb2d718b4e51e4e
|
4
|
+
data.tar.gz: c4375d1804b0544eb0ae25f7fb4c486c5c7e28fb4f92557f10425cbb3ee8b22c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 56c28734f054ffa9daddcd5597638aa798d03415c9bf6878816afbbe0ac74a15f0c7a7f9e21e6fd03155080fc934dd05da64c41833374a76ca42205a95b8f95a
|
7
|
+
data.tar.gz: 63dd77adcfb26e2693f5f0784407fc6289060e3fb56d48ad03635384e605850cfafe2a8989df58a260754f8f128c382c4df80d409662134b79cf04a04dff21dd
|
data/Gemfile.lock
CHANGED
data/lib/deserializer/base.rb
CHANGED
@@ -10,7 +10,7 @@ module Deserializer
|
|
10
10
|
|
11
11
|
# deserializer usage functions
|
12
12
|
|
13
|
-
def from_params( params = {} )
|
13
|
+
def from_params( params = {}, parent_deserializer: nil)
|
14
14
|
new( params ).deserialize
|
15
15
|
end
|
16
16
|
|
@@ -27,6 +27,9 @@ module Deserializer
|
|
27
27
|
|
28
28
|
# deserialize
|
29
29
|
self.class.__attrs.each do |_, attr|
|
30
|
+
if_method_name = attr.if_opt
|
31
|
+
next if if_method_name && !send(if_method_name)
|
32
|
+
|
30
33
|
object.merge!( attr.to_hash( params, self ) ) do |key, old_value, new_value|
|
31
34
|
# in the case that 2 has_ones merge into the same key. Not sure i want to support this
|
32
35
|
if old_value.is_a?( Hash ) && new_value.is_a?( Hash )
|
data/lib/deserializer/version.rb
CHANGED
data/test/lib/deserializers.rb
CHANGED
@@ -145,4 +145,27 @@ class KeyedHasOneDeserializer < Deserializer::Base
|
|
145
145
|
attribute :internal, key: :external
|
146
146
|
|
147
147
|
has_one :user_info, deserializer: ::BasicDeserializer, key: :thing
|
148
|
+
end
|
149
|
+
|
150
|
+
class ConditionalDeserializer < Deserializer::Base
|
151
|
+
attr_accessor :include_conditional
|
152
|
+
|
153
|
+
attribute :conditional, if: :include_conditional
|
154
|
+
|
155
|
+
has_one :user_info, deserializer: ::BasicDeserializer, if: :include_conditional
|
156
|
+
has_many :attributes, deserializer: AttributeDeserializer, if: :include_conditional
|
157
|
+
end
|
158
|
+
|
159
|
+
class ParentDeserializerContextDeserializer < Deserializer::Base
|
160
|
+
attr_accessor :include_conditional
|
161
|
+
|
162
|
+
attribute :conditional, if: :include_conditional
|
163
|
+
|
164
|
+
has_one :inner, deserializer: ParentDeserializerContextDeserializer, if: :include_conditional
|
165
|
+
|
166
|
+
def self.from_params(params = {}, parent_deserializer: nil)
|
167
|
+
deserializer = new(params)
|
168
|
+
deserializer.include_conditional = parent_deserializer.include_conditional if parent_deserializer
|
169
|
+
deserializer.deserialize
|
170
|
+
end
|
148
171
|
end
|
@@ -47,24 +47,24 @@ class DeserializerTest < Minitest::Test
|
|
47
47
|
def test_ignore_empty_option
|
48
48
|
d = EmptiableAttributeDeserializer
|
49
49
|
|
50
|
-
assert_equal ({ emptiable: true }), d.from_params(
|
51
|
-
assert_equal ({ nonemptiable: true }), d.from_params(
|
50
|
+
assert_equal ({ emptiable: true }), d.from_params({emptiable: true})
|
51
|
+
assert_equal ({ nonemptiable: true }), d.from_params({nonemptiable: true})
|
52
52
|
|
53
53
|
[false, nil, "", [], {}].each do |empty_value|
|
54
|
-
assert_equal ({}), d.from_params(
|
55
|
-
assert_equal ({ nonemptiable: empty_value }), d.from_params(
|
54
|
+
assert_equal ({}), d.from_params({emptiable: empty_value})
|
55
|
+
assert_equal ({ nonemptiable: empty_value }), d.from_params({nonemptiable: empty_value})
|
56
56
|
end
|
57
57
|
end
|
58
58
|
|
59
59
|
def test_ignore_empty_with_key
|
60
60
|
d = EmptiableAttributeDeserializer
|
61
61
|
|
62
|
-
assert_equal ({ emptiable_with_key: true }), d.from_params(
|
63
|
-
assert_equal ({ nonemptiable_with_key: true }), d.from_params(
|
62
|
+
assert_equal ({ emptiable_with_key: true }), d.from_params({empty: true})
|
63
|
+
assert_equal ({ nonemptiable_with_key: true }), d.from_params({non_empty: true})
|
64
64
|
|
65
65
|
[false, nil, "", [], {}].each do |value|
|
66
|
-
assert_equal ({}), d.from_params(
|
67
|
-
assert_equal ({ nonemptiable_with_key: value }), d.from_params(
|
66
|
+
assert_equal ({}), d.from_params({key: value})
|
67
|
+
assert_equal ({ nonemptiable_with_key: value }), d.from_params({non_empty: value})
|
68
68
|
end
|
69
69
|
end
|
70
70
|
|
@@ -197,4 +197,30 @@ class DeserializerTest < Minitest::Test
|
|
197
197
|
|
198
198
|
assert_equal inherited_permitted_params, InheritedDeserializer.permitted_params
|
199
199
|
end
|
200
|
+
|
201
|
+
def test_conditional
|
202
|
+
params = {
|
203
|
+
conditional: :val, user_info: { user_id: 6, text: "text" },
|
204
|
+
attributes: [{user: 6, text: "foo"}, {user: 6, text: "something"}]
|
205
|
+
}
|
206
|
+
assert_equal ({}), ConditionalDeserializer.from_params(params)
|
207
|
+
|
208
|
+
deserializer = ConditionalDeserializer.new(params)
|
209
|
+
deserializer.include_conditional = true
|
210
|
+
assert_equal({
|
211
|
+
conditional: :val, user_info: { user_id: 6, text: "text" },
|
212
|
+
attributes: [{user_id: 6, text: "foo"}, {user_id: 6, text: "something"}]
|
213
|
+
}, deserializer.deserialize)
|
214
|
+
end
|
215
|
+
|
216
|
+
def test_override_create_child_deserializer
|
217
|
+
params = { conditional: :val, inner: { conditional: :yay } }
|
218
|
+
assert_equal ({}), ParentDeserializerContextDeserializer.from_params(params)
|
219
|
+
|
220
|
+
deserializer = ParentDeserializerContextDeserializer.new(params)
|
221
|
+
deserializer.include_conditional = true
|
222
|
+
assert_equal({
|
223
|
+
conditional: :val, inner: { conditional: :yay }
|
224
|
+
}, deserializer.deserialize)
|
225
|
+
end
|
200
226
|
end
|