jsonity 1.0.2 → 1.0.3

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: c2659fcf6db5641ea0b6dbf288e14562dcd57cc6
4
- data.tar.gz: 87eea35053efa6c0f1b5dc7ec4ae5a6185ff22b2
3
+ metadata.gz: da188047e9b7ddbaecc15901058e76a25e5b5897
4
+ data.tar.gz: 1f5129af89a01e9be55a1a42df32de5d9b2b32ea
5
5
  SHA512:
6
- metadata.gz: 97c515d3530a9922573f0cc1902739aa5e5876f248c0b2ad3d345fb67cf9860c22a6a4100d30ebdb966626af45470ea2bf8725291c015c76ad62aa0ef4469f97
7
- data.tar.gz: 8db7d2049b3996e47c7c7c57075edbb6321596f9ddef05b6a801a94eab698b3e7420bdb4895885eabeab7c01235069c4bb4e66b556a68e9844359788d43c120d
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
- ### Object assignment
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 pass as an argument:
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.feature_time(Time.now) { |now| now + 1.years }
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
- "feature_time": "2015-09-13 12:32:39 +0900",
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
- Assume that `@user.avatar` is `nil`,
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
- Explicitly specify an object to use inside a block:
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
- t.profile!(inherit: true) { |profile|
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
- In case you might explicitly **specify an object to use** in mixin, you can do by passing it in the first argument:
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 current object as a second block parameter.
434
+ You can get the data object as a second block parameter.
425
435
 
426
436
  ```ruby
427
437
  Jsonity.build { |t|
@@ -157,17 +157,15 @@ module Jsonity
157
157
 
158
158
  obj = get_object_for name, options
159
159
 
160
- is_array = obj && obj.class < ::Enumerable
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 is_array
166
+ if obj
169
167
  @deferred_array_blocks[name] = {
170
- obj: obj,
168
+ obj: obj.to_a,
171
169
  blocks: [block],
172
170
  }
173
171
  end
@@ -1,3 +1,3 @@
1
1
  module Jsonity
2
- VERSION = '1.0.2'
2
+ VERSION = '1.0.3'
3
3
  end
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.2
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-13 00:00:00.000000000 Z
11
+ date: 2014-09-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler