jsonity 1.0.2 → 1.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +52 -42
- data/lib/jsonity/builder.rb +3 -5
- data/lib/jsonity/version.rb +1 -1
- 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: da188047e9b7ddbaecc15901058e76a25e5b5897
|
4
|
+
data.tar.gz: 1f5129af89a01e9be55a1a42df32de5d9b2b32ea
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bf9ec2f76b3d59a1e0ef274688bccee68a6682eb69ef0c28534f0fe68b81c0522d846fbb725838893671327b509882b21ee3a3cd4f224f33d737636f37f88d22
|
7
|
+
data.tar.gz: e80452f852e5a11ae60789405fac3160218050c680bf16ff77995de63927328df3237feae18efd889c44ef65b6ab8592cdbd2ac99fe2825decb9516e654ef838
|
data/README.md
CHANGED
@@ -7,7 +7,7 @@ I'd been writing JSON API with [Jbuilder](https://github.com/rails/jbuilder), [R
|
|
7
7
|
|
8
8
|
- Jbuilder is very verbose in syntax, and its functonalities of partial and mixin are actually weak
|
9
9
|
- RABL has simple syntax, but writing complex data structure with it is not very readable
|
10
|
-
- ActiveModel::Serializer is persuasive role in Rails architecture, but can get very useless when you need to fully control from controller what attributes of nested (associated) object to be included
|
10
|
+
- ActiveModel::Serializer is persuasive role in Rails architecture, but can get very useless when you need to fully control from controller what attributes of multi-nested (associated) object to be included
|
11
11
|
|
12
12
|
So I chose to create new one -- Jsonity, which is simple and powerful JSON builder especially for JSON API representations.
|
13
13
|
|
@@ -81,7 +81,7 @@ Jsonity.build { |t|
|
|
81
81
|
Usage
|
82
82
|
-----
|
83
83
|
|
84
|
-
###
|
84
|
+
### Data object assignment
|
85
85
|
|
86
86
|
To declare the data object for use:
|
87
87
|
|
@@ -92,7 +92,7 @@ Jsonity.build { |t|
|
|
92
92
|
}
|
93
93
|
```
|
94
94
|
|
95
|
-
Or
|
95
|
+
Or passing as an argument:
|
96
96
|
|
97
97
|
```ruby
|
98
98
|
Jsonity.build(@user) { |user|
|
@@ -128,8 +128,8 @@ Jsonity.build(@user) { |user|
|
|
128
128
|
# block parameter isn't required
|
129
129
|
user.russian_roulette { rand(1..10) }
|
130
130
|
|
131
|
-
# or with specified object
|
132
|
-
user.
|
131
|
+
# or with specified the data object
|
132
|
+
user.hello('world') { |w| w.upcase }
|
133
133
|
|
134
134
|
# block can be omitted if the value is constant
|
135
135
|
user.seventeen 17
|
@@ -138,7 +138,7 @@ Jsonity.build(@user) { |user|
|
|
138
138
|
{
|
139
139
|
"full_name": "John Smith",
|
140
140
|
"russian_roulette": 4,
|
141
|
-
"
|
141
|
+
"hello": "WORLD",
|
142
142
|
"seventeen": 17
|
143
143
|
}
|
144
144
|
=end
|
@@ -148,7 +148,6 @@ Aliased attributes works well as you expected:
|
|
148
148
|
|
149
149
|
```ruby
|
150
150
|
Jsonity.build(@user) { |user|
|
151
|
-
# show `id` as `my_id`
|
152
151
|
user.my_id &:id
|
153
152
|
}
|
154
153
|
=begin
|
@@ -173,8 +172,21 @@ Jsonity.build(@user) { |user|
|
|
173
172
|
}
|
174
173
|
}
|
175
174
|
=begin
|
176
|
-
|
175
|
+
{
|
176
|
+
"name": "John Smith",
|
177
|
+
"avatar": {
|
178
|
+
"image_url": "http://www.example.com/avatar.png",
|
179
|
+
"width": 512,
|
180
|
+
"height": 512
|
181
|
+
}
|
182
|
+
}
|
183
|
+
=end
|
184
|
+
```
|
177
185
|
|
186
|
+
Assume that `@user.avatar` is `nil`, the output will be:
|
187
|
+
|
188
|
+
```ruby
|
189
|
+
=begin
|
178
190
|
{
|
179
191
|
"name": "John Smith",
|
180
192
|
"avatar": {
|
@@ -208,7 +220,7 @@ Assume that `@user.avatar` is `nil`,
|
|
208
220
|
=end
|
209
221
|
```
|
210
222
|
|
211
|
-
|
223
|
+
To specify the data object to use inside a block:
|
212
224
|
|
213
225
|
```ruby
|
214
226
|
Jsonity.build { |t|
|
@@ -231,12 +243,12 @@ Jsonity.build { |t|
|
|
231
243
|
=end
|
232
244
|
```
|
233
245
|
|
234
|
-
Or a block can inherit the parent object:
|
246
|
+
Or a block can inherit the parent data object:
|
235
247
|
|
236
248
|
```ruby
|
237
249
|
Jsonity.build { |t|
|
238
250
|
t.user!(@user) { |user|
|
239
|
-
|
251
|
+
user.profile!(inherit: true) { |profile|
|
240
252
|
profile.name # @user.name
|
241
253
|
}
|
242
254
|
}
|
@@ -252,6 +264,31 @@ Jsonity.build { |t|
|
|
252
264
|
=end
|
253
265
|
```
|
254
266
|
|
267
|
+
### Array nodes
|
268
|
+
|
269
|
+
Including a collection of objects, just use `[]` and write the same syntax of hash node:
|
270
|
+
|
271
|
+
```ruby
|
272
|
+
Jsonity.build(@user) { |user|
|
273
|
+
user[].friends! { |friend|
|
274
|
+
friend.name # @user.friends[i].name
|
275
|
+
}
|
276
|
+
}
|
277
|
+
=begin
|
278
|
+
{
|
279
|
+
"friends": [
|
280
|
+
{ "name": "John Smith" },
|
281
|
+
{ "name": "William Northington" }
|
282
|
+
]
|
283
|
+
}
|
284
|
+
=end
|
285
|
+
```
|
286
|
+
|
287
|
+
Similar to hash nodes in naming convention,
|
288
|
+
if `@user.friends = nil` nodes suffix with `!` will be an empty array `[]`, in contrast, some with `?` will be `null`.
|
289
|
+
|
290
|
+
Also passing the data object or inheritance can be done in the same way as hash nodes.
|
291
|
+
|
255
292
|
### Automatic attributes inclusion
|
256
293
|
|
257
294
|
If you set `attr_json` in any class, **the specified attributes will automatically be included**:
|
@@ -299,33 +336,6 @@ Jsonity.build { |t|
|
|
299
336
|
=end
|
300
337
|
```
|
301
338
|
|
302
|
-
|
303
|
-
### Array nodes
|
304
|
-
|
305
|
-
Including a collection of objects, just use `[]` and write the same syntax of hash node:
|
306
|
-
|
307
|
-
```ruby
|
308
|
-
Jsonity.build(@user) { |user|
|
309
|
-
user[].friends! { |friend|
|
310
|
-
friend.name # @user.friends[i].name
|
311
|
-
}
|
312
|
-
}
|
313
|
-
=begin
|
314
|
-
{
|
315
|
-
"friends": [
|
316
|
-
{ "name": "John Smith" },
|
317
|
-
{ "name": "William Northington" }
|
318
|
-
]
|
319
|
-
}
|
320
|
-
=end
|
321
|
-
```
|
322
|
-
|
323
|
-
Similar to hash nodes in naming convention,
|
324
|
-
if `@user.friends = nil` nodes suffix with `!` will be an empty array `[]`, in contrast, some with `?` will be `null`.
|
325
|
-
|
326
|
-
Also passing the object or inheritance can be done in the same way as hash nodes.
|
327
|
-
|
328
|
-
|
329
339
|
### Mixin / Scope
|
330
340
|
|
331
341
|
Since Jsonity aim to be simple and light, **use plain `Proc`** to fullfill functonality of mixin.
|
@@ -355,7 +365,7 @@ Jsonity.build { |t|
|
|
355
365
|
=end
|
356
366
|
```
|
357
367
|
|
358
|
-
|
368
|
+
To explicitly **specify the data object to use** in mixin, you can do by passing it in the first argument:
|
359
369
|
|
360
370
|
```ruby
|
361
371
|
Jsonity.build { |t|
|
@@ -369,7 +379,7 @@ Jsonity.build { |t|
|
|
369
379
|
=end
|
370
380
|
```
|
371
381
|
|
372
|
-
So you take this functonality for **scope**:
|
382
|
+
So you can take this functonality for **scope**:
|
373
383
|
|
374
384
|
```ruby
|
375
385
|
Jsonity.build { |t|
|
@@ -419,9 +429,9 @@ Notice that two objects `meta!` got merged.
|
|
419
429
|
```
|
420
430
|
|
421
431
|
|
422
|
-
### Using object
|
432
|
+
### Using data object
|
423
433
|
|
424
|
-
You can get the
|
434
|
+
You can get the data object as a second block parameter.
|
425
435
|
|
426
436
|
```ruby
|
427
437
|
Jsonity.build { |t|
|
data/lib/jsonity/builder.rb
CHANGED
@@ -157,17 +157,15 @@ module Jsonity
|
|
157
157
|
|
158
158
|
obj = get_object_for name, options
|
159
159
|
|
160
|
-
|
161
|
-
|
162
|
-
if !is_array && options[:_nullable]
|
160
|
+
if !obj && options[:_nullable]
|
163
161
|
@content[name] ||= nil
|
164
162
|
else
|
165
163
|
@content[name] = [] unless @content[name].is_a?(::Array)
|
166
164
|
end
|
167
165
|
|
168
|
-
if
|
166
|
+
if obj
|
169
167
|
@deferred_array_blocks[name] = {
|
170
|
-
obj:
|
168
|
+
obj: obj.to_a,
|
171
169
|
blocks: [block],
|
172
170
|
}
|
173
171
|
end
|
data/lib/jsonity/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsonity
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yuki Iwanaga
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-09-
|
11
|
+
date: 2014-09-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|