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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 313ed8a7a1a0c8b420a9f213b652110bae0e76fe3603ed46a6b20f4eba42caa4
4
- data.tar.gz: '09d3d148fa5ac7423ed55330e925005f5b5618b9ceda987c34dbbb81d9d550bc'
3
+ metadata.gz: 68606f4b9f309cdfab8577c3e9e13870f95abfe1f3941b875cb2d718b4e51e4e
4
+ data.tar.gz: c4375d1804b0544eb0ae25f7fb4c486c5c7e28fb4f92557f10425cbb3ee8b22c
5
5
  SHA512:
6
- metadata.gz: dab35c48779cdbbb473c0b3f0cf73a58eb41f31d4118e4a516bcbb820ccd06644d59677c8924c403689acd42dab959bf469080138b759e1bd49c15f1cdbc5627
7
- data.tar.gz: 1043eeab5329a5ebee511ecb944173873066a7b4777d97138fd63c774079781fb04cc61bd906b04f63ec5eae8e169455b550ab573b19d8d99d6ec5c279dc859e
6
+ metadata.gz: 56c28734f054ffa9daddcd5597638aa798d03415c9bf6878816afbbe0ac74a15f0c7a7f9e21e6fd03155080fc934dd05da64c41833374a76ca42205a95b8f95a
7
+ data.tar.gz: 63dd77adcfb26e2693f5f0784407fc6289060e3fb56d48ad03635384e605850cfafe2a8989df58a260754f8f128c382c4df80d409662134b79cf04a04dff21dd
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- iso-deserializer (0.8.0)
4
+ iso-deserializer (0.9.0)
5
5
  activesupport (>= 5.0.0)
6
6
 
7
7
  GEM
@@ -7,6 +7,10 @@ module Deserializer
7
7
  def deserializer
8
8
  opts[:deserializer]
9
9
  end
10
+
11
+ def from_params(params)
12
+ deserializer.from_params(params, parent_deserializer: object)
13
+ end
10
14
  end
11
15
  end
12
16
  end
@@ -11,6 +11,10 @@ module Deserializer
11
11
  @opts.fetch :key, @name
12
12
  end
13
13
 
14
+ def if_opt
15
+ @opts.fetch(:if, nil)
16
+ end
17
+
14
18
  def to_hash( params, object )
15
19
  attribute = @type.new( @name, @opts, object )
16
20
  attribute.to_hash( params )
@@ -5,7 +5,7 @@ module Deserializer
5
5
  def value( params )
6
6
  target = []
7
7
  params[key].each do |association_datum|
8
- target << deserializer.from_params( association_datum )
8
+ target << from_params( association_datum )
9
9
  end
10
10
  target
11
11
  end
@@ -4,7 +4,7 @@ module Deserializer
4
4
 
5
5
  def to_hash( params )
6
6
  return {} unless params.has_key? key
7
- value = deserializer.from_params( params[key] )
7
+ value = from_params( params[key] )
8
8
 
9
9
  if object.respond_to? name
10
10
 
@@ -3,7 +3,7 @@ module Deserializer
3
3
  class NestedAssociation < Association
4
4
 
5
5
  def to_hash( params )
6
- { name => deserializer.from_params( params ) }
6
+ { name => from_params( params ) }
7
7
  end
8
8
  end
9
9
  end
@@ -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 )
@@ -1,3 +1,3 @@
1
1
  module Deserializer
2
- VERSION = "0.8.0"
2
+ VERSION = "0.9.0"
3
3
  end
@@ -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( emptiable: true )
51
- assert_equal ({ nonemptiable: true }), d.from_params( nonemptiable: true )
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( emptiable: empty_value )
55
- assert_equal ({ nonemptiable: empty_value }), d.from_params( nonemptiable: empty_value )
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( empty: true )
63
- assert_equal ({ nonemptiable_with_key: true }), d.from_params( non_empty: true )
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( key: value )
67
- assert_equal ({ nonemptiable_with_key: value }), d.from_params( non_empty: value )
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: iso-deserializer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Isometric