lp-serializable 0.2.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +650 -0
- data/.travis.yml +1 -1
- data/Gemfile.lock +12 -12
- data/README.md +98 -35
- data/lib/fast_jsonapi/object_serializer.rb +8 -1
- data/lib/fast_jsonapi/serialization_core.rb +3 -6
- data/lib/lp/serializable.rb +30 -18
- data/lib/lp/serializable/exceptions.rb +11 -0
- data/lib/lp/serializable/strategies.rb +12 -8
- data/lib/lp/serializable/utilities.rb +17 -13
- data/lib/lp/serializable/version.rb +2 -2
- data/lp-serializable.gemspec +3 -3
- metadata +7 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a7075a5d45e3269b8dca2d8504bbc73b4e8dfd9de407c2676762f14013903ccf
|
4
|
+
data.tar.gz: 9874bc7adfee9b4173217b22243e34f64c02b268f083a1a6da6f9e2dfb428c16
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 86ec74de30ed67b821d39814479b3c3af16a25d6c425c0a8bc938bccebf2c9c9b3c3b7507b91eea1c4aeb6580e0f14a1e2258a5c40a84546e57ee648af6c342a
|
7
|
+
data.tar.gz: 2617995b0005c59bd5302aac10fcfc13df80f8e41ee43a811c0d643bfef489bc6a8aac4c659685c2c8c3805baa7f77d4c77945b0941f9068677f9fa9e9e0ec57
|
data/.rubocop.yml
ADDED
@@ -0,0 +1,650 @@
|
|
1
|
+
AllCops:
|
2
|
+
Exclude:
|
3
|
+
- db/schema.rb
|
4
|
+
|
5
|
+
Naming/AccessorMethodName:
|
6
|
+
Description: Check the naming of accessor methods for get_/set_.
|
7
|
+
Enabled: false
|
8
|
+
|
9
|
+
Style/Alias:
|
10
|
+
Description: 'Use alias_method instead of alias.'
|
11
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#alias-method'
|
12
|
+
Enabled: false
|
13
|
+
|
14
|
+
Style/ArrayJoin:
|
15
|
+
Description: 'Use Array#join instead of Array#*.'
|
16
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#array-join'
|
17
|
+
Enabled: false
|
18
|
+
|
19
|
+
Style/AsciiComments:
|
20
|
+
Description: 'Use only ascii symbols in comments.'
|
21
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#english-comments'
|
22
|
+
Enabled: false
|
23
|
+
|
24
|
+
Naming/AsciiIdentifiers:
|
25
|
+
Description: 'Use only ascii symbols in identifiers.'
|
26
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#english-identifiers'
|
27
|
+
Enabled: false
|
28
|
+
|
29
|
+
Style/Attr:
|
30
|
+
Description: 'Checks for uses of Module#attr.'
|
31
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#attr'
|
32
|
+
Enabled: false
|
33
|
+
|
34
|
+
Metrics/BlockNesting:
|
35
|
+
Description: 'Avoid excessive block nesting'
|
36
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#three-is-the-number-thou-shalt-count'
|
37
|
+
Enabled: false
|
38
|
+
|
39
|
+
Style/CaseEquality:
|
40
|
+
Description: 'Avoid explicit use of the case equality operator(===).'
|
41
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#no-case-equality'
|
42
|
+
Enabled: false
|
43
|
+
|
44
|
+
Style/CharacterLiteral:
|
45
|
+
Description: 'Checks for uses of character literals.'
|
46
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#no-character-literals'
|
47
|
+
Enabled: false
|
48
|
+
|
49
|
+
Style/ClassAndModuleChildren:
|
50
|
+
Description: 'Checks style of children classes and modules.'
|
51
|
+
Enabled: true
|
52
|
+
EnforcedStyle: nested
|
53
|
+
|
54
|
+
Metrics/ClassLength:
|
55
|
+
Description: 'Avoid classes longer than 100 lines of code.'
|
56
|
+
Enabled: false
|
57
|
+
|
58
|
+
Metrics/ModuleLength:
|
59
|
+
Description: 'Avoid modules longer than 100 lines of code.'
|
60
|
+
Enabled: false
|
61
|
+
|
62
|
+
Style/ClassVars:
|
63
|
+
Description: 'Avoid the use of class variables.'
|
64
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#no-class-vars'
|
65
|
+
Enabled: false
|
66
|
+
|
67
|
+
Style/CollectionMethods:
|
68
|
+
Enabled: true
|
69
|
+
PreferredMethods:
|
70
|
+
find: detect
|
71
|
+
inject: reduce
|
72
|
+
collect: map
|
73
|
+
find_all: select
|
74
|
+
|
75
|
+
Style/ColonMethodCall:
|
76
|
+
Description: 'Do not use :: for method call.'
|
77
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#double-colons'
|
78
|
+
Enabled: false
|
79
|
+
|
80
|
+
Style/CommentAnnotation:
|
81
|
+
Description: >-
|
82
|
+
Checks formatting of special comments
|
83
|
+
(TODO, FIXME, OPTIMIZE, HACK, REVIEW).
|
84
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#annotate-keywords'
|
85
|
+
Enabled: false
|
86
|
+
|
87
|
+
Metrics/AbcSize:
|
88
|
+
Description: >-
|
89
|
+
A calculated magnitude based on number of assignments,
|
90
|
+
branches, and conditions.
|
91
|
+
Enabled: false
|
92
|
+
|
93
|
+
Metrics/BlockLength:
|
94
|
+
CountComments: true # count full line comments?
|
95
|
+
Max: 25
|
96
|
+
ExcludedMethods: []
|
97
|
+
Exclude:
|
98
|
+
- "spec/**/*"
|
99
|
+
|
100
|
+
Metrics/CyclomaticComplexity:
|
101
|
+
Description: >-
|
102
|
+
A complexity metric that is strongly correlated to the number
|
103
|
+
of test cases needed to validate a method.
|
104
|
+
Enabled: false
|
105
|
+
|
106
|
+
Rails/Delegate:
|
107
|
+
Description: 'Prefer delegate method for delegations.'
|
108
|
+
Enabled: false
|
109
|
+
|
110
|
+
Style/PreferredHashMethods:
|
111
|
+
Description: 'Checks use of `has_key?` and `has_value?` Hash methods.'
|
112
|
+
StyleGuide: '#hash-key'
|
113
|
+
Enabled: false
|
114
|
+
|
115
|
+
Style/Documentation:
|
116
|
+
Description: 'Document classes and non-namespace modules.'
|
117
|
+
Enabled: false
|
118
|
+
|
119
|
+
Style/DoubleNegation:
|
120
|
+
Description: 'Checks for uses of double negation (!!).'
|
121
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#no-bang-bang'
|
122
|
+
Enabled: false
|
123
|
+
|
124
|
+
Style/EachWithObject:
|
125
|
+
Description: 'Prefer `each_with_object` over `inject` or `reduce`.'
|
126
|
+
Enabled: false
|
127
|
+
|
128
|
+
Style/EmptyLiteral:
|
129
|
+
Description: 'Prefer literals to Array.new/Hash.new/String.new.'
|
130
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#literal-array-hash'
|
131
|
+
Enabled: false
|
132
|
+
|
133
|
+
# Checks whether the source file has a utf-8 encoding comment or not
|
134
|
+
# AutoCorrectEncodingComment must match the regex
|
135
|
+
# /#.*coding\s?[:=]\s?(?:UTF|utf)-8/
|
136
|
+
Style/Encoding:
|
137
|
+
Enabled: false
|
138
|
+
|
139
|
+
Style/EvenOdd:
|
140
|
+
Description: 'Favor the use of Fixnum#even? && Fixnum#odd?'
|
141
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#predicate-methods'
|
142
|
+
Enabled: false
|
143
|
+
|
144
|
+
Naming/FileName:
|
145
|
+
Description: 'Use snake_case for source file names.'
|
146
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#snake-case-files'
|
147
|
+
Enabled: false
|
148
|
+
|
149
|
+
Style/FrozenStringLiteralComment:
|
150
|
+
Description: >-
|
151
|
+
Add the frozen_string_literal comment to the top of files
|
152
|
+
to help transition from Ruby 2.3.0 to Ruby 3.0.
|
153
|
+
Enabled: false
|
154
|
+
|
155
|
+
Style/FlipFlop:
|
156
|
+
Description: 'Checks for flip flops'
|
157
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#no-flip-flops'
|
158
|
+
Enabled: false
|
159
|
+
|
160
|
+
Style/FormatString:
|
161
|
+
Description: 'Enforce the use of Kernel#sprintf, Kernel#format or String#%.'
|
162
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#sprintf'
|
163
|
+
Enabled: false
|
164
|
+
|
165
|
+
Style/GlobalVars:
|
166
|
+
Description: 'Do not introduce global variables.'
|
167
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#instance-vars'
|
168
|
+
Reference: 'http://www.zenspider.com/Languages/Ruby/QuickRef.html'
|
169
|
+
Enabled: false
|
170
|
+
|
171
|
+
Style/GuardClause:
|
172
|
+
Description: 'Check for conditionals that can be replaced with guard clauses'
|
173
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#no-nested-conditionals'
|
174
|
+
Enabled: false
|
175
|
+
|
176
|
+
Style/IfUnlessModifier:
|
177
|
+
Description: >-
|
178
|
+
Favor modifier if/unless usage when you have a
|
179
|
+
single-line body.
|
180
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#if-as-a-modifier'
|
181
|
+
Enabled: false
|
182
|
+
|
183
|
+
Style/IfWithSemicolon:
|
184
|
+
Description: 'Do not use if x; .... Use the ternary operator instead.'
|
185
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#no-semicolon-ifs'
|
186
|
+
Enabled: false
|
187
|
+
|
188
|
+
Style/InlineComment:
|
189
|
+
Description: 'Avoid inline comments.'
|
190
|
+
Enabled: false
|
191
|
+
|
192
|
+
Style/Lambda:
|
193
|
+
Description: 'Use the new lambda literal syntax for single-line blocks.'
|
194
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#lambda-multi-line'
|
195
|
+
Enabled: false
|
196
|
+
|
197
|
+
Style/LambdaCall:
|
198
|
+
Description: 'Use lambda.call(...) instead of lambda.(...).'
|
199
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#proc-call'
|
200
|
+
Enabled: false
|
201
|
+
|
202
|
+
Style/LineEndConcatenation:
|
203
|
+
Description: >-
|
204
|
+
Use \ instead of + or << to concatenate two string literals at
|
205
|
+
line end.
|
206
|
+
Enabled: false
|
207
|
+
|
208
|
+
Metrics/LineLength:
|
209
|
+
Description: 'Limit lines to 80 characters.'
|
210
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#80-character-limits'
|
211
|
+
Max: 80
|
212
|
+
|
213
|
+
Metrics/MethodLength:
|
214
|
+
Description: 'Avoid methods longer than 10 lines of code.'
|
215
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#short-methods'
|
216
|
+
Enabled: false
|
217
|
+
|
218
|
+
Style/ModuleFunction:
|
219
|
+
Description: 'Checks for usage of `extend self` in modules.'
|
220
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#module-function'
|
221
|
+
Enabled: false
|
222
|
+
|
223
|
+
Style/MultilineBlockChain:
|
224
|
+
Description: 'Avoid multi-line chains of blocks.'
|
225
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#single-line-blocks'
|
226
|
+
Enabled: false
|
227
|
+
|
228
|
+
Style/NegatedIf:
|
229
|
+
Description: >-
|
230
|
+
Favor unless over if for negative conditions
|
231
|
+
(or control flow or).
|
232
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#unless-for-negatives'
|
233
|
+
Enabled: false
|
234
|
+
|
235
|
+
Style/NegatedWhile:
|
236
|
+
Description: 'Favor until over while for negative conditions.'
|
237
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#until-for-negatives'
|
238
|
+
Enabled: false
|
239
|
+
|
240
|
+
Style/Next:
|
241
|
+
Description: 'Use `next` to skip iteration instead of a condition at the end.'
|
242
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#no-nested-conditionals'
|
243
|
+
Enabled: false
|
244
|
+
|
245
|
+
Style/NilComparison:
|
246
|
+
Description: 'Prefer x.nil? to x == nil.'
|
247
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#predicate-methods'
|
248
|
+
Enabled: false
|
249
|
+
|
250
|
+
Style/Not:
|
251
|
+
Description: 'Use ! instead of not.'
|
252
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#bang-not-not'
|
253
|
+
Enabled: false
|
254
|
+
|
255
|
+
Style/NumericLiterals:
|
256
|
+
Description: >-
|
257
|
+
Add underscores to large numeric literals to improve their
|
258
|
+
readability.
|
259
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#underscores-in-numerics'
|
260
|
+
Enabled: false
|
261
|
+
|
262
|
+
Style/OneLineConditional:
|
263
|
+
Description: >-
|
264
|
+
Favor the ternary operator(?:) over
|
265
|
+
if/then/else/end constructs.
|
266
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#ternary-operator'
|
267
|
+
Enabled: false
|
268
|
+
|
269
|
+
Naming/BinaryOperatorParameterName:
|
270
|
+
Description: 'When defining binary operators, name the argument other.'
|
271
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#other-arg'
|
272
|
+
Enabled: false
|
273
|
+
|
274
|
+
Metrics/ParameterLists:
|
275
|
+
Description: 'Avoid parameter lists longer than three or four parameters.'
|
276
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#too-many-params'
|
277
|
+
Enabled: false
|
278
|
+
|
279
|
+
Style/PercentLiteralDelimiters:
|
280
|
+
Description: 'Use `%`-literal delimiters consistently'
|
281
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#percent-literal-braces'
|
282
|
+
Enabled: false
|
283
|
+
|
284
|
+
Style/PerlBackrefs:
|
285
|
+
Description: 'Avoid Perl-style regex back references.'
|
286
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#no-perl-regexp-last-matchers'
|
287
|
+
Enabled: false
|
288
|
+
|
289
|
+
Naming/PredicateName:
|
290
|
+
Description: 'Check the names of predicate methods.'
|
291
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#bool-methods-qmark'
|
292
|
+
NamePrefixBlacklist:
|
293
|
+
- is_
|
294
|
+
Exclude:
|
295
|
+
- spec/**/*
|
296
|
+
|
297
|
+
Style/Proc:
|
298
|
+
Description: 'Use proc instead of Proc.new.'
|
299
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#proc'
|
300
|
+
Enabled: false
|
301
|
+
|
302
|
+
Style/RaiseArgs:
|
303
|
+
Description: 'Checks the arguments passed to raise/fail.'
|
304
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#exception-class-messages'
|
305
|
+
Enabled: false
|
306
|
+
|
307
|
+
Style/RegexpLiteral:
|
308
|
+
Description: 'Use / or %r around regular expressions.'
|
309
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#percent-r'
|
310
|
+
Enabled: false
|
311
|
+
|
312
|
+
Style/SelfAssignment:
|
313
|
+
Description: >-
|
314
|
+
Checks for places where self-assignment shorthand should have
|
315
|
+
been used.
|
316
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#self-assignment'
|
317
|
+
Enabled: false
|
318
|
+
|
319
|
+
Style/SingleLineBlockParams:
|
320
|
+
Description: 'Enforces the names of some block params.'
|
321
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#reduce-blocks'
|
322
|
+
Enabled: false
|
323
|
+
|
324
|
+
Style/SingleLineMethods:
|
325
|
+
Description: 'Avoid single-line methods.'
|
326
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#no-single-line-methods'
|
327
|
+
Enabled: false
|
328
|
+
|
329
|
+
Style/SignalException:
|
330
|
+
Description: 'Checks for proper usage of fail and raise.'
|
331
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#fail-method'
|
332
|
+
Enabled: false
|
333
|
+
|
334
|
+
Style/SpecialGlobalVars:
|
335
|
+
Description: 'Avoid Perl-style global variables.'
|
336
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#no-cryptic-perlisms'
|
337
|
+
Enabled: false
|
338
|
+
|
339
|
+
Style/StringLiterals:
|
340
|
+
Description: 'Checks if uses of quotes match the configured preference.'
|
341
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#consistent-string-literals'
|
342
|
+
EnforcedStyle: double_quotes
|
343
|
+
Enabled: true
|
344
|
+
|
345
|
+
Style/TrailingCommaInArguments:
|
346
|
+
Description: 'Checks for trailing comma in argument lists.'
|
347
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#no-trailing-array-commas'
|
348
|
+
EnforcedStyleForMultiline: comma
|
349
|
+
SupportedStylesForMultiline:
|
350
|
+
- comma
|
351
|
+
- consistent_comma
|
352
|
+
- no_comma
|
353
|
+
Enabled: true
|
354
|
+
|
355
|
+
Style/TrailingCommaInArrayLiteral:
|
356
|
+
Description: 'Checks for trailing comma in array literals.'
|
357
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#no-trailing-array-commas'
|
358
|
+
EnforcedStyleForMultiline: comma
|
359
|
+
SupportedStylesForMultiline:
|
360
|
+
- comma
|
361
|
+
- consistent_comma
|
362
|
+
- no_comma
|
363
|
+
Enabled: true
|
364
|
+
|
365
|
+
Style/TrailingCommaInHashLiteral:
|
366
|
+
Description: 'Checks for trailing comma in hash literals.'
|
367
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#no-trailing-array-commas'
|
368
|
+
EnforcedStyleForMultiline: comma
|
369
|
+
SupportedStylesForMultiline:
|
370
|
+
- comma
|
371
|
+
- consistent_comma
|
372
|
+
- no_comma
|
373
|
+
Enabled: true
|
374
|
+
|
375
|
+
Style/TrivialAccessors:
|
376
|
+
Description: 'Prefer attr_* methods to trivial readers/writers.'
|
377
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#attr_family'
|
378
|
+
Enabled: false
|
379
|
+
|
380
|
+
Style/VariableInterpolation:
|
381
|
+
Description: >-
|
382
|
+
Don't interpolate global, instance and class variables
|
383
|
+
directly in strings.
|
384
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#curlies-interpolate'
|
385
|
+
Enabled: false
|
386
|
+
|
387
|
+
Style/WhenThen:
|
388
|
+
Description: 'Use when x then ... for one-line cases.'
|
389
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#one-line-cases'
|
390
|
+
Enabled: false
|
391
|
+
|
392
|
+
Style/WhileUntilModifier:
|
393
|
+
Description: >-
|
394
|
+
Favor modifier while/until usage when you have a
|
395
|
+
single-line body.
|
396
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#while-as-a-modifier'
|
397
|
+
Enabled: false
|
398
|
+
|
399
|
+
Style/WordArray:
|
400
|
+
Description: 'Use %w or %W for arrays of words.'
|
401
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#percent-w'
|
402
|
+
Enabled: false
|
403
|
+
|
404
|
+
# Layout
|
405
|
+
|
406
|
+
Layout/AlignParameters:
|
407
|
+
Description: 'Here we check if the parameters on a multi-line method call or definition are aligned.'
|
408
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#no-double-indent'
|
409
|
+
Enabled: false
|
410
|
+
|
411
|
+
Layout/ConditionPosition:
|
412
|
+
Description: >-
|
413
|
+
Checks for condition placed in a confusing position relative to
|
414
|
+
the keyword.
|
415
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#same-line-condition'
|
416
|
+
Enabled: false
|
417
|
+
|
418
|
+
Layout/DotPosition:
|
419
|
+
Description: 'Checks the position of the dot in multi-line method calls.'
|
420
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#consistent-multi-line-chains'
|
421
|
+
EnforcedStyle: trailing
|
422
|
+
|
423
|
+
Layout/ExtraSpacing:
|
424
|
+
Description: 'Do not use unnecessary spacing.'
|
425
|
+
Enabled: true
|
426
|
+
|
427
|
+
Layout/MultilineOperationIndentation:
|
428
|
+
Description: >-
|
429
|
+
Checks indentation of binary operations that span more than
|
430
|
+
one line.
|
431
|
+
Enabled: true
|
432
|
+
EnforcedStyle: indented
|
433
|
+
|
434
|
+
Layout/MultilineMethodCallIndentation:
|
435
|
+
Description: >-
|
436
|
+
Checks indentation of method calls with the dot operator
|
437
|
+
that span more than one line.
|
438
|
+
Enabled: true
|
439
|
+
EnforcedStyle: indented
|
440
|
+
|
441
|
+
Layout/InitialIndentation:
|
442
|
+
Description: >-
|
443
|
+
Checks the indentation of the first non-blank non-comment line in a file.
|
444
|
+
Enabled: false
|
445
|
+
|
446
|
+
# Lint
|
447
|
+
|
448
|
+
Lint/AmbiguousOperator:
|
449
|
+
Description: >-
|
450
|
+
Checks for ambiguous operators in the first argument of a
|
451
|
+
method invocation without parentheses.
|
452
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#parens-as-args'
|
453
|
+
Enabled: false
|
454
|
+
|
455
|
+
Lint/AmbiguousRegexpLiteral:
|
456
|
+
Description: >-
|
457
|
+
Checks for ambiguous regexp literals in the first argument of
|
458
|
+
a method invocation without parenthesis.
|
459
|
+
Enabled: false
|
460
|
+
|
461
|
+
Lint/AssignmentInCondition:
|
462
|
+
Description: "Don't use assignment in conditions."
|
463
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#safe-assignment-in-condition'
|
464
|
+
Enabled: false
|
465
|
+
|
466
|
+
Lint/CircularArgumentReference:
|
467
|
+
Description: "Don't refer to the keyword argument in the default value."
|
468
|
+
Enabled: false
|
469
|
+
|
470
|
+
Lint/DeprecatedClassMethods:
|
471
|
+
Description: 'Check for deprecated class method calls.'
|
472
|
+
Enabled: false
|
473
|
+
|
474
|
+
Lint/DuplicatedKey:
|
475
|
+
Description: 'Check for duplicate keys in hash literals.'
|
476
|
+
Enabled: false
|
477
|
+
|
478
|
+
Lint/EachWithObjectArgument:
|
479
|
+
Description: 'Check for immutable argument given to each_with_object.'
|
480
|
+
Enabled: false
|
481
|
+
|
482
|
+
Lint/ElseLayout:
|
483
|
+
Description: 'Check for odd code arrangement in an else block.'
|
484
|
+
Enabled: false
|
485
|
+
|
486
|
+
Lint/FormatParameterMismatch:
|
487
|
+
Description: 'The number of parameters to format/sprint must match the fields.'
|
488
|
+
Enabled: false
|
489
|
+
|
490
|
+
Lint/HandleExceptions:
|
491
|
+
Description: "Don't suppress exception."
|
492
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#dont-hide-exceptions'
|
493
|
+
Enabled: false
|
494
|
+
|
495
|
+
Lint/LiteralAsCondition:
|
496
|
+
Description: 'Checks of literals used in conditions.'
|
497
|
+
Enabled: false
|
498
|
+
|
499
|
+
Lint/LiteralInInterpolation:
|
500
|
+
Description: 'Checks for literals used in interpolation.'
|
501
|
+
Enabled: false
|
502
|
+
|
503
|
+
Lint/Loop:
|
504
|
+
Description: >-
|
505
|
+
Use Kernel#loop with break rather than begin/end/until or
|
506
|
+
begin/end/while for post-loop tests.
|
507
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#loop-with-break'
|
508
|
+
Enabled: false
|
509
|
+
|
510
|
+
Lint/NestedMethodDefinition:
|
511
|
+
Description: 'Do not use nested method definitions.'
|
512
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#no-nested-methods'
|
513
|
+
Enabled: false
|
514
|
+
|
515
|
+
Lint/NonLocalExitFromIterator:
|
516
|
+
Description: 'Do not use return in iterator to cause non-local exit.'
|
517
|
+
Enabled: false
|
518
|
+
|
519
|
+
Lint/ParenthesesAsGroupedExpression:
|
520
|
+
Description: >-
|
521
|
+
Checks for method calls with a space before the opening
|
522
|
+
parenthesis.
|
523
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#parens-no-spaces'
|
524
|
+
Enabled: false
|
525
|
+
|
526
|
+
Lint/RequireParentheses:
|
527
|
+
Description: >-
|
528
|
+
Use parentheses in the method call to avoid confusion
|
529
|
+
about precedence.
|
530
|
+
Enabled: false
|
531
|
+
|
532
|
+
Lint/UnderscorePrefixedVariableName:
|
533
|
+
Description: 'Do not use prefix `_` for a variable that is used.'
|
534
|
+
Enabled: false
|
535
|
+
|
536
|
+
Lint/UnneededCopDisableDirective:
|
537
|
+
Description: >-
|
538
|
+
Checks for rubocop:disable comments that can be removed.
|
539
|
+
Note: this cop is not disabled when disabling all cops.
|
540
|
+
It must be explicitly disabled.
|
541
|
+
Enabled: false
|
542
|
+
|
543
|
+
Lint/Void:
|
544
|
+
Description: 'Possible use of operator/literal/variable in void context.'
|
545
|
+
Enabled: false
|
546
|
+
|
547
|
+
# Performance
|
548
|
+
|
549
|
+
Performance/CaseWhenSplat:
|
550
|
+
Description: >-
|
551
|
+
Place `when` conditions that use splat at the end
|
552
|
+
of the list of `when` branches.
|
553
|
+
Enabled: false
|
554
|
+
|
555
|
+
Performance/Count:
|
556
|
+
Description: >-
|
557
|
+
Use `count` instead of `select...size`, `reject...size`,
|
558
|
+
`select...count`, `reject...count`, `select...length`,
|
559
|
+
and `reject...length`.
|
560
|
+
Enabled: false
|
561
|
+
|
562
|
+
Performance/Detect:
|
563
|
+
Description: >-
|
564
|
+
Use `detect` instead of `select.first`, `find_all.first`,
|
565
|
+
`select.last`, and `find_all.last`.
|
566
|
+
Reference: 'https://github.com/JuanitoFatas/fast-ruby#enumerabledetect-vs-enumerableselectfirst-code'
|
567
|
+
Enabled: false
|
568
|
+
|
569
|
+
Performance/FlatMap:
|
570
|
+
Description: >-
|
571
|
+
Use `Enumerable#flat_map`
|
572
|
+
instead of `Enumerable#map...Array#flatten(1)`
|
573
|
+
or `Enumberable#collect..Array#flatten(1)`
|
574
|
+
Reference: 'https://github.com/JuanitoFatas/fast-ruby#enumerablemaparrayflatten-vs-enumerableflat_map-code'
|
575
|
+
Enabled: false
|
576
|
+
|
577
|
+
Performance/ReverseEach:
|
578
|
+
Description: 'Use `reverse_each` instead of `reverse.each`.'
|
579
|
+
Reference: 'https://github.com/JuanitoFatas/fast-ruby#enumerablereverseeach-vs-enumerablereverse_each-code'
|
580
|
+
Enabled: false
|
581
|
+
|
582
|
+
Performance/Sample:
|
583
|
+
Description: >-
|
584
|
+
Use `sample` instead of `shuffle.first`,
|
585
|
+
`shuffle.last`, and `shuffle[Fixnum]`.
|
586
|
+
Reference: 'https://github.com/JuanitoFatas/fast-ruby#arrayshufflefirst-vs-arraysample-code'
|
587
|
+
Enabled: false
|
588
|
+
|
589
|
+
Performance/Size:
|
590
|
+
Description: >-
|
591
|
+
Use `size` instead of `count` for counting
|
592
|
+
the number of elements in `Array` and `Hash`.
|
593
|
+
Reference: 'https://github.com/JuanitoFatas/fast-ruby#arraycount-vs-arraysize-code'
|
594
|
+
Enabled: false
|
595
|
+
|
596
|
+
Performance/StringReplacement:
|
597
|
+
Description: >-
|
598
|
+
Use `tr` instead of `gsub` when you are replacing the same
|
599
|
+
number of characters. Use `delete` instead of `gsub` when
|
600
|
+
you are deleting characters.
|
601
|
+
Reference: 'https://github.com/JuanitoFatas/fast-ruby#stringgsub-vs-stringtr-code'
|
602
|
+
Enabled: false
|
603
|
+
|
604
|
+
# Rails
|
605
|
+
|
606
|
+
Rails/ActionFilter:
|
607
|
+
Description: 'Enforces consistent use of action filter methods.'
|
608
|
+
Enabled: false
|
609
|
+
|
610
|
+
Rails/Date:
|
611
|
+
Description: >-
|
612
|
+
Checks the correct usage of date aware methods,
|
613
|
+
such as Date.today, Date.current etc.
|
614
|
+
Enabled: false
|
615
|
+
|
616
|
+
Rails/FindBy:
|
617
|
+
Description: 'Prefer find_by over where.first.'
|
618
|
+
Enabled: false
|
619
|
+
|
620
|
+
Rails/FindEach:
|
621
|
+
Description: 'Prefer all.find_each over all.find.'
|
622
|
+
Enabled: false
|
623
|
+
|
624
|
+
Rails/HasAndBelongsToMany:
|
625
|
+
Description: 'Prefer has_many :through to has_and_belongs_to_many.'
|
626
|
+
Enabled: false
|
627
|
+
|
628
|
+
Rails/Output:
|
629
|
+
Description: 'Checks for calls to puts, print, etc.'
|
630
|
+
Enabled: false
|
631
|
+
|
632
|
+
Rails/ReadWriteAttribute:
|
633
|
+
Description: >-
|
634
|
+
Checks for read_attribute(:attr) and
|
635
|
+
write_attribute(:attr, val).
|
636
|
+
Enabled: false
|
637
|
+
|
638
|
+
Rails/ScopeArgs:
|
639
|
+
Description: 'Checks the arguments of ActiveRecord scopes.'
|
640
|
+
Enabled: false
|
641
|
+
|
642
|
+
Rails/TimeZone:
|
643
|
+
Description: 'Checks the correct usage of time zone aware methods.'
|
644
|
+
StyleGuide: 'https://github.com/bbatsov/rails-style-guide#time'
|
645
|
+
Reference: 'http://danilenko.org/2012/7/6/rails_timezones'
|
646
|
+
Enabled: false
|
647
|
+
|
648
|
+
Rails/Validation:
|
649
|
+
Description: 'Use validates :attribute, hash of validations.'
|
650
|
+
Enabled: false
|
data/.travis.yml
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
lp-serializable (0.
|
4
|
+
lp-serializable (1.0.1)
|
5
5
|
activesupport (>= 4.2)
|
6
|
-
fast_jsonapi (>= 1.
|
6
|
+
fast_jsonapi (>= 1.3)
|
7
7
|
|
8
8
|
GEM
|
9
9
|
remote: https://rubygems.org/
|
@@ -48,13 +48,13 @@ GEM
|
|
48
48
|
case_transform (0.2)
|
49
49
|
activesupport
|
50
50
|
coderay (1.1.2)
|
51
|
-
concurrent-ruby (1.
|
51
|
+
concurrent-ruby (1.1.5)
|
52
52
|
crass (1.0.4)
|
53
53
|
diff-lcs (1.3)
|
54
|
-
erubi (1.
|
55
|
-
fast_jsonapi (1.
|
54
|
+
erubi (1.8.0)
|
55
|
+
fast_jsonapi (1.5)
|
56
56
|
activesupport (>= 4.2)
|
57
|
-
i18n (1.0
|
57
|
+
i18n (1.6.0)
|
58
58
|
concurrent-ruby (~> 1.0)
|
59
59
|
jsonapi-deserializable (0.2.0)
|
60
60
|
jsonapi-rb (0.5.0)
|
@@ -65,19 +65,19 @@ GEM
|
|
65
65
|
jsonapi-renderer (~> 0.2.0)
|
66
66
|
jsonapi-serializers (1.0.1)
|
67
67
|
activesupport
|
68
|
-
loofah (2.2.
|
68
|
+
loofah (2.2.3)
|
69
69
|
crass (~> 1.0.2)
|
70
70
|
nokogiri (>= 1.5.9)
|
71
71
|
method_source (0.9.0)
|
72
|
-
mini_portile2 (2.
|
72
|
+
mini_portile2 (2.4.0)
|
73
73
|
minitest (5.11.3)
|
74
|
-
nokogiri (1.
|
75
|
-
mini_portile2 (~> 2.
|
74
|
+
nokogiri (1.10.2)
|
75
|
+
mini_portile2 (~> 2.4.0)
|
76
76
|
oj (3.6.3)
|
77
77
|
pry (0.11.3)
|
78
78
|
coderay (~> 1.1.0)
|
79
79
|
method_source (~> 0.9.0)
|
80
|
-
rack (2.0.
|
80
|
+
rack (2.0.6)
|
81
81
|
rack-test (1.0.0)
|
82
82
|
rack (>= 1.0, < 3)
|
83
83
|
rails-dom-testing (2.0.3)
|
@@ -128,4 +128,4 @@ DEPENDENCIES
|
|
128
128
|
sqlite3 (~> 1.3)
|
129
129
|
|
130
130
|
BUNDLED WITH
|
131
|
-
1.16.
|
131
|
+
1.16.3
|
data/README.md
CHANGED
@@ -24,54 +24,61 @@ Or install it yourself as:
|
|
24
24
|
|
25
25
|
## Usage
|
26
26
|
|
27
|
-
|
27
|
+
**Controller Definition**
|
28
28
|
|
29
29
|
```ruby
|
30
30
|
class ApplicationController < ActionController::Base
|
31
|
-
|
31
|
+
include Lp::Serializable
|
32
32
|
end
|
33
33
|
|
34
34
|
class MoviesController < ApplicationController
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
35
|
+
def index
|
36
|
+
movies = Movie.all
|
37
|
+
movies_hash = serializable_collection(movies, 'Movie')
|
38
|
+
render json: movies_hash
|
39
|
+
end
|
40
|
+
|
41
|
+
def show
|
42
|
+
movie = Movie.find(params[:id])
|
43
|
+
movie_hash = serializable(movie)
|
44
|
+
render json: movie_hash
|
45
|
+
end
|
46
46
|
end
|
47
47
|
```
|
48
48
|
|
49
|
-
|
49
|
+
**Serializer Definition**
|
50
50
|
|
51
51
|
```ruby
|
52
52
|
class MovieSerializer
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
53
|
+
include FastJsonapi::ObjectSerializer
|
54
|
+
|
55
|
+
attributes :name
|
56
|
+
|
57
|
+
attribute :year, if: Proc.new { |object| object.year.present? }
|
58
|
+
|
59
|
+
attribute :last_updated do |object|
|
60
|
+
object.updated_at
|
61
|
+
end
|
62
|
+
|
63
|
+
has_many :actors
|
64
|
+
belongs_to :owner
|
59
65
|
end
|
60
66
|
|
61
67
|
class ActorSerializer
|
62
|
-
|
68
|
+
include FastJsonapi::ObjectSerializer
|
63
69
|
|
64
|
-
|
70
|
+
attributes :id
|
65
71
|
end
|
66
72
|
|
67
73
|
class OwnerSerializer
|
68
|
-
|
74
|
+
include FastJsonapi::ObjectSerializer
|
69
75
|
|
70
|
-
|
76
|
+
attributes :id
|
71
77
|
end
|
72
78
|
```
|
73
79
|
|
74
|
-
|
80
|
+
## Object Serialization
|
81
|
+
**Sample Object**
|
75
82
|
|
76
83
|
```ruby
|
77
84
|
movie = Movie.new
|
@@ -83,14 +90,12 @@ movie.movie_type_id = 1
|
|
83
90
|
movie
|
84
91
|
```
|
85
92
|
|
86
|
-
|
87
|
-
|
88
|
-
#### Return a hash
|
93
|
+
**Return a hash**
|
89
94
|
```ruby
|
90
|
-
hash =
|
95
|
+
hash = serializable(movie)
|
91
96
|
```
|
92
97
|
|
93
|
-
|
98
|
+
**Output**
|
94
99
|
|
95
100
|
```json
|
96
101
|
{
|
@@ -98,7 +103,7 @@ hash = serialize_and_flatten(movie)
|
|
98
103
|
"id": "3",
|
99
104
|
"type": "movie",
|
100
105
|
"name": "test movie",
|
101
|
-
"
|
106
|
+
"last_updated": "2019-04-26 18:55:46 UTC",
|
102
107
|
"actors": [
|
103
108
|
{
|
104
109
|
"id": "1",
|
@@ -120,11 +125,69 @@ hash = serialize_and_flatten(movie)
|
|
120
125
|
|
121
126
|
For more information on configuration, refer to [fast_jsonapi](https://github.com/Netflix/fast_jsonapi#customizable-options) documentation.
|
122
127
|
|
128
|
+
## Deeply Nested Serialization Pattern
|
129
|
+
|
130
|
+
Fastjson API does not support serialization of deeply nested resources.
|
131
|
+
|
132
|
+
To get around this, extend `Lp::Serializable` in your serializers:
|
133
|
+
|
134
|
+
```ruby
|
135
|
+
class MovieSerializer
|
136
|
+
include FastJsonapi::ObjectSerializer
|
137
|
+
extend Lp::Serializable
|
138
|
+
|
139
|
+
...
|
140
|
+
end
|
141
|
+
```
|
142
|
+
|
143
|
+
Define custom attributes for relationships, instead of defining them via fastjson_api:
|
144
|
+
|
145
|
+
```ruby
|
146
|
+
class MovieSerializer
|
147
|
+
include FastJsonapi::ObjectSerializer
|
148
|
+
extend Lp::Serializable
|
149
|
+
|
150
|
+
attribute :actors do |object|
|
151
|
+
collection = object.actors
|
152
|
+
serializer = 'Actor'
|
153
|
+
serializable_collection(collection, serializer, nested: true)
|
154
|
+
end
|
155
|
+
end
|
156
|
+
```
|
157
|
+
|
158
|
+
Attribute `:actors` will trigger `ActorSerializer` to serialize the actors collection. Consequently, any relationships defined in `ActorSerializer` via custom attributes and serialized with `serializable_` methods (using the `nested: true` option) will be appropriately nested.
|
159
|
+
|
160
|
+
## Custom Serializer Class
|
161
|
+
|
162
|
+
Use `#serializable_class` to serialize with a custom class:
|
163
|
+
|
164
|
+
```ruby
|
165
|
+
def show
|
166
|
+
movie = Movie.find(params[:id])
|
167
|
+
# Will serialize with FilmSerializer instead of MovieSerializer
|
168
|
+
movie_hash = serializable_class(movie, 'Film')
|
169
|
+
render json: movie_hash
|
170
|
+
end
|
171
|
+
```
|
172
|
+
|
123
173
|
## Options Support
|
124
174
|
|
125
|
-
|
175
|
+
Supported options include:
|
176
|
+
|
177
|
+
- `:fields` ([Sparse Fieldsets](https://github.com/Netflix/fast_jsonapi#sparse-fieldsets))
|
178
|
+
- `:params` ([Params](https://github.com/Netflix/fast_jsonapi#params))
|
179
|
+
- [Conditional Attributes](https://github.com/Netflix/fast_jsonapi#conditional-attributes)
|
180
|
+
|
181
|
+
Other options are "supported" but may yeild unexpected results, as Serializable's hash flattening prioritizes deeply nested data structures.
|
182
|
+
|
183
|
+
`:is_collection` is baked into Seriazable methods for accurate detection of collections or singular resources.
|
184
|
+
|
185
|
+
## Aliases
|
186
|
+
|
187
|
+
- `serialize_and_flatten()` = `serializable()`
|
188
|
+
- `serialize_and_flatten_with_class_name()` = `serializable_class()`
|
189
|
+
- `serialize_and_flatten_collection()` = `serializable_collection()`
|
126
190
|
|
127
|
-
This affects serializing with [params](https://github.com/Netflix/fast_jsonapi#params), [compound documents](https://github.com/Netflix/fast_jsonapi#compound-document), metadata, and links.
|
128
191
|
|
129
192
|
## Development
|
130
193
|
|
@@ -134,7 +197,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
134
197
|
|
135
198
|
## Contributing
|
136
199
|
|
137
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
200
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/LaunchPadLab/lp-serializable. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
138
201
|
|
139
202
|
## License
|
140
203
|
|
@@ -142,4 +205,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
|
|
142
205
|
|
143
206
|
## Code of Conduct
|
144
207
|
|
145
|
-
Everyone interacting in the Lp::Serializable project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/
|
208
|
+
Everyone interacting in the Lp::Serializable project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/LaunchPadLab/lp-serializable/blob/master/CODE_OF_CONDUCT.md).
|
@@ -4,6 +4,7 @@ require 'active_support/core_ext/object'
|
|
4
4
|
require 'active_support/concern'
|
5
5
|
require 'active_support/inflector'
|
6
6
|
require 'fast_jsonapi/serialization_core'
|
7
|
+
require 'fast_jsonapi/attribute'
|
7
8
|
|
8
9
|
module FastJsonapi
|
9
10
|
module ObjectSerializer
|
@@ -149,11 +150,17 @@ module FastJsonapi
|
|
149
150
|
|
150
151
|
def attributes(*attributes_list, &block)
|
151
152
|
attributes_list = attributes_list.first if attributes_list.first.class.is_a?(Array)
|
153
|
+
options = attributes_list.last.is_a?(Hash) ? attributes_list.pop : {}
|
152
154
|
self.attributes_to_serialize = {} if self.attributes_to_serialize.nil?
|
155
|
+
|
153
156
|
attributes_list.each do |attr_name|
|
154
157
|
method_name = attr_name
|
155
158
|
key = run_key_transform(method_name)
|
156
|
-
attributes_to_serialize[key] =
|
159
|
+
attributes_to_serialize[key] = Attribute.new(
|
160
|
+
key: key,
|
161
|
+
method: block || method_name,
|
162
|
+
options: options
|
163
|
+
)
|
157
164
|
end
|
158
165
|
end
|
159
166
|
|
@@ -73,12 +73,9 @@ module FastJsonapi
|
|
73
73
|
end
|
74
74
|
|
75
75
|
def attributes_hash(record, params = {})
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
else
|
80
|
-
record.public_send(method)
|
81
|
-
end
|
76
|
+
attributes = attributes_to_serialize
|
77
|
+
attributes.each_with_object({}) do |(_k, attribute), hash|
|
78
|
+
attribute.serialize(record, params, hash)
|
82
79
|
end
|
83
80
|
end
|
84
81
|
|
data/lib/lp/serializable.rb
CHANGED
@@ -1,33 +1,45 @@
|
|
1
1
|
require "lp/serializable/strategies"
|
2
|
+
require "lp/serializable/exceptions"
|
2
3
|
|
3
4
|
module Lp
|
4
5
|
module Serializable
|
5
6
|
include Strategies
|
7
|
+
include Exceptions
|
6
8
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
end
|
12
|
-
|
13
|
-
def serialize_and_flatten(resource, options={})
|
14
|
-
base_hash = serialize_hash(resource)
|
9
|
+
def serialize_and_flatten(resource, options = {})
|
10
|
+
return { :data => nil } if resource.nil? # Match the return of serializable_class when given nil
|
11
|
+
collection_option = collection?(false)
|
12
|
+
base_hash = serialize_hash(resource, options.merge(collection_option))
|
15
13
|
flatten_and_nest_data(base_hash, set_nested_option(options))
|
16
14
|
end
|
17
15
|
|
18
|
-
def serialize_and_flatten_with_class_name(
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
16
|
+
def serialize_and_flatten_with_class_name(
|
17
|
+
resource,
|
18
|
+
class_name,
|
19
|
+
options = {}
|
20
|
+
)
|
21
|
+
raise UnserializableCollection if resource.is_a?(Array)
|
22
|
+
collection_option = collection?(false)
|
23
|
+
base_hash = serializable_hash_with_class_name(
|
24
|
+
resource,
|
25
|
+
class_name,
|
26
|
+
options.merge(collection_option),
|
27
|
+
)
|
23
28
|
flatten_and_nest_data(base_hash, set_nested_option(options))
|
24
29
|
end
|
25
30
|
|
26
|
-
def serialize_and_flatten_collection(resource, class_name, options={})
|
27
|
-
|
28
|
-
|
29
|
-
|
31
|
+
def serialize_and_flatten_collection(resource, class_name, options = {})
|
32
|
+
collection_option = collection?(true)
|
33
|
+
base_hash = serializable_hash_with_class_name(
|
34
|
+
resource,
|
35
|
+
class_name,
|
36
|
+
options.merge(collection_option),
|
37
|
+
)
|
30
38
|
flatten_array_and_nest_data(base_hash, set_nested_option(options))
|
31
39
|
end
|
40
|
+
|
41
|
+
alias_method :serializable, :serialize_and_flatten
|
42
|
+
alias_method :serializable_class, :serialize_and_flatten_with_class_name
|
43
|
+
alias_method :serializable_collection, :serialize_and_flatten_collection
|
32
44
|
end
|
33
|
-
end
|
45
|
+
end
|
@@ -9,14 +9,14 @@ module Lp
|
|
9
9
|
|
10
10
|
private
|
11
11
|
|
12
|
-
def serialize_hash(resource, options={})
|
13
|
-
"#{resource.class.name}Serializer"
|
14
|
-
|
12
|
+
def serialize_hash(resource, options = {})
|
13
|
+
"#{resource.class.name}Serializer".
|
14
|
+
constantize.new(resource, options).serializable_hash
|
15
15
|
end
|
16
16
|
|
17
|
-
def serializable_hash_with_class_name(resource, class_name, options={})
|
18
|
-
"#{class_name}Serializer"
|
19
|
-
|
17
|
+
def serializable_hash_with_class_name(resource, class_name, options = {})
|
18
|
+
"#{class_name}Serializer".
|
19
|
+
constantize.new(resource, options).serializable_hash
|
20
20
|
end
|
21
21
|
|
22
22
|
def flatten_and_nest_data(hash, nested)
|
@@ -26,10 +26,14 @@ module Lp
|
|
26
26
|
def flatten_array_and_nest_data(hash, nested)
|
27
27
|
nest_data?(flatten_array_of_hashes(expose_data(hash)), nested)
|
28
28
|
end
|
29
|
-
|
29
|
+
|
30
|
+
def collection?(boolean)
|
31
|
+
{ is_collection: boolean }
|
32
|
+
end
|
33
|
+
|
30
34
|
def set_nested_option(options)
|
31
35
|
options.fetch(:nested, false)
|
32
36
|
end
|
33
37
|
end
|
34
38
|
end
|
35
|
-
end
|
39
|
+
end
|
@@ -3,7 +3,7 @@ module Lp
|
|
3
3
|
module Utilities
|
4
4
|
private
|
5
5
|
|
6
|
-
REDUNDANT_KEYS = %i
|
6
|
+
REDUNDANT_KEYS = %i[attributes relationships].freeze
|
7
7
|
|
8
8
|
def nest_data?(resource, nested)
|
9
9
|
if nested
|
@@ -35,27 +35,31 @@ module Lp
|
|
35
35
|
|
36
36
|
def flatten_hash(hash)
|
37
37
|
return unless hash
|
38
|
-
hash.each_with_object({}) do |(
|
39
|
-
if hash_and_matches_redundant_keys?(
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
# NOTE: extract this into a different method?
|
44
|
-
elsif hash_and_has_data_key?(v)
|
45
|
-
h[k] = expose_data(v)
|
38
|
+
hash.each_with_object({}) do |(key, value), h|
|
39
|
+
if hash_and_matches_redundant_keys?(key, value)
|
40
|
+
flatten_hash_map(value, h)
|
41
|
+
elsif hash_and_has_data_key?(value)
|
42
|
+
h[key] = expose_data(value)
|
46
43
|
else
|
47
|
-
h[
|
44
|
+
h[key] = value
|
48
45
|
end
|
49
46
|
end
|
50
47
|
end
|
51
48
|
|
49
|
+
def flatten_hash_map(value, hash)
|
50
|
+
flatten_hash(value).map do |h_k, h_v|
|
51
|
+
hash[h_k.to_s.to_sym] = h_v
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
52
55
|
def hash_and_has_data_key?(value)
|
53
56
|
value.is_a?(Hash) && value.key?(:data)
|
54
57
|
end
|
55
58
|
|
56
|
-
|
57
|
-
|
59
|
+
# NOTE Supports native relationship references in serializer
|
60
|
+
def hash_and_matches_redundant_keys?(key, value)
|
61
|
+
value.is_a?(Hash) && REDUNDANT_KEYS.any? { |sym| sym == key }
|
58
62
|
end
|
59
63
|
end
|
60
64
|
end
|
61
|
-
end
|
65
|
+
end
|
data/lp-serializable.gemspec
CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.authors = ["mrjonesbot"]
|
10
10
|
spec.email = ["nate@mrjones.io"]
|
11
11
|
|
12
|
-
spec.summary = "Serialize with Fast JSON API, flatten like
|
12
|
+
spec.summary = "Serialize with Fast JSON API, flatten like AMS"
|
13
13
|
spec.description = "JSON API(jsonapi.org) serializer wrapper methods that work with Rails and can be used to serialize any kind of ruby object with AMS style output."
|
14
14
|
spec.homepage = "https://www.github.com/launchpadlab/lp-serializable"
|
15
15
|
spec.license = "MIT"
|
@@ -45,5 +45,5 @@ Gem::Specification.new do |spec|
|
|
45
45
|
spec.add_runtime_dependency(%q<activesupport>, [">= 4.2"])
|
46
46
|
spec.add_development_dependency(%q<byebug>, [">= 0"])
|
47
47
|
|
48
|
-
spec.add_dependency "fast_jsonapi", '>= 1.
|
49
|
-
end
|
48
|
+
spec.add_dependency "fast_jsonapi", '>= 1.3'
|
49
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lp-serializable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- mrjonesbot
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-11-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: appraisal
|
@@ -212,14 +212,14 @@ dependencies:
|
|
212
212
|
requirements:
|
213
213
|
- - ">="
|
214
214
|
- !ruby/object:Gem::Version
|
215
|
-
version: '1.
|
215
|
+
version: '1.3'
|
216
216
|
type: :runtime
|
217
217
|
prerelease: false
|
218
218
|
version_requirements: !ruby/object:Gem::Requirement
|
219
219
|
requirements:
|
220
220
|
- - ">="
|
221
221
|
- !ruby/object:Gem::Version
|
222
|
-
version: '1.
|
222
|
+
version: '1.3'
|
223
223
|
description: JSON API(jsonapi.org) serializer wrapper methods that work with Rails
|
224
224
|
and can be used to serialize any kind of ruby object with AMS style output.
|
225
225
|
email:
|
@@ -230,6 +230,7 @@ extra_rdoc_files: []
|
|
230
230
|
files:
|
231
231
|
- ".gitignore"
|
232
232
|
- ".rspec"
|
233
|
+
- ".rubocop.yml"
|
233
234
|
- ".ruby-version"
|
234
235
|
- ".travis.yml"
|
235
236
|
- CODE_OF_CONDUCT.md
|
@@ -244,6 +245,7 @@ files:
|
|
244
245
|
- lib/fast_jsonapi/object_serializer.rb
|
245
246
|
- lib/fast_jsonapi/serialization_core.rb
|
246
247
|
- lib/lp/serializable.rb
|
248
|
+
- lib/lp/serializable/exceptions.rb
|
247
249
|
- lib/lp/serializable/strategies.rb
|
248
250
|
- lib/lp/serializable/utilities.rb
|
249
251
|
- lib/lp/serializable/version.rb
|
@@ -271,5 +273,5 @@ rubyforge_project:
|
|
271
273
|
rubygems_version: 2.7.3
|
272
274
|
signing_key:
|
273
275
|
specification_version: 4
|
274
|
-
summary: Serialize with Fast JSON API, flatten like
|
276
|
+
summary: Serialize with Fast JSON API, flatten like AMS
|
275
277
|
test_files: []
|