simple_params 1.5.1 → 1.5.2

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 CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- NjI2NzEzZDRlYTRkMjRmMDY2ZjJjYmMxNzBjMWJlNDhkZTQzYzExNg==
4
+ NDE2OTkyZTFiMjYzYTQ1NmVlNzI4OWUyNzM3MGMyZjEyNzRjOGUyNg==
5
5
  data.tar.gz: !binary |-
6
- YjQ5OTg0NmVjNWFkNjAzNTA1ODI1ZDMzMDkwMjg4YjZmNjM3NmVhYg==
6
+ NTMxMGU3ZWE5NTY4ZmI2OGMyZDk0ZjZiOTIxNGJmMmI0NjgxNjRhMw==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- YzNhMzcxZGM2MTM3MTkxOWViODM1Mjk2YWUyYWEzMzViYmY0MDE4NTFkNTRk
10
- MWU5NDNhY2I5NWQyMDNiYTcwM2Y4NThjYTVhZjYwN2Y2YzNlNGQ2ZmRmOGJh
11
- NjIyODVhYmJkMjBiZDUyYjFhOTAyZTYzZWQyYjQ3OTg3MmQ0MWY=
9
+ OTJmMzRhNTFlOWI0YjAwZjU1N2E1NDhjYTkzZWJjZTczMWMxODQyOWRkNmYw
10
+ NjRhZjU2OTNlNDI4MzAwZGM0ZTJiMTNhMWEyMDEzODE0MzBkZWMxZmE0YTA0
11
+ ZjI0MmJhNGY1MWZkZGE2NTQwMDZkMWZkNGQyNGE1NzE3YTdhZTY=
12
12
  data.tar.gz: !binary |-
13
- ZDhjM2Y4NDhlZTQyODY5NTFjY2RhNTY2YWY1MGIzMGM0ZDNlM2VjYTg0OGI4
14
- YmUzYTI5MDU2NTE2ZGY2MThkYWY2NjI1MjNkZjM3ZTFkNDUwNDVmYzkwNjQy
15
- ZTk3NTQxZDZjMmZiMDZlZDE3OWFjNDAzNDI5OTc0MzljZWEzOWY=
13
+ ZGZkZmI0NmI4NzY3MWJhYWQwNjRiZDk1NDBkOWNjNDk2ODc3OTFmYzQwYzIz
14
+ N2JmNDk5MTZkYzZiNDE5MjI2ZDJhYTc5ZTFlODk3MmJjMjllZTA5NzBmNDYy
15
+ NzY2NTJhNWMzZTc5N2FlMzRiM2YxMWRlNjUwZjkxM2U0OTJjOTk=
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- simple_params (1.5.0)
4
+ simple_params (1.5.1)
5
5
  activemodel (>= 3.0, < 5.0)
6
6
  shoulda-matchers (~> 2.8)
7
7
  virtus (>= 1.0.0)
@@ -61,6 +61,7 @@ module SimpleParams
61
61
  send("#{name}=", value)
62
62
  end
63
63
 
64
+ # TODO: This needs more testing
64
65
  singular_key = singularized_key(name)
65
66
  define_method("build_#{singular_key}") do |value={}|
66
67
  klass.new(value, self, name)
@@ -75,12 +75,12 @@ module SimpleParams
75
75
  else
76
76
  # This logic basically sets the nested class to an instance of itself, unless
77
77
  # it is optional.
78
- init = if opts[:optional]
79
- nil
78
+ init_value = if opts[:optional]
79
+ klass.hash? ? nil : []
80
80
  else
81
81
  klass_instance = klass.new({}, self, name)
82
+ klass.hash? ? klass_instance : [klass_instance]
82
83
  end
83
- init_value = klass.hash? ? init : [init]
84
84
  instance_variable_set("@#{name}", init_value)
85
85
  end
86
86
  end
@@ -1,3 +1,3 @@
1
1
  module SimpleParams
2
- VERSION = "1.5.1"
2
+ VERSION = "1.5.2"
3
3
  end
@@ -1,6 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  class AcceptanceParams < SimpleParams::Params
4
+ with_rails_helpers
4
5
  allow_undefined_params
5
6
  param :reference, type: :object, optional: true
6
7
  param :name
@@ -24,18 +25,15 @@ class AcceptanceParams < SimpleParams::Params
24
25
  end
25
26
 
26
27
  nested_array :dogs do
27
- with_rails_helpers
28
28
  param :name
29
29
  param :age, type: :integer, validations: { inclusion: { in: 1..20 } }
30
30
  end
31
31
 
32
32
  nested_array :cats, with_ids: true do
33
- with_rails_helpers
34
33
  param :name
35
34
  end
36
35
 
37
36
  nested_array :birds, optional: true, with_ids: true do
38
- with_rails_helpers
39
37
  param :name
40
38
  end
41
39
 
@@ -73,6 +71,13 @@ describe SimpleParams::Params do
73
71
  end
74
72
  end
75
73
 
