deserializer 0.7.0 → 0.8.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 +2 -2
- data/README.md +49 -0
- data/lib/deserializer/attribute/has_one_association.rb +2 -2
- data/lib/deserializer/version.rb +1 -1
- data/test/lib/deserializers.rb +11 -0
- data/test/unit/deserializer_test.rb +13 -0
- 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: ad134a88991784cdb31663a128857d6bdd2f818f
|
4
|
+
data.tar.gz: 56a0ea6c8d1c0c44eba5e4b26eff012d39e6407e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1fa4cf73fe4f13b057f6577925ca5a166de76d580d69d4d7bee2d22860cda19c2e36df169bf7a97d0c5c92f9449763093e6561619eafa89ca0bc43682c0cf001
|
7
|
+
data.tar.gz: 7cb020cad12a271885e6dc6581fdf499119fa9f8d9e237fde90693fb526636be84e2db7348b0119db2966d7c2ce7d65f94f4b167f877c7227efc56d2fff37e12
|
data/Gemfile.lock
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
deserializer (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.
|
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
|
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 {
|
30
|
+
return { name => value }
|
31
31
|
end
|
32
32
|
end
|
33
33
|
end
|
data/lib/deserializer/version.rb
CHANGED
data/test/lib/deserializers.rb
CHANGED
@@ -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.
|
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-
|
11
|
+
date: 2016-03-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|