smart_params 2.0.2 → 2.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 +4 -4
- data/lib/smart_params.rb +1 -1
- data/lib/smart_params/field.rb +8 -5
- data/lib/smart_params/version.rb +1 -1
- data/lib/smart_params_spec.rb +33 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 43b4b84cd337c3737f9f83d70a8e64d3a95e0e08a4106eca29bd0c00cceedcf9
|
4
|
+
data.tar.gz: 6e303c416ae81b6e933a4d7a3fb4e497746c1968f4730e317c8cab253c62805e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bb603816b69b989284b538ab7fe95e82418652f3560b0100071329a3f0e1fe6013a7e3318d2c9432847905c5600dae5aaed52a9dfef55d7dbf27e98b4746e122
|
7
|
+
data.tar.gz: 2cdfa446d1c492e786087390d1220cac38b5f6f2fee75e234427e9b6c8e58d25f2a590b12c3af4d3b7fd027cd4610bde849f3da0da6f2e29bc034da956f25cfc
|
data/lib/smart_params.rb
CHANGED
data/lib/smart_params/field.rb
CHANGED
@@ -7,7 +7,8 @@ module SmartParams
|
|
7
7
|
attr_reader :subfields
|
8
8
|
attr_reader :type
|
9
9
|
|
10
|
-
def initialize(keychain:, type:, &nesting)
|
10
|
+
def initialize(keychain:, type:, root: false, &nesting)
|
11
|
+
@root = root
|
11
12
|
@keychain = Array(keychain)
|
12
13
|
@subfields = Set.new
|
13
14
|
@type = type
|
@@ -26,10 +27,8 @@ module SmartParams
|
|
26
27
|
end
|
27
28
|
|
28
29
|
def claim(raw)
|
29
|
-
|
30
|
-
@value = type[raw]
|
31
|
-
else
|
32
|
-
@value = type[raw.dig(*keychain)]
|
30
|
+
unless root?
|
31
|
+
@value = type[if keychain.empty? then raw else raw.dig(*keychain) end]
|
33
32
|
end
|
34
33
|
rescue Dry::Types::ConstraintError => bad_type_exception
|
35
34
|
raise SmartParams::Error::InvalidPropertyType, keychain: keychain, wanted: type, raw: if keychain.empty? then raw else raw.dig(*keychain) end
|
@@ -50,6 +49,10 @@ module SmartParams
|
|
50
49
|
value.nil?
|
51
50
|
end
|
52
51
|
|
52
|
+
def root?
|
53
|
+
@root
|
54
|
+
end
|
55
|
+
|
53
56
|
def weight
|
54
57
|
keychain.length
|
55
58
|
end
|
data/lib/smart_params/version.rb
CHANGED
data/lib/smart_params_spec.rb
CHANGED
@@ -81,6 +81,39 @@ RSpec.describe SmartParams do
|
|
81
81
|
describe "#as_json" do
|
82
82
|
subject {schema.as_json}
|
83
83
|
|
84
|
+
context "with extra params" do
|
85
|
+
let(:params) do
|
86
|
+
{
|
87
|
+
data: {
|
88
|
+
type: "accounts",
|
89
|
+
attributes: {
|
90
|
+
email: "kurtis@example.com"
|
91
|
+
}
|
92
|
+
},
|
93
|
+
x: "y"
|
94
|
+
}
|
95
|
+
end
|
96
|
+
|
97
|
+
it "returns as json" do
|
98
|
+
expect(
|
99
|
+
subject
|
100
|
+
).to eq(
|
101
|
+
{
|
102
|
+
"data" => hash_including(
|
103
|
+
{
|
104
|
+
"type" => "accounts",
|
105
|
+
"attributes" => hash_including(
|
106
|
+
{
|
107
|
+
"email" => "kurtis@example.com",
|
108
|
+
}
|
109
|
+
)
|
110
|
+
}
|
111
|
+
),
|
112
|
+
}
|
113
|
+
)
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
84
117
|
context "with a reasonably good params" do
|
85
118
|
let(:params) do
|
86
119
|
{
|