simple_params 0.0.5 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/Gemfile.lock +1 -1
- data/lib/simple_params/params.rb +14 -20
- data/lib/simple_params/version.rb +1 -1
- data/spec/acceptance_spec.rb +44 -0
- data/spec/multiple_classes_spec.rb +95 -0
- data/spec/params_spec.rb +5 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
ZGFjNDQ2M2VjMDY5MDExZTI3MGMxOGQzYzA5M2ExOGY3MDA0YzlhNg==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
MGMzOGQ3MDNjMDY2ZjE2ZTY1ZTBjM2IyYTExMTJlOWZmMjZhOWYxZQ==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
YjU1ZmI0OGRiNzZlYjFmZjcyMTAyMjFjNjViMTNiMGQzNzUzODVjYTEwY2U0
|
10
|
+
OTkwMmUzYWVmMTI0NmZmYTFlYzMzOTA5NGE2NWRmMGZjMWUwYjBjYjE3YzRm
|
11
|
+
OGNjZjFmYWMzZDlkMTEyYjM0Yjg5ZDMwOGU1MmRmNTQ1YzYwODc=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
NzA2NjI5OWM0MDJiOGE1YTBlZmYwMTgyYzlkMTYyN2RkM2Q5NzNkNDliOWQx
|
14
|
+
NmQ5Yjc4OWY3YTZjYjBkNTBjYWMyN2NjZmQ4NjExNDM5ZGE2N2QwZGY0Yjcx
|
15
|
+
ZDkxYmU4YzZmMTliOTc3ZDVhYjU1MTE0MzZlMTA5YjQ5NTU3ZDE=
|
data/Gemfile.lock
CHANGED
data/lib/simple_params/params.rb
CHANGED
@@ -50,7 +50,7 @@ module SimpleParams
|
|
50
50
|
|
51
51
|
def nested_hash(name, opts={}, &block)
|
52
52
|
attr_accessor name
|
53
|
-
nested_class = define_nested_class(opts, &block)
|
53
|
+
nested_class = define_nested_class(name, opts, &block)
|
54
54
|
@nested_hashes ||= {}
|
55
55
|
@nested_hashes[name.to_sym] = nested_class
|
56
56
|
end
|
@@ -88,16 +88,12 @@ module SimpleParams
|
|
88
88
|
validates name, validations unless validations.empty?
|
89
89
|
end
|
90
90
|
|
91
|
-
def define_nested_class(options, &block)
|
91
|
+
def define_nested_class(name, options, &block)
|
92
|
+
klass_name = name.to_s.split('_').collect(&:capitalize).join
|
92
93
|
Class.new(Params).tap do |klass|
|
93
|
-
name_function = Proc.new {
|
94
|
-
def self.model_name
|
95
|
-
ActiveModel::Name.new(self, nil, "temp")
|
96
|
-
end
|
97
|
-
}
|
98
|
-
klass.class_eval(&name_function)
|
99
94
|
klass.class_eval(&block)
|
100
|
-
klass.class_eval("self.options = options")
|
95
|
+
klass.class_eval("self.options = #{options}")
|
96
|
+
self.const_set(klass_name, klass)
|
101
97
|
end
|
102
98
|
end
|
103
99
|
end
|
@@ -146,7 +142,7 @@ module SimpleParams
|
|
146
142
|
if @original_params.include?(method_name.to_sym)
|
147
143
|
value = @original_params[method_name.to_sym]
|
148
144
|
if value.is_a?(Hash)
|
149
|
-
define_anonymous_class(value)
|
145
|
+
define_anonymous_class(method_name, value)
|
150
146
|
else
|
151
147
|
Attribute.new(self, method_name).value = value
|
152
148
|
end
|
@@ -206,17 +202,15 @@ module SimpleParams
|
|
206
202
|
end
|
207
203
|
end
|
208
204
|
|
209
|
-
def define_anonymous_class(hash)
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
}
|
216
|
-
k.class_eval(&name_function)
|
205
|
+
def define_anonymous_class(name, hash)
|
206
|
+
klass_name = name.to_s.split('_').collect(&:capitalize).join
|
207
|
+
anonymous_klass = Class.new(Params).tap do |klass|
|
208
|
+
# Have to undefine, then redefine, this const in order to avoid warnings.
|
209
|
+
self.class.send(:remove_const, klass_name) if self.class.const_defined?(klass_name)
|
210
|
+
self.class.const_set(klass_name, klass)
|
217
211
|
end
|
218
|
-
|
219
|
-
|
212
|
+
anonymous_klass.allow_undefined_params
|
213
|
+
anonymous_klass.new(hash)
|
220
214
|
end
|
221
215
|
end
|
222
216
|
end
|
data/spec/acceptance_spec.rb
CHANGED
@@ -12,6 +12,7 @@ class AcceptanceParams < SimpleParams::Params
|
|
12
12
|
param :city, validations: { length: { in: 4..40 } }
|
13
13
|
param :zip_code, optional: true
|
14
14
|
param :state, default: "North Carolina"
|
15
|
+
param :company, optional: true
|
15
16
|
end
|
16
17
|
|
17
18
|
def name_has_letters
|
@@ -41,6 +42,20 @@ describe SimpleParams::Params do
|
|
41
42
|
end
|
42
43
|
end
|
43
44
|
|
45
|
+
describe "nested_class", nested_class: true do
|
46
|
+
it "names nested class correctly" do
|
47
|
+
nested = AcceptanceParams.new.address
|
48
|
+
name = nested.class.name
|
49
|
+
name.should eq("AcceptanceParams::Address")
|
50
|
+
end
|
51
|
+
|
52
|
+
# it "names nested class model_class correctly" do
|
53
|
+
# nested = AcceptanceParams.new.address
|
54
|
+
# name = nested.model_class
|
55
|
+
# name.should eq("")
|
56
|
+
# end
|
57
|
+
end
|
58
|
+
|
44
59
|
describe "accessors", accessors: true do
|
45
60
|
let(:params) { AcceptanceParams.new }
|
46
61
|
|
@@ -183,6 +198,34 @@ describe SimpleParams::Params do
|
|
183
198
|
)
|
184
199
|
end
|
185
200
|
end
|
201
|
+
|
202
|
+
describe "acceptance cases" do
|
203
|
+
let(:params) do
|
204
|
+
{
|
205
|
+
name: "Tom",
|
206
|
+
age: 41,
|
207
|
+
address: {
|
208
|
+
street: "1 Main St.",
|
209
|
+
city: "Chicago",
|
210
|
+
state: "IL",
|
211
|
+
zip_code: 33440
|
212
|
+
}
|
213
|
+
}
|
214
|
+
end
|
215
|
+
|
216
|
+
it "is valid after multiple times" do
|
217
|
+
acceptance_params = AcceptanceParams.new(params)
|
218
|
+
acceptance_params.should be_valid
|
219
|
+
acceptance_params.should be_valid
|
220
|
+
end
|
221
|
+
|
222
|
+
it "is invalidated if validity changes after initial assignment" do
|
223
|
+
acceptance_params = AcceptanceParams.new(params)
|
224
|
+
acceptance_params.should be_valid
|
225
|
+
acceptance_params.name = nil
|
226
|
+
acceptance_params.should_not be_valid
|
227
|
+
end
|
228
|
+
end
|
186
229
|
end
|
187
230
|
|
188
231
|
describe "api_pie_documentation", api_pie_documentation: true do
|
@@ -198,6 +241,7 @@ describe SimpleParams::Params do
|
|
198
241
|
param :city, String, desc: '', required: true
|
199
242
|
param :zip_code, String, desc: '', required: false
|
200
243
|
param :state, String, desc: '', required: true
|
244
|
+
param :company, String, desc: '', required: false
|
201
245
|
end
|
202
246
|
API_PIE_DOCS
|
203
247
|
|
@@ -0,0 +1,95 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class Example
|
4
|
+
class ParamsOne < SimpleParams::Params
|
5
|
+
param :name
|
6
|
+
param :age, type: :integer, validations: { inclusion: { in: 18..100 } }
|
7
|
+
|
8
|
+
nested_hash :address do
|
9
|
+
param :street
|
10
|
+
param :city, validations: { length: { in: 4..40 } }
|
11
|
+
param :zip_code, optional: true
|
12
|
+
param :state, optional: true, default: "North Carolina"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class ParamsTwo < SimpleParams::Params
|
17
|
+
param :name
|
18
|
+
param :age, type: :integer, optional: true
|
19
|
+
|
20
|
+
nested_hash :address do
|
21
|
+
param :street
|
22
|
+
param :city, validations: { length: { in: 4..40 } }
|
23
|
+
param :zip_code
|
24
|
+
param :state
|
25
|
+
param :country
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe SimpleParams::Params do
|
31
|
+
describe "api_pie_documentation", api_pie_documentation: true do
|
32
|
+
it "generates valid api_pie documentation for Example::ParamsOne" do
|
33
|
+
documentation = Example::ParamsOne.api_pie_documentation
|
34
|
+
api_docs = <<-API_PIE_DOCS
|
35
|
+
param :name, String, desc: '', required: true
|
36
|
+
param :age, Integer, desc: '', required: true
|
37
|
+
param :address, Hash, desc: '', required: true do
|
38
|
+
param :street, String, desc: '', required: true
|
39
|
+
param :city, String, desc: '', required: true
|
40
|
+
param :zip_code, String, desc: '', required: false
|
41
|
+
param :state, String, desc: '', required: false
|
42
|
+
end
|
43
|
+
API_PIE_DOCS
|
44
|
+
|
45
|
+
expect(documentation).to be_a String
|
46
|
+
expect(documentation.gsub(/\s+/, "")).to eq api_docs.gsub(/\s+/, "")
|
47
|
+
end
|
48
|
+
|
49
|
+
it "generates valid api_pie documentation for Example::ParamsTwo" do
|
50
|
+
documentation = Example::ParamsTwo.api_pie_documentation
|
51
|
+
api_docs = <<-API_PIE_DOCS
|
52
|
+
param :name, String, desc: '', required: true
|
53
|
+
param :age, Integer, desc: '', required: false
|
54
|
+
param :address, Hash, desc: '', required: true do
|
55
|
+
param :street, String, desc: '', required: true
|
56
|
+
param :city, String, desc: '', required: true
|
57
|
+
param :zip_code, String, desc: '', required: true
|
58
|
+
param :state, String, desc: '', required: true
|
59
|
+
param :country, String, desc: '', required: true
|
60
|
+
end
|
61
|
+
API_PIE_DOCS
|
62
|
+
|
63
|
+
expect(documentation).to be_a String
|
64
|
+
expect(documentation.gsub(/\s+/, "")).to eq api_docs.gsub(/\s+/, "")
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
describe "acceptance", acceptance: true do
|
69
|
+
it "is valid with only required params for Example::ParamsOne" do
|
70
|
+
params = {
|
71
|
+
name: "Tom",
|
72
|
+
age: 21,
|
73
|
+
address: {
|
74
|
+
street: "1 Main St.",
|
75
|
+
city: "Chicago"
|
76
|
+
}
|
77
|
+
}
|
78
|
+
Example::ParamsOne.new(params).should be_valid
|
79
|
+
end
|
80
|
+
|
81
|
+
it "is valid with only required params for Example::ParamsTwo" do
|
82
|
+
params = {
|
83
|
+
name: "Tom",
|
84
|
+
address: {
|
85
|
+
street: "1 Main St.",
|
86
|
+
city: "Chicago",
|
87
|
+
zip_code: "20122",
|
88
|
+
state: "IL",
|
89
|
+
country: "USA"
|
90
|
+
}
|
91
|
+
}
|
92
|
+
Example::ParamsTwo.new(params).should be_valid
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
data/spec/params_spec.rb
CHANGED
@@ -292,6 +292,11 @@ describe SimpleParams::Params do
|
|
292
292
|
model.nested.should be_a(SimpleParams::Params)
|
293
293
|
end
|
294
294
|
|
295
|
+
it "allows undefined nested params, and name class correctly" do
|
296
|
+
model = DummyParams.new(nested: { some_value: 1 } )
|
297
|
+
model.nested.class.name.should eq("DummyParams::Nested")
|
298
|
+
end
|
299
|
+
|
295
300
|
it "allows accessors for nested attributes" do
|
296
301
|
model = DummyParams.new(nested: { some_value: 1 } )
|
297
302
|
model.nested.some_value.should eq(1)
|
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: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- brycesenz
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-06-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -155,6 +155,7 @@ files:
|
|
155
155
|
- spec/errors_spec.rb
|
156
156
|
- spec/fixtures/dummy_params.rb
|
157
157
|
- spec/formatter_spec.rb
|
158
|
+
- spec/multiple_classes_spec.rb
|
158
159
|
- spec/params_spec.rb
|
159
160
|
- spec/spec_helper.rb
|
160
161
|
- spec/support/shared_examples/base_attribute.rb
|
@@ -196,6 +197,7 @@ test_files:
|
|
196
197
|
- spec/errors_spec.rb
|
197
198
|
- spec/fixtures/dummy_params.rb
|
198
199
|
- spec/formatter_spec.rb
|
200
|
+
- spec/multiple_classes_spec.rb
|
199
201
|
- spec/params_spec.rb
|
200
202
|
- spec/spec_helper.rb
|
201
203
|
- spec/support/shared_examples/base_attribute.rb
|