jsonapi-object-mapper 0.3.0 → 0.4.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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 35390dfcd2da06f3fadc628d41986b07891efa2590a4271896a9717ed44ed7b6
|
4
|
+
data.tar.gz: eb48c0ab5e7409c53ba03828704adcef9e27377443498b4f5a97430cde8cd5ad
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 96a5b2682366e3d45ff87b768c1ace3463847721293a0237b7661fe4c0d19b2049ed2f1616b73f9d47eea4a62b69d7fb359e1e86753ff1123388c171a7f62e97
|
7
|
+
data.tar.gz: 5d75d3a36a2cd47ef118dcd4e51f31129e5f7cd8e8e7b0daaa93077e8d809551261f52a2ef321e4c20a30a8a82d75dad2344056413e95d740234978b45d6fe72
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -33,8 +33,9 @@ class User < JsonAPIObjectMapper::Deserializer::Resource
|
|
33
33
|
# Embedding with another Resource class, will deserialize the `included` resource with the given class
|
34
34
|
has_one :photo, embed_with: Photo
|
35
35
|
|
36
|
-
# By default the value will be assigned whatever
|
37
|
-
#
|
36
|
+
# By default the value will be assigned whatever is located in the `included` selection.
|
37
|
+
# Otherwise basic relationship resource information will be added.
|
38
|
+
# - IE: { "type" = "friend", "id" = "10" }
|
38
39
|
has_one :friend
|
39
40
|
|
40
41
|
# This will accept the default value
|
@@ -45,8 +46,7 @@ class User < JsonAPIObjectMapper::Deserializer::Resource
|
|
45
46
|
attr_value.upcase
|
46
47
|
end
|
47
48
|
|
48
|
-
# You can mass
|
49
|
-
|
49
|
+
# You can mass-assign attributes using the `attributes` method instead if blocks don't matter
|
50
50
|
attributes :ssn, :passport, :more_person_info
|
51
51
|
end
|
52
52
|
|
@@ -54,7 +54,7 @@ end
|
|
54
54
|
User.call_collection(json_payload) #=> [<#User:123>, <#User:432>]
|
55
55
|
user = User.call(json_payload) #=> <#User:123>
|
56
56
|
|
57
|
-
user.first_name #=> "
|
57
|
+
user.first_name #=> "FOOER"
|
58
58
|
user.last_name #=> "Bar"
|
59
59
|
|
60
60
|
```
|
@@ -16,10 +16,12 @@ module JsonAPIObjectMapper
|
|
16
16
|
|
17
17
|
def id(&block)
|
18
18
|
self.id_block = block || DEFAULT_ID_BLOCK
|
19
|
+
define_method(:id) { fetch_attribute(:id) }
|
19
20
|
end
|
20
21
|
|
21
22
|
def type(&block)
|
22
23
|
self.type_block = block || DEFAULT_TYPE_BLOCK
|
24
|
+
define_method(:type) { fetch_attribute(:type) }
|
23
25
|
end
|
24
26
|
|
25
27
|
def attribute(attribute_name, &block)
|
@@ -9,7 +9,7 @@ module JsonAPIObjectMapper
|
|
9
9
|
extend DSL
|
10
10
|
|
11
11
|
class << self
|
12
|
-
attr_accessor :rel_blocks, :rel_options, :attr_blocks
|
12
|
+
attr_accessor :rel_blocks, :rel_options, :attr_blocks, :id_block, :type_block
|
13
13
|
end
|
14
14
|
instance_variable_set("@attr_blocks", {})
|
15
15
|
instance_variable_set("@rel_blocks", {})
|
@@ -20,6 +20,8 @@ module JsonAPIObjectMapper
|
|
20
20
|
klass.instance_variable_set("@attr_blocks", attr_blocks.dup)
|
21
21
|
klass.instance_variable_set("@rel_blocks", rel_blocks.dup)
|
22
22
|
klass.instance_variable_set("@rel_options", rel_options.dup)
|
23
|
+
klass.instance_variable_set("@id_block", id_block)
|
24
|
+
klass.instance_variable_set("@type_block", type_block)
|
23
25
|
end
|
24
26
|
|
25
27
|
def self.call_collection(document)
|
@@ -41,6 +43,8 @@ module JsonAPIObjectMapper
|
|
41
43
|
def initialize(payload = nil, included = nil)
|
42
44
|
super()
|
43
45
|
data = payload || {}
|
46
|
+
@id = data["id"]
|
47
|
+
@type = data["type"]
|
44
48
|
@attributes = data["attributes"] || {}
|
45
49
|
@relationships = data["relationships"] || {}
|
46
50
|
@includes = IncludedResources.load(included)
|
@@ -52,10 +56,16 @@ module JsonAPIObjectMapper
|
|
52
56
|
private
|
53
57
|
|
54
58
|
def deserialize!
|
59
|
+
deserialize_id_type!
|
55
60
|
deserialize_attributes!
|
56
61
|
deserialize_relationships!
|
57
62
|
end
|
58
63
|
|
64
|
+
def deserialize_id_type!
|
65
|
+
assign_attribute("id", self.class.id_block&.call(@id))
|
66
|
+
assign_attribute("type", self.class.type_block&.call(@id))
|
67
|
+
end
|
68
|
+
|
59
69
|
def deserialize_attributes!
|
60
70
|
return if @attributes.empty?
|
61
71
|
@attributes.each_pair(&method(:initialize_attribute))
|