deserializer 0.7.0 → 0.8.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d7ead03cc6f7509656922f6a300a7416730840dc
4
- data.tar.gz: f10fff12d9341c2c51cfcf83d4374e1cfbf82d7a
3
+ metadata.gz: ad134a88991784cdb31663a128857d6bdd2f818f
4
+ data.tar.gz: 56a0ea6c8d1c0c44eba5e4b26eff012d39e6407e
5
5
  SHA512:
6
- metadata.gz: f922a56a0beab0caffce4555722f93c0a41c4dbcd6d41fde5bd30875346409f49e12d8d20e316f324ff026b6c5b2fd3e0112f2a73495c8dd506b43722ae6c459
7
- data.tar.gz: 37cd5ed9ea2990d5af586cf309a19e4bbed380b3881802dd6f33abe2e890e2af28fb5270e882db1caa9b72cec6ec176f403b5d8577a41d9e28efe80ec210eb82
6
+ metadata.gz: 1fa4cf73fe4f13b057f6577925ca5a166de76d580d69d4d7bee2d22860cda19c2e36df169bf7a97d0c5c92f9449763093e6561619eafa89ca0bc43682c0cf001
7
+ data.tar.gz: 7cb020cad12a271885e6dc6581fdf499119fa9f8d9e237fde90693fb526636be84e2db7348b0119db2966d7c2ce7d65f94f4b167f877c7227efc56d2fff37e12
@@ -1,13 +1,13 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- deserializer (0.6.0)
4
+ deserializer (0.8.0)
5
5
  activesupport
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
- activesupport (4.2.5)
10
+ activesupport (4.2.6)
11
11
  i18n (~> 0.7)
12
12
  json (~> 1.7, >= 1.7.7)
13
13
  minitest (~> 5.1)
data/README.md CHANGED
@@ -392,6 +392,55 @@ Given params:
392
392
  }
393
393
  ```
394
394
 
395
+ #### key
396
+
397
+ You can deserialize a `has_one` association into a different key from what the json gives you. For example:
398
+ ```json
399
+ {
400
+ id: 6,
401
+ name: "mac & cheese",
402
+ alias:
403
+ {
404
+ id: 83,
405
+ name: "macaroni and cheese"
406
+ }
407
+ }
408
+ ```
409
+
410
+ but your model is
411
+
412
+ ```ruby
413
+ class Dish
414
+ has_one :alias
415
+ accepted_nested_attributes_for :alias
416
+ end
417
+ ```
418
+ instead of renaming the hash in the controller, you can do
419
+
420
+ ```ruby
421
+ class DishDeserializer < Deserializer::Base
422
+ attributes :id,
423
+ :name
424
+
425
+ has_one :alias_attributes, deserializer: AliasDeserializer, key: :alias
426
+ end
427
+ ```
428
+
429
+ which would output
430
+
431
+ ```ruby
432
+ {
433
+ id: 6,
434
+ name: "mac & cheese",
435
+ alias_attributes:
436
+ {
437
+ id: 83,
438
+ name: "macaroni and cheese"
439
+ }
440
+ }
441
+ ```
442
+
443
+
395
444
  ### has_many
396
445
  `has_many` association expects a param and its deserializer:
397
446
 
@@ -3,7 +3,7 @@ module Deserializer
3
3
  class HasOneAssociation < Association
4
4
 
5
5
  def to_hash( params )
6
- return {} unless params[name]
6
+ return {} unless params.has_key? key
7
7
  value = deserializer.from_params( params[key] )
8
8
 
9
9
  if object.respond_to? name
@@ -27,7 +27,7 @@ module Deserializer
27
27
  return { target => value }
28
28
  end
29
29
  else
30
- return { key => value }
30
+ return { name => value }
31
31
  end
32
32
  end
33
33
  end
@@ -1,3 +1,3 @@
1
1
  module Deserializer
2
- VERSION = "0.7.0"
2
+ VERSION = "0.8.0"
3
3
  end
@@ -134,4 +134,15 @@ class InheritedDeserializer < HasOneWithTargetDeserializer
134
134
  :that
135
135
 
136
136
  attribute :created_by
137
+ end
138
+
139
+ class KeyedHasManyDeserializer < Deserializer::Base
140
+ attribute :id
141
+ has_many :special_attributes, deserializer: AttributeDeserializer, key: :attributes
142
+ end
143
+
144
+ class KeyedHasOneDeserializer < Deserializer::Base
145
+ attribute :internal, key: :external
146
+
147
+ has_one :user_info, deserializer: ::BasicDeserializer, key: :thing
137
148
  end
@@ -99,6 +99,12 @@ class DeserializerTest < Minitest::Test
99
99
  assert_equal expected, VanillaHasOneDeserializer.from_params( params )
100
100
  end
101
101
 
102
+ def test_has_one_supports_key
103
+ expected = { internal: :thing, user_info: { user_id: 6, text: "text" }}
104
+ params = { external: :thing, thing: @params }
105
+ assert_equal expected, KeyedHasOneDeserializer.from_params( params )
106
+ end
107
+
102
108
  def test_has_one_requires_deserializer
103
109
  assert_raises Deserializer::DeserializerError do
104
110
  BasicDeserializer.has_one :splosion
@@ -167,6 +173,13 @@ class DeserializerTest < Minitest::Test
167
173
  assert_equal ({}), HasManyDeserializer.from_params( {} )
168
174
  end
169
175
 
176
+ def test_has_many_supports_key
177
+ params = { id: 1, attributes: [{user: 6, text: "lol"}, {user: 6, text: "something"}] }
178
+ expected = { id: 1, special_attributes: [{user_id: 6, text: "lol"}, {user_id: 6, text: "something"}] }
179
+
180
+ assert_equal expected, KeyedHasManyDeserializer.from_params( params )
181
+ end
182
+
170
183
  def test_has_one_handles_no_input
171
184
  assert_equal ({}), VanillaHasOneDeserializer.from_params( {} )
172
185
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: deserializer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Greg Orlov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-20 00:00:00.000000000 Z
11
+ date: 2016-03-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport