simple_params 1.4.2 → 1.4.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 +8 -8
- data/Gemfile.lock +1 -1
- data/lib/simple_params/attribute.rb +34 -0
- data/lib/simple_params/concerns/date_time_helpers.rb +9 -18
- data/lib/simple_params/version.rb +1 -1
- data/spec/acceptance_spec.rb +1 -1
- data/spec/rails_integration_spec.rb +70 -40
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
OTg2MTk0ZWYwZTNmNzhhYzQ0MzBkYjk1MDdhZGQwODAwZGE0NmMyNg==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
ZjY5MWNmODJiNmUzNjAxNGQxNzViMmRjOTkyNmYyMWJhZTBiNjk0OQ==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
MDc0YTExOGFiZjNjMWRkMTg4YWY3ZWIxZTM0YzY5MDdhMjBhMjBiZWQ1YTc3
|
10
|
+
ZjA0YTc1NTVlNjhlOGQwNDI2N2JlMjJjMTBhODAyN2U3MzAzYTA3YTMwZGUw
|
11
|
+
OWJiMDk4Mjc4YzFmMDdjMjk3NWVlNTUxMzQxNzZkYjIwZGEyYzY=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
NDEzNDJjMGI3YTQxODk3OTQwNWUzY2E4NDhlMjQ1MDg4NGRjYjk3ZjUxODE1
|
14
|
+
M2E1MDBmMGE5ZTQ0MDdhMzg0MWZjMWQxZmE2YzA3YmJhMWRkM2YxNTUxMzBm
|
15
|
+
ZWUwODdkZjY5NmI2YmRiMmFkMWNjMzg2N2ZiYzhkYzk3NDg3YWI=
|
data/Gemfile.lock
CHANGED
@@ -39,6 +39,20 @@ module SimpleParams
|
|
39
39
|
end
|
40
40
|
end
|
41
41
|
|
42
|
+
def assign_parameter_attributes(pairs)
|
43
|
+
@p6i ||= pairs["6i"]
|
44
|
+
@p5i ||= pairs["5i"]
|
45
|
+
@p4i ||= pairs["4i"]
|
46
|
+
@p3i ||= pairs["3i"]
|
47
|
+
@p2i ||= pairs["2i"]
|
48
|
+
@p1i ||= pairs["1i"]
|
49
|
+
if all_multiparams_present?
|
50
|
+
self.value = parse_multiparams
|
51
|
+
end
|
52
|
+
rescue ArgumentError
|
53
|
+
self.value = nil
|
54
|
+
end
|
55
|
+
|
42
56
|
private
|
43
57
|
def raw_default
|
44
58
|
if @default.is_a?(Proc)
|
@@ -47,5 +61,25 @@ module SimpleParams
|
|
47
61
|
@default
|
48
62
|
end
|
49
63
|
end
|
64
|
+
|
65
|
+
def all_multiparams_present?
|
66
|
+
if @type == Date
|
67
|
+
[@p1i, @p2i, @p3i].all? { |p| !p.nil? }
|
68
|
+
elsif (@type == DateTime) || (@type == Time)
|
69
|
+
[@p1i, @p2i, @p3i, @p4i, @p5i, @p6i].all? { |p| !p.nil? }
|
70
|
+
else
|
71
|
+
true
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def parse_multiparams
|
76
|
+
if @type == Date
|
77
|
+
Date.new(@p1i.to_i, @p2i.to_i, @p3i.to_i)
|
78
|
+
elsif @type == DateTime
|
79
|
+
DateTime.new(@p1i.to_i, @p2i.to_i, @p3i.to_i, @p4i.to_i, @p5i.to_i, @p6i.to_i)
|
80
|
+
elsif @type == Time
|
81
|
+
Time.new(@p1i.to_i, @p2i.to_i, @p3i.to_i, @p4i.to_i, @p5i.to_i, @p6i.to_i)
|
82
|
+
end
|
83
|
+
end
|
50
84
|
end
|
51
85
|
end
|
@@ -9,58 +9,49 @@ module SimpleParams
|
|
9
9
|
def define_date_helper_methods(name)
|
10
10
|
define_method("#{name}(3i)=") do |day|
|
11
11
|
attribute = send("#{name}_attribute")
|
12
|
-
|
13
|
-
attribute.send("value=", Date.new(value.year, value.month, day.to_i))
|
12
|
+
attribute.assign_parameter_attributes("3i" => day)
|
14
13
|
end
|
15
14
|
|
16
15
|
define_method("#{name}(2i)=") do |month|
|
17
16
|
attribute = send("#{name}_attribute")
|
18
|
-
|
19
|
-
attribute.send("value=", Date.new(value.year, month.to_i, value.day))
|
17
|
+
attribute.assign_parameter_attributes("2i" => month)
|
20
18
|
end
|
21
19
|
|
22
20
|
define_method("#{name}(1i)=") do |year|
|
23
21
|
attribute = send("#{name}_attribute")
|
24
|
-
|
25
|
-
attribute.send("value=", Date.new(year.to_i, value.month, value.day))
|
22
|
+
attribute.assign_parameter_attributes("1i" => year)
|
26
23
|
end
|
27
24
|
end
|
28
25
|
|
29
26
|
def define_datetime_helper_methods(name)
|
30
27
|
define_method("#{name}(6i)=") do |sec|
|
31
28
|
attribute = send("#{name}_attribute")
|
32
|
-
|
33
|
-
attribute.send("value=", Time.new(value.year, value.month, value.day, value.hour, value.min, sec.to_i, value.utc_offset))
|
29
|
+
attribute.assign_parameter_attributes("6i" => sec)
|
34
30
|
end
|
35
31
|
|
36
32
|
define_method("#{name}(5i)=") do |minute|
|
37
33
|
attribute = send("#{name}_attribute")
|
38
|
-
|
39
|
-
attribute.send("value=", Time.new(value.year, value.month, value.day, value.hour, minute.to_i, value.sec, value.utc_offset))
|
34
|
+
attribute.assign_parameter_attributes("5i" => minute)
|
40
35
|
end
|
41
36
|
|
42
37
|
define_method("#{name}(4i)=") do |hour|
|
43
38
|
attribute = send("#{name}_attribute")
|
44
|
-
|
45
|
-
attribute.send("value=", Time.new(value.year, value.month, value.day, hour.to_i, value.min, value.sec, value.utc_offset))
|
39
|
+
attribute.assign_parameter_attributes("4i" => hour)
|
46
40
|
end
|
47
41
|
|
48
42
|
define_method("#{name}(3i)=") do |day|
|
49
43
|
attribute = send("#{name}_attribute")
|
50
|
-
|
51
|
-
attribute.send("value=", Time.new(value.year, value.month, day.to_i, value.hour, value.min, value.sec, value.utc_offset))
|
44
|
+
attribute.assign_parameter_attributes("3i" => day)
|
52
45
|
end
|
53
46
|
|
54
47
|
define_method("#{name}(2i)=") do |month|
|
55
48
|
attribute = send("#{name}_attribute")
|
56
|
-
|
57
|
-
attribute.send("value=", Time.new(value.year, month.to_i, value.day, value.hour, value.min, value.sec, value.utc_offset))
|
49
|
+
attribute.assign_parameter_attributes("2i" => month)
|
58
50
|
end
|
59
51
|
|
60
52
|
define_method("#{name}(1i)=") do |year|
|
61
53
|
attribute = send("#{name}_attribute")
|
62
|
-
|
63
|
-
attribute.send("value=", Time.new(year.to_i, value.month, value.day, value.hour, value.min, value.sec, value.utc_offset))
|
54
|
+
attribute.assign_parameter_attributes("1i" => year)
|
64
55
|
end
|
65
56
|
end
|
66
57
|
end
|
data/spec/acceptance_spec.rb
CHANGED
@@ -192,7 +192,7 @@ describe SimpleParams::Params do
|
|
192
192
|
end
|
193
193
|
|
194
194
|
describe "datetime setters", datetime_accessors: true do
|
195
|
-
it "can set date through Rails style date setters" do
|
195
|
+
it "can set date through Rails style date setters", failing: true do
|
196
196
|
params = AcceptanceParams.new(
|
197
197
|
"date_of_birth(3i)" => "5",
|
198
198
|
"date_of_birth(2i)" => "6",
|
@@ -21,52 +21,82 @@ class RailsIntegrationParams < SimpleParams::Params
|
|
21
21
|
end
|
22
22
|
|
23
23
|
describe SimpleParams::Params do
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
dogs_attributes: {
|
39
|
-
"0" => {
|
40
|
-
name: "Spot",
|
41
|
-
age: 4
|
24
|
+
context "with valid params" do
|
25
|
+
let!(:params) do
|
26
|
+
RailsIntegrationParams.new(
|
27
|
+
name: "Tom",
|
28
|
+
age: 21,
|
29
|
+
"current_time(6i)" => 59,
|
30
|
+
"current_time(5i)" => 58,
|
31
|
+
"current_time(4i)" => 11,
|
32
|
+
"current_time(3i)" => 4,
|
33
|
+
"current_time(2i)" => 3,
|
34
|
+
"current_time(1i)" => 2009,
|
35
|
+
address_attributes: {
|
36
|
+
street: "1 Main St.",
|
37
|
+
city: "Charlotte"
|
42
38
|
},
|
43
|
-
|
44
|
-
|
39
|
+
dogs_attributes: {
|
40
|
+
"0" => {
|
41
|
+
name: "Spot",
|
42
|
+
age: 4
|
43
|
+
},
|
44
|
+
"1" => {
|
45
|
+
age: 6
|
46
|
+
}
|
45
47
|
}
|
46
|
-
|
47
|
-
|
48
|
-
end
|
48
|
+
)
|
49
|
+
end
|
49
50
|
|
50
|
-
|
51
|
-
|
52
|
-
|
51
|
+
specify "setting datetime" do
|
52
|
+
expect(params.current_time).to eq DateTime.new(2009, 3, 4, 11, 58, 59)
|
53
|
+
end
|
53
54
|
|
54
|
-
|
55
|
-
|
56
|
-
|
55
|
+
specify "sets address" do
|
56
|
+
address = params.address
|
57
|
+
expect(address.street).to eq "1 Main St."
|
58
|
+
expect(address.city).to eq "Charlotte"
|
59
|
+
end
|
57
60
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
61
|
+
specify "sets dogs"do
|
62
|
+
dogs = params.dogs
|
63
|
+
expect(dogs.count).to eq 2
|
64
|
+
expect(dogs[0].name).to eq "Spot"
|
65
|
+
expect(dogs[0].age).to eq 4
|
66
|
+
expect(dogs[1].name).to eq nil
|
67
|
+
expect(dogs[1].age).to eq 6
|
68
|
+
end
|
62
69
|
end
|
63
70
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
+
context "with invalid datetime params" do
|
72
|
+
let!(:params) do
|
73
|
+
RailsIntegrationParams.new(
|
74
|
+
name: "Tom",
|
75
|
+
age: 21,
|
76
|
+
"current_time(6i)" => nil,
|
77
|
+
"current_time(5i)" => nil,
|
78
|
+
"current_time(4i)" => nil,
|
79
|
+
"current_time(3i)" => 1,
|
80
|
+
"current_time(2i)" => nil,
|
81
|
+
"current_time(1i)" => nil,
|
82
|
+
address_attributes: {
|
83
|
+
street: "1 Main St.",
|
84
|
+
city: "Charlotte"
|
85
|
+
},
|
86
|
+
dogs_attributes: {
|
87
|
+
"0" => {
|
88
|
+
name: "Spot",
|
89
|
+
age: 4
|
90
|
+
},
|
91
|
+
"1" => {
|
92
|
+
age: 6
|
93
|
+
}
|
94
|
+
}
|
95
|
+
)
|
96
|
+
end
|
97
|
+
|
98
|
+
specify "setting datetime to nil" do
|
99
|
+
expect(params.current_time).to eq(nil)
|
100
|
+
end
|
71
101
|
end
|
72
102
|
end
|
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.4.
|
4
|
+
version: 1.4.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- brycesenz
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-09-
|
11
|
+
date: 2015-09-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|