smart_params 2.2.0 → 2.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/smart_params.rb +19 -21
- data/lib/smart_params/version.rb +1 -1
- data/lib/smart_params_spec.rb +170 -14
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fefed41179430e01ed431251aa5b42a279d21d9e8789500f39c7de54092d72a9
|
4
|
+
data.tar.gz: d247325e2b0b1dacdcd6f9680d6fcf8629d81bc1b7416d699efdc5141b5c0660
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 384fe2938d4cf4fe73a45c69620d5d5017a5c38358fcb8bd14d617693231571e8bee7dbd19ce6091d3fa3a547199f21db781c7ed9b7633b44c0bbfc5a80a5099
|
7
|
+
data.tar.gz: ae916f2a969e10be1fc7cbdfa116a1fb89c3a5b3970fdd6771297b1d7ea56222fa9c9414625bc658d6b50b27d5e10b0ced5af328524455fe5bf5e8ae7c3430cd
|
data/lib/smart_params.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
-
require "ostruct"
|
2
1
|
require "dry-types"
|
3
2
|
require "recursive-open-struct"
|
4
3
|
require "active_support/concern"
|
5
4
|
require "active_support/core_ext/object"
|
5
|
+
require "active_support/core_ext/module/delegation"
|
6
6
|
|
7
7
|
module SmartParams
|
8
8
|
extend ActiveSupport::Concern
|
@@ -20,15 +20,11 @@ module SmartParams
|
|
20
20
|
@safe = safe
|
21
21
|
@raw = raw
|
22
22
|
@schema = self.class.instance_variable_get(:@schema)
|
23
|
-
@fields = [@schema, *unfold(@schema.subfields)]
|
24
|
-
.sort_by(&:weight)
|
25
|
-
.each { |field| field.claim(raw) }
|
23
|
+
@fields = [@schema, *unfold(@schema.subfields)].sort_by(&:weight).each { |field| field.claim(raw) }
|
26
24
|
rescue SmartParams::Error::InvalidPropertyType => invalid_property_type_exception
|
27
|
-
if safe?
|
28
|
-
|
29
|
-
|
30
|
-
@exception = invalid_property_type_exception
|
31
|
-
end
|
25
|
+
raise invalid_property_type_exception if safe?
|
26
|
+
|
27
|
+
@exception = invalid_property_type_exception
|
32
28
|
end
|
33
29
|
|
34
30
|
def payload
|
@@ -39,25 +35,27 @@ module SmartParams
|
|
39
35
|
end
|
40
36
|
end
|
41
37
|
|
42
|
-
def
|
38
|
+
def to_hash(options = nil)
|
43
39
|
if @exception.present?
|
44
40
|
@exception.as_json(options)
|
45
41
|
else
|
46
42
|
structure.as_json(options)
|
47
43
|
end
|
48
44
|
end
|
45
|
+
alias_method :as_json, :to_hash
|
49
46
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
47
|
+
delegate :[], to: :to_hash
|
48
|
+
delegate :fetch, to: :to_hash
|
49
|
+
delegate :fetch_values, to: :to_hash
|
50
|
+
delegate :merge, to: :to_hash
|
51
|
+
delegate :keys, to: :to_hash
|
52
|
+
delegate :key?, to: :to_hash
|
53
|
+
delegate :has_key?, to: :to_hash
|
54
|
+
delegate :values, to: :to_hash
|
55
|
+
delegate :value?, to: :to_hash
|
56
|
+
delegate :has_value?, to: :to_hash
|
57
|
+
delegate :dig, to: :to_hash
|
58
|
+
delegate :to_s, to: :to_hash
|
61
59
|
|
62
60
|
def method_missing(name, *arguments, &block)
|
63
61
|
if payload.respond_to?(name)
|
data/lib/smart_params/version.rb
CHANGED
data/lib/smart_params_spec.rb
CHANGED
@@ -34,6 +34,8 @@ RSpec.describe SmartParams do
|
|
34
34
|
end
|
35
35
|
|
36
36
|
describe "#payload" do
|
37
|
+
subject {schema.payload}
|
38
|
+
|
37
39
|
context "with a reasonably good params" do
|
38
40
|
let(:params) do
|
39
41
|
{
|
@@ -60,27 +62,25 @@ RSpec.describe SmartParams do
|
|
60
62
|
}
|
61
63
|
end
|
62
64
|
|
63
|
-
it "
|
64
|
-
expect(
|
65
|
+
it "has a chain path data.type" do
|
66
|
+
expect(subject.data.type).to eq("accounts")
|
65
67
|
end
|
66
68
|
|
67
|
-
it "
|
68
|
-
expect(
|
69
|
+
it "has a chain path data.attributes.email" do
|
70
|
+
expect(subject.data.attributes.email).to eq("kurtis@example.com")
|
69
71
|
end
|
70
72
|
|
71
|
-
it "
|
72
|
-
expect(
|
73
|
+
it "has a chain path data.attributes.password" do
|
74
|
+
expect(subject.data.attributes.password).to be_kind_of(String)
|
73
75
|
end
|
74
76
|
|
75
|
-
it "
|
76
|
-
expect(
|
77
|
+
it "has a chain path meta.jsonapi_version" do
|
78
|
+
expect(subject.meta.jsonapi_version).to eq("1.0")
|
77
79
|
end
|
78
80
|
end
|
79
81
|
end
|
80
82
|
|
81
|
-
|
82
|
-
subject {schema.as_json}
|
83
|
-
|
83
|
+
shared_examples "native types" do
|
84
84
|
context "with extra params" do
|
85
85
|
let(:params) do
|
86
86
|
{
|
@@ -95,7 +95,7 @@ RSpec.describe SmartParams do
|
|
95
95
|
}
|
96
96
|
end
|
97
97
|
|
98
|
-
it "returns as
|
98
|
+
it "returns as native data types" do
|
99
99
|
expect(
|
100
100
|
subject
|
101
101
|
).to match(
|
@@ -128,7 +128,7 @@ RSpec.describe SmartParams do
|
|
128
128
|
}
|
129
129
|
end
|
130
130
|
|
131
|
-
it "returns as
|
131
|
+
it "returns as native data types" do
|
132
132
|
expect(
|
133
133
|
subject
|
134
134
|
).to match(
|
@@ -175,7 +175,7 @@ RSpec.describe SmartParams do
|
|
175
175
|
}
|
176
176
|
end
|
177
177
|
|
178
|
-
it "returns as
|
178
|
+
it "returns as native data types" do
|
179
179
|
expect(
|
180
180
|
subject
|
181
181
|
).to match(
|
@@ -212,4 +212,160 @@ RSpec.describe SmartParams do
|
|
212
212
|
end
|
213
213
|
end
|
214
214
|
end
|
215
|
+
|
216
|
+
describe "#to_hash" do
|
217
|
+
subject {schema.to_hash}
|
218
|
+
|
219
|
+
include_examples "native types"
|
220
|
+
end
|
221
|
+
|
222
|
+
describe "#as_json" do
|
223
|
+
subject {schema.as_json}
|
224
|
+
|
225
|
+
include_examples "native types"
|
226
|
+
end
|
227
|
+
|
228
|
+
describe "#fetch" do
|
229
|
+
subject {schema.fetch("data")}
|
230
|
+
|
231
|
+
context "with a reasonably good params" do
|
232
|
+
let(:params) do
|
233
|
+
{
|
234
|
+
data: {
|
235
|
+
type: "accounts",
|
236
|
+
attributes: {
|
237
|
+
email: "kurtis@example.com"
|
238
|
+
}
|
239
|
+
},
|
240
|
+
meta: {
|
241
|
+
jsonapi_version: "1.0"
|
242
|
+
},
|
243
|
+
included: [
|
244
|
+
{
|
245
|
+
data: {
|
246
|
+
id: "a",
|
247
|
+
type: "widget",
|
248
|
+
attributes: {
|
249
|
+
title: "Widget A",
|
250
|
+
}
|
251
|
+
}
|
252
|
+
}
|
253
|
+
]
|
254
|
+
}
|
255
|
+
end
|
256
|
+
|
257
|
+
it "returns the native type" do
|
258
|
+
expect(
|
259
|
+
subject
|
260
|
+
).to match(
|
261
|
+
hash_including(
|
262
|
+
{
|
263
|
+
"type" => "accounts",
|
264
|
+
"attributes" => hash_including(
|
265
|
+
{
|
266
|
+
"email" => "kurtis@example.com",
|
267
|
+
"password" => an_instance_of(String)
|
268
|
+
}
|
269
|
+
)
|
270
|
+
}
|
271
|
+
)
|
272
|
+
)
|
273
|
+
end
|
274
|
+
end
|
275
|
+
end
|
276
|
+
|
277
|
+
describe "#dig" do
|
278
|
+
subject {schema.dig("data", "attributes", "email")}
|
279
|
+
|
280
|
+
context "with a reasonably good params" do
|
281
|
+
let(:params) do
|
282
|
+
{
|
283
|
+
data: {
|
284
|
+
type: "accounts",
|
285
|
+
attributes: {
|
286
|
+
email: "kurtis@example.com"
|
287
|
+
}
|
288
|
+
},
|
289
|
+
meta: {
|
290
|
+
jsonapi_version: "1.0"
|
291
|
+
},
|
292
|
+
included: [
|
293
|
+
{
|
294
|
+
data: {
|
295
|
+
id: "a",
|
296
|
+
type: "widget",
|
297
|
+
attributes: {
|
298
|
+
title: "Widget A",
|
299
|
+
}
|
300
|
+
}
|
301
|
+
}
|
302
|
+
]
|
303
|
+
}
|
304
|
+
end
|
305
|
+
|
306
|
+
it "returns the native type" do
|
307
|
+
expect(
|
308
|
+
subject
|
309
|
+
).to eq(
|
310
|
+
"kurtis@example.com"
|
311
|
+
)
|
312
|
+
end
|
313
|
+
end
|
314
|
+
end
|
315
|
+
|
316
|
+
describe "#fetch_values" do
|
317
|
+
subject {schema.fetch_values("data", "meta")}
|
318
|
+
|
319
|
+
context "with a reasonably good params" do
|
320
|
+
let(:params) do
|
321
|
+
{
|
322
|
+
data: {
|
323
|
+
type: "accounts",
|
324
|
+
attributes: {
|
325
|
+
email: "kurtis@example.com"
|
326
|
+
}
|
327
|
+
},
|
328
|
+
meta: {
|
329
|
+
jsonapi_version: "1.0"
|
330
|
+
},
|
331
|
+
included: [
|
332
|
+
{
|
333
|
+
data: {
|
334
|
+
id: "a",
|
335
|
+
type: "widget",
|
336
|
+
attributes: {
|
337
|
+
title: "Widget A",
|
338
|
+
}
|
339
|
+
}
|
340
|
+
}
|
341
|
+
]
|
342
|
+
}
|
343
|
+
end
|
344
|
+
|
345
|
+
it "returns the native type" do
|
346
|
+
expect(
|
347
|
+
subject
|
348
|
+
).to match(
|
349
|
+
[
|
350
|
+
hash_including(
|
351
|
+
{
|
352
|
+
"type" => "accounts",
|
353
|
+
"attributes" => hash_including(
|
354
|
+
{
|
355
|
+
"email" => "kurtis@example.com",
|
356
|
+
"password" => an_instance_of(String)
|
357
|
+
}
|
358
|
+
)
|
359
|
+
}
|
360
|
+
),
|
361
|
+
hash_including(
|
362
|
+
{
|
363
|
+
"jsonapi_version" => "1.0"
|
364
|
+
}
|
365
|
+
)
|
366
|
+
]
|
367
|
+
)
|
368
|
+
end
|
369
|
+
end
|
370
|
+
end
|
215
371
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: smart_params
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kurtis Rainbolt-Greene
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-08-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|