74
+ describe "rails_helpers", rails_helpers: true do
75
+ it "can build optional class" do
76
+ klass = AcceptanceParams.new.build_phone
77
+ klass.should be_a(AcceptanceParams::Phone)
78
+ end
79
+ end
80
+
76
81
  describe "original_params", original_params: true do
77
82
  it "returns symbolized params hash" do
78
83
  params = AcceptanceParams.new(name: "Tom", address: { "street" => "1 Main St."} )
@@ -120,7 +125,8 @@ describe SimpleParams::Params do
120
125
  city: nil,
121
126
  zip_code: nil,
122
127
  state: "North Carolina",
123
- company: nil
128
+ company: nil,
129
+ _destroy: false
124
130
  },
125
131
  phone: nil,
126
132
  dogs: [
@@ -160,6 +166,33 @@ describe SimpleParams::Params do
160
166
  name = nested.class.name
161
167
  name.should eq("AcceptanceParams::Address")
162
168
  end
169
+
170
+ describe "params assignment" do
171
+ let(:params) do
172
+ {
173
+ dogs: [
174
+ { name: "Max", age: 12 },
175
+ { name: "Spot", age: 4 },
176
+ { name: "Pants", age: 6, _destroy: true },
177
+ ],
178
+ cats: {
179
+ "0" => { name: "Paws" },
180
+ "1" => { name: "Turbo", _destroy: "1" },
181
+ "2" => { name: "Felix" }
182
+ }
183
+ }
184
+ end
185
+
186
+ subject { AcceptanceParams.new(params) }
187
+
188
+ it "builds correct number of dogs" do
189
+ subject.dogs.count.should eq(2)
190
+ end
191
+
192
+ it "builds correct number of cats" do
193
+ subject.cats.count.should eq(2)
194
+ end
195
+ end
163
196
  end
164
197
 
165
198
  describe "accessors", accessors: true do
@@ -233,10 +266,10 @@ describe SimpleParams::Params do
233
266
  params.attributes.should eq([:reference, :name, :date_of_birth, :current_time, :age, :color, :sibling_names, :address, :phone, :dogs, :cats, :birds])
234
267
  end
235
268
 
236
- it "returns array of attribute symbols for nested class", failing: true do
269
+ it "returns array of attribute symbols for nested class" do
237
270
  params = AcceptanceParams::Address.new({}, nil, "address")
238
271
  params.parent_attribute_name.should eq(:address)
239
- params.attributes.should eq([:street, :city, :zip_code, :state, :company])
272
+ params.attributes.should eq([:street, :city, :zip_code, :state, :company, :_destroy])
240
273
  end
241
274
 
242
275
  it "initializes attributes correctly" do
@@ -352,31 +385,31 @@ describe SimpleParams::Params do
352
385
  params.errors[:dogs][1][:age].should eq(["is not included in the list", "can't be blank"])
353
386
  end
354
387
 
355
- # it "initializes birds as empty", failing: true do
356
- # params = AcceptanceParams.new
357
- # params.birds.should be_empty
358
- # end
359
-
360
- # it "allows absense of optional params", failing: true do
361
- # params = AcceptanceParams.new(
362
- # name: "test",
363
- # address: {
364
- # street: "1 Main St.",
365
- # city: "Asheville",
366
- # zip_code: "28806"
367
- # },
368
- # dogs: [
369
- # { name: "spot", age: 13 }
370
- # ],
371
- # cats: {
372
- # "0" => {
373
- # name: "Purr"
374
- # }
375
- # },
376
- # )
377
- # params.birds.should be_empty
378
- # params.should be_valid
379
- # end
388
+ it "initializes birds as nil" do
389
+ params = AcceptanceParams.new
390
+ params.birds.should be_empty
391
+ end
392
+
393
+ it "allows absense of optional params" do
394
+ params = AcceptanceParams.new(
395
+ name: "test",
396
+ address: {
397
+ street: "1 Main St.",
398
+ city: "Asheville",
399
+ zip_code: "28806"
400
+ },
401
+ dogs: [
402
+ { name: "spot", age: 13 }
403
+ ],
404
+ cats: {
405
+ "0" => {
406
+ name: "Purr"
407
+ }
408
+ },
409
+ )
410
+ params.birds.should be_empty
411
+ params.should be_valid
412
+ end
380
413
  end
381
414
 
382
415
  describe "#validate!" do
@@ -603,9 +636,11 @@ describe SimpleParams::Params do
603
636
  param :zip_code, String, desc: '', required: false
604
637
  param :state, String, desc: '', required: false
605
638
  param :company, String, desc: '', required: false
639
+ param :_destroy, Boolean, desc:'', required: false
606
640
  end
607
641
  param :phone, Hash, desc: '', required: false do
608
642
  param :phone_number, String, desc: '', required: true
643
+ param :_destroy, Boolean, desc:'', required: false
609
644
  end
610
645
  param :dogs, Array, desc: '', required: true do
611
646
  param :name, String, desc: '', required: true
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_params
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.1
4
+ version: 1.5.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - brycesenz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-02 00:00:00.000000000 Z
11
+ date: 2015-12-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel