@@ -6,184 +6,4 @@ layout: guides
66Custom deserializable resources are defined by subclassing
77` JSONAPI::Deserializable::Resource ` and using its DSL.
88
9- Example:
10-
11- ``` ruby
12- class DeserializablePost < JSONAPI ::Deserializable ::Resource
13- attribute :date do |attr |
14- { created_at: attr }
15- end
16-
17- relationship :author do |_rel , id , type |
18- { user_id: id,
19- user_type: type }
20- end
21-
22- relationship :comments do |_rel , ids , _types |
23- { response_ids: ids }
24- end
25- end
26-
27- # Will allow to build the following hash:
28- # => {
29- # type: 'posts',
30- # id: '5',
31- # created_at: '2016-11-18',
32- # title: 'Hello JSON API',
33- # user_id: '12',
34- # user_type: 'users',
35- # response_ids: ['54', '32', '72']
36- # }
37- ```
38-
39- The principle is simple: the payload elements for which a custom deserialization
40- scheme was defined will use that, while other fields will be deserialized using
41- the default deserialization scheme (which can itself be configured).
42-
43- Note: If the targeted element does not exist in the payload, the corresponding
44- fields are not defined.
45-
46- ## Type
47-
48- The type of the primary data can be accessed via the ` type ` DSL method.
49-
50- Example:
51-
52- ``` ruby
53- class DeserializablePost < JSONAPI ::Deserializable ::Post
54- # ...
55- type do |t |
56- { primary_type: t.capitalize }
57- end
58- end
59- ```
60-
61- ## Id
62-
63- The id of the primary data can be accessed via the ` id ` DSL method.
64-
65- Example:
66-
67- ``` ruby
68- class DeserializablePost < JSONAPI ::Deserializable ::Post
69- # ...
70- id do |i |
71- { primary_id: i.to_i }
72- end
73- end
74- ```
75-
76- ## Attributes
77-
78- Attributes of the primary data can be accessed via the ` attribute ` DSL method.
79- The ` attribute ` method takes a symbol representing the name of the targeted
80- attribute in the input payload, and a block to define field(s) of the resulting
81- hash.
82-
83- Example:
84-
85- ``` ruby
86- class DeserializablePost < JSONAPI ::Deserializable ::Post
87- # ...
88- attribute :date do |d |
89- { created_at: d }
90- end
91- end
92- ```
93-
94- ## Relationships
95-
96- Relationships of the primary data can be accessed via the ` has_many ` and
97- ` has_one ` DSL methods.
98-
99- ### To-many relationships
100-
101- The ` has_many ` DSL method takes a symbol representing the name of the targeted
102- relationship in the input payload, and a block to define field(s) of the
103- resulting hash. The block is called with three parameters: ` relationship ` (the
104- whole relationship hash of the input payload), ` ids ` (an array of the ids of
105- related resources), and ` types ` (an array of types of the related resources).
106-
107- Example:
108-
109- ``` ruby
110- class DeserializablePost < JSONAPI ::Deserializable ::Post
111- # ...
112- has_many :comments do |rel , ids , types |
113- { comment_ids: ids,
114- comment_types: types.map(& :capitalize ),
115- comment_meta: rel[' meta' ] }
116- end
117- end
118- ```
119-
120- ### To-one relationships
121-
122- The ` has_one ` DSL method takes a symbol representing the name of the targeted
123- relationship in the input payload, and a block to define field(s) of the
124- resulting hash. The block is called with three parameters: ` relationship ` (the
125- whole relationship hash of the input payload), ` id ` (the id of the related
126- resource), and ` type ` (the type of the related resource).
127-
128- Example:
129-
130- ``` ruby
131- class DeserializablePost < JSONAPI ::Deserializable ::Post
132- # ...
133- has_one :author do |rel , id , type |
134- { author_id: id,
135- author_type: type.capitalize,
136- author_meta: rel[' meta' ] }
137- end
138- end
139- ```
140-
141- ## Meta
142-
143- Not available yet.
144-
145- ## Links
146-
147- Not available yet.
148-
149- ## Configuration
150-
151- The default deserialization scheme can be configure in the following way:
152-
153- ``` ruby
154- # Modifying the global default deserialization scheme
155- JSONAPI ::Deserializable ::Resource .configure do |config |
156- config.default_id = proc { |id | { id: id } }
157- config.default_type = proc { |type | { type: type } }
158- config.default_attribute = proc do |key , value |
159- { key => value }
160- end
161- config.default_has_one = proc do |key , rel , id , type |
162- { " #{ key } _id}" .to_sym => id, " #{ key } _type" .to_sym => type }
163- end
164- config.default_has_many = proc do |key , rel , ids , types |
165- { " #{ key } _ids}" .to_sym => ids, " #{ key } _types" .to_sym => types }
166- end
167- end
168-
169- # Modifying the default deserialization scheme of a single deserializable
170- # resource class
171-
172- class DeserializablePost < JSONAPI ::Deserializable ::Resource
173- # ...
174- end
175-
176- DeserializablePost .configure do |config |
177- config.default_id = proc { |id | { id: id } }
178- config.default_type = proc { |type | { type: type } }
179- config.default_attribute = proc do |key , value |
180- { key => value }
181- end
182- config.default_has_one = proc do |key , rel , id , type |
183- { " #{ key } _id" .to_sym => id, " #{ key } _type" .to_sym => type }
184- end
185- config.default_has_many = proc do |key , rel , ids , types |
186- { " #{ key } _ids" .to_sym => ids, " #{ key } _types" .to_sym => types }
187- end
188- end
189- ```
9+ Documentation currently being rewritten.
0 commit comments