json_dumper 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +166 -2
  3. data/lib/json_dumper/version.rb +1 -1
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 79357d5f4525bea45352f2a0a4d12e256099254f
4
- data.tar.gz: 3e6a651e6eadd83dbde1c5086580dca624e4cd47
3
+ metadata.gz: 03696b97e2c460ce88cba10d65eb5bba0110e301
4
+ data.tar.gz: 968d98f039debe2b33984d7a776fa69fa8d74292
5
5
  SHA512:
6
- metadata.gz: b67062a0b892a32951eb9b53749b2ffc722829bf6d917ae8ff6944710e42121882f720c99f9820bf6c333a9b6777d7e9ee261994f72f149394f74fdfe0462c2c
7
- data.tar.gz: 52290d5cb95766106bf0fbc290b3d13459dec136dc277a1535060cd5240f98d4fccda6fd671f9060102fb6571f53de500845e9a21af0b0947b509e12a86f77b1
6
+ metadata.gz: d824e295886e4c8aebf0b629e3b7c2955a56ece629adb3dd71e80ba75f90574646eba7443257abc10b065873a71d105573884b85b03e65699bd7bbc0c3f15e28
7
+ data.tar.gz: 61bc5d9e059167159815d436e781c11f2df87ea37a1160d4af59093c889b494d29f344eaef00ea34e0aa8214c8d12b45c93fb2dbee3b4562216a527e63b08ccb
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # JsonDumper
2
2
 
3
- Serialize Ruby into JSON.
3
+ Serialize Ruby into JSON. Cleaner. Faster.
4
4
 
5
5
  This gem is intended to
6
6
  * help serialize Ruby objects and ActiveRecord objects into json
@@ -25,7 +25,171 @@ Or install it yourself as:
25
25
 
26
26
  ## Usage
27
27
 
28
- TODO: Write usage instructions here
28
+ Let's say you want to serialize Human object, which has many Cars.
29
+ First define a class that is going to serialize Human:
30
+ ```
31
+ class HumanJson < JsonDumper::Base
32
+ def preview
33
+ {
34
+ id: id,
35
+ first_name: first_name,
36
+ last_name: last_name,
37
+ born_at: created_at.strftime('%m/%d/%Y')
38
+ }
39
+ end
40
+ end
41
+ ```
42
+ you can call it like that:
43
+ ```
44
+ john = Human.create(
45
+ first_name: 'John',
46
+ last_name: 'Doe',
47
+ created_at: 20.years.ago
48
+ )
49
+
50
+ json = HumanJson.preview(john)
51
+ json == {
52
+ id: 1,
53
+ first_name: 'John',
54
+ last_name: 'Doe',
55
+ born_at: '09/19/1997'
56
+ }
57
+ ```
58
+
59
+ Whenever you invoke a method on a JsonDumper::Base instance and it is missing a similar method is invoked on the object you passed to the serializer.
60
+ For example in the snippet above a method `id` is going to be called on `john` object.
61
+ ```
62
+ {
63
+ id: id,
64
+ ...
65
+ }
66
+ ```
67
+
68
+ Let's introduce an association into the mix:
69
+ ```
70
+ class CarJson < JsonDumper::Base
71
+ def preview
72
+ {
73
+ id: id,
74
+ name: name,
75
+ }
76
+ end
77
+ end
78
+
79
+ class HumanJson < JsonDumper::Base
80
+ # ...
81
+ def details
82
+ preview.merge(
83
+ car: CarJson.preview(car)
84
+ )
85
+ end
86
+ end
87
+
88
+ ferrari = Car.create(
89
+ name: 'Ferrari',
90
+ )
91
+ john.car = ferrari
92
+
93
+ json = HumanJson.details(john)
94
+ json == {
95
+ id: 1,
96
+ first_name: 'John',
97
+ last_name: 'Doe',
98
+ born_at: '09/19/1997',
99
+ car: {
100
+ id: 1,
101
+ name: 'Ferrari'
102
+ }
103
+ }
104
+ ```
105
+
106
+ This structure provides a very clean way to specify dependencies for ActiveRecord preloader:
107
+ ```
108
+ class HumanJson < JsonDumper::Base
109
+ def preview
110
+ # ...
111
+ end
112
+
113
+ def preview_preload
114
+ {}
115
+ end
116
+
117
+ def details
118
+ # ...
119
+ end
120
+
121
+ def details_preload
122
+ preview_preload.merge(car: [])
123
+ end
124
+ end
125
+ ```
126
+ Furthermore you can omit defining `preview_preload` because JsonDumper returns empty hashes (`{}`) whenever a method does not exist and its name ends with `_preload`.
127
+
128
+ You can utilize it in the following way:
129
+ ```
130
+ preloader = ActiveRecord::Base::Preloader.new
131
+ preloader.preload(john, HumanJson.details_preload)
132
+ json = HumanJson.details(john)
133
+ ```
134
+ Another cool feature that you can now do is to do both preloading and serialization in a single command via `fetch_METHOD_NAME`.
135
+ This creates a special JsonDumper::Delayed object which delays its execution until it's time to render. This allows to do preloading at render time.
136
+
137
+ Since this is a common operation you can include `JsonDumper::Base` in your controller.
138
+ ```
139
+ class HumansController < ActionController::Base
140
+ include JsonDumper::Helper
141
+
142
+ def show
143
+ human = Human.find(params[:id])
144
+ json = dumper_json(
145
+ my_human: HumanJson.fetch_details(human)
146
+ )
147
+ render json: json
148
+ end
149
+
150
+ # OR
151
+
152
+ def show
153
+ human = Human.find(params[:id])
154
+ render_dumper_json(
155
+ my_human: HumanJson.fetch_details(human)
156
+ )
157
+ end
158
+ end
159
+
160
+ # going to render:
161
+ {
162
+ myHuman: {
163
+ id: 1,
164
+ firstName: 'John',
165
+ lastName: 'Doe',
166
+ bornAt: '09/19/1997',
167
+ car: {
168
+ id: 1,
169
+ name: 'Ferrari'
170
+ }
171
+ }
172
+ }
173
+ ```
174
+ Take a note that `dumper_json` also camelizes your keys.
175
+
176
+ ### Usage with [Gon](https://github.com/gazay/gon)
177
+
178
+ This gem also provides a seamless integration with Gon gem.
179
+ The above example could be rewritten in the following way:
180
+ ```
181
+ class HumansController < ActionController::Base
182
+ def show
183
+ human = Human.find(params[:id])
184
+ gon.my_human = HumanJson.fetch_details(human)
185
+ end
186
+ end
187
+ ```
188
+
189
+ Later in your javascript:
190
+ ```
191
+ console.log(gon.myHuman);
192
+ ```
29
193
 
30
194
  ## Development
31
195
 
@@ -1,3 +1,3 @@
1
1
  module JsonDumper
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_dumper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - therusskiy
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-08-13 00:00:00.000000000 Z
11
+ date: 2017-08-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord