smart_params 2.0.2 → 2.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ec98add977831a04f50ff7b0ca25cd092d22587c79c01ed7da7f44e97811cae1
4
- data.tar.gz: d5ee94308ecebd7021e50fcde8692365814c512c59591a0ac359aa30641d5c4c
3
+ metadata.gz: 43b4b84cd337c3737f9f83d70a8e64d3a95e0e08a4106eca29bd0c00cceedcf9
4
+ data.tar.gz: 6e303c416ae81b6e933a4d7a3fb4e497746c1968f4730e317c8cab253c62805e
5
5
  SHA512:
6
- metadata.gz: 9b0e8197a145e3a788081b23af7cc491e0f8dc92b9297d1da04cc38a7a285164c061c0a2dfe2035e5b3375ae93561e9cfa5b4eddc44ecbc0de7118bc2ac2b583
7
- data.tar.gz: 6a0586b150170cdaa07ecca164a815cf8863ad6bd4afbf236b586a40c548102320b4b64d25a356cb5079ad6a1fdd681bae4704ffbe690a567c59805c0c589320
6
+ metadata.gz: bb603816b69b989284b538ab7fe95e82418652f3560b0100071329a3f0e1fe6013a7e3318d2c9432847905c5600dae5aaed52a9dfef55d7dbf27e98b4746e122
7
+ data.tar.gz: 2cdfa446d1c492e786087390d1220cac38b5f6f2fee75e234427e9b6c8e58d25f2a590b12c3af4d3b7fd027cd4610bde849f3da0da6f2e29bc034da956f25cfc
data/lib/smart_params.rb CHANGED
@@ -86,7 +86,7 @@ module SmartParams
86
86
 
87
87
  class_methods do
88
88
  def schema(type:, &subfield)
89
- @schema = Field.new(keychain: [], type: type, &subfield)
89
+ @schema = Field.new(keychain: [], type: type, root: true, &subfield)
90
90
  end
91
91
  end
92
92
  end
@@ -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
- if keychain.empty?
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
@@ -1,3 +1,3 @@
1
1
  module SmartParams
2
- VERSION = "2.0.2"
2
+ VERSION = "2.0.3"
3
3
  end
@@ -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
  {
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smart_params
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.2
4
+ version: 2.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kurtis Rainbolt-Greene