simple_params 0.0.2.pre4 → 0.0.2.pre5
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 +10 -2
- data/lib/simple_params/api_pie_doc/attribute.rb +81 -0
- data/lib/simple_params/api_pie_doc.rb +38 -0
- data/lib/simple_params/params.rb +23 -17
- data/lib/simple_params/version.rb +1 -1
- data/lib/simple_params.rb +2 -0
- data/simple_params.gemspec +1 -0
- data/spec/acceptance_spec.rb +30 -10
- data/spec/api_pie_doc/attribute_spec.rb +145 -0
- data/spec/api_pie_doc_spec.rb +40 -0
- data/spec/fixtures/dummy_params.rb +32 -0
- data/spec/params_spec.rb +39 -44
- data/spec/spec_helper.rb +1 -0
- metadata +24 -2
checksums.yaml
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
---
|
|
2
2
|
!binary "U0hBMQ==":
|
|
3
3
|
metadata.gz: !binary |-
|
|
4
|
-
|
|
4
|
+
ZTZkN2ZhYmUzN2JhN2UyMTFmZDA1ZmE5MThiNGJiOTg1ZGEzZGY3YQ==
|
|
5
5
|
data.tar.gz: !binary |-
|
|
6
|
-
|
|
6
|
+
OWFiODcyY2M2ODAwODA3MGFhMDY5ZTkwMDQxM2M3MWE4NDdiNzAwMw==
|
|
7
7
|
SHA512:
|
|
8
8
|
metadata.gz: !binary |-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
N2UwMzExNTc0ZGIxMzA4MDYxYzdiZWM1YTgwMzUxMmQzYjc4OTkzOTBiOGEw
|
|
10
|
+
YTBlMWM3Nzk2MzVjNjYxYzQ3ZmZkMzdhYjk2Y2MwM2ZhZTU3YjhiNTk1Y2Ix
|
|
11
|
+
ZDc4NzU3MzdhOGYzZjZkNTUxMjdjODZhYTE4Yzk0ZGUxMTJhZDc=
|
|
12
12
|
data.tar.gz: !binary |-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
YjM3MTNkY2UxYzU5YmE5OGQxYmNiNTI2MjUzZGI4ZGExYWQzZTA4NjM3MzFm
|
|
14
|
+
YjllOGU4ZWZiYWRiODQxY2E1NmE4NDExY2JjNGNhYzYzODhiNDUwMjhlMDRh
|
|
15
|
+
YTdhOWE1MTNkZmRlYTY4NDZmNWEyYjFhN2U3NTkzZTliNGY2OGI=
|
data/Gemfile.lock
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
PATH
|
|
2
2
|
remote: .
|
|
3
3
|
specs:
|
|
4
|
-
simple_params (0.0.2.
|
|
4
|
+
simple_params (0.0.2.pre4)
|
|
5
5
|
activemodel (>= 3.0, < 5.0)
|
|
6
6
|
virtus (>= 1.0.0)
|
|
7
7
|
|
|
@@ -22,6 +22,7 @@ GEM
|
|
|
22
22
|
ice_nine (~> 0.11.0)
|
|
23
23
|
thread_safe (~> 0.3, >= 0.3.1)
|
|
24
24
|
builder (3.2.2)
|
|
25
|
+
coderay (1.1.0)
|
|
25
26
|
coercible (1.0.0)
|
|
26
27
|
descendants_tracker (~> 0.0.1)
|
|
27
28
|
descendants_tracker (0.0.4)
|
|
@@ -31,7 +32,12 @@ GEM
|
|
|
31
32
|
i18n (0.7.0)
|
|
32
33
|
ice_nine (0.11.1)
|
|
33
34
|
json (1.8.2)
|
|
35
|
+
method_source (0.8.2)
|
|
34
36
|
minitest (5.5.1)
|
|
37
|
+
pry (0.10.1)
|
|
38
|
+
coderay (~> 1.1.0)
|
|
39
|
+
method_source (~> 0.8.1)
|
|
40
|
+
slop (~> 3.4)
|
|
35
41
|
rake (10.4.2)
|
|
36
42
|
rspec (2.99.0)
|
|
37
43
|
rspec-core (~> 2.99.0)
|
|
@@ -41,7 +47,8 @@ GEM
|
|
|
41
47
|
rspec-expectations (2.99.2)
|
|
42
48
|
diff-lcs (>= 1.1.3, < 2.0)
|
|
43
49
|
rspec-mocks (2.99.3)
|
|
44
|
-
|
|
50
|
+
slop (3.6.0)
|
|
51
|
+
thread_safe (0.3.5)
|
|
45
52
|
tzinfo (1.2.2)
|
|
46
53
|
thread_safe (~> 0.1)
|
|
47
54
|
virtus (1.0.4)
|
|
@@ -55,6 +62,7 @@ PLATFORMS
|
|
|
55
62
|
|
|
56
63
|
DEPENDENCIES
|
|
57
64
|
bundler (~> 1.5)
|
|
65
|
+
pry
|
|
58
66
|
rake (~> 10.1)
|
|
59
67
|
rspec (~> 2.6)
|
|
60
68
|
simple_params!
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
module SimpleParams
|
|
2
|
+
class ApiPieDoc::Attribute
|
|
3
|
+
attr_accessor :attribute, :nested, :options
|
|
4
|
+
|
|
5
|
+
alias :nested? :nested
|
|
6
|
+
|
|
7
|
+
def initialize(simple_params_attribute)
|
|
8
|
+
self.attribute = simple_params_attribute
|
|
9
|
+
self.nested = simple_params_attribute.is_a?(Hash)
|
|
10
|
+
self.options ||= nested? ? attribute.delete(:options) : attribute[1]
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def name
|
|
14
|
+
nested? ? attribute.keys.first.to_s : attribute[0].to_s
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def to_s
|
|
18
|
+
return nil if do_not_document?
|
|
19
|
+
|
|
20
|
+
if nested?
|
|
21
|
+
nested_description
|
|
22
|
+
else
|
|
23
|
+
attribute_description
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
private
|
|
28
|
+
|
|
29
|
+
def do_not_document?
|
|
30
|
+
options[:document].eql?(false)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def nested_description
|
|
34
|
+
start = "param :#{name}, Hash, #{description}, #{requirement_description} do"
|
|
35
|
+
attribute_descriptors = []
|
|
36
|
+
attribute.values.flat_map(&:to_a).each do |nested_attribute|
|
|
37
|
+
attribute_descriptors << self.class.new(nested_attribute).to_s
|
|
38
|
+
end
|
|
39
|
+
finish = "end"
|
|
40
|
+
[start, attribute_descriptors, finish].flatten.join("\n")
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def attribute_description
|
|
44
|
+
base = "param :#{name}"
|
|
45
|
+
[base, type_description, description, requirement_description].reject(&:blank?).join(", ")
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
NotValidValueError = Class.new(StandardError)
|
|
49
|
+
|
|
50
|
+
def type_description
|
|
51
|
+
value = options[:type]
|
|
52
|
+
case value
|
|
53
|
+
when :string, :integer, :array, :hash
|
|
54
|
+
"#{value.to_s.capitalize.constantize}"
|
|
55
|
+
when 'String', 'Integer', 'Array', 'Hash'
|
|
56
|
+
"#{value}"
|
|
57
|
+
when :decimal, :datetime, :date, :time, :float, :boolean
|
|
58
|
+
""
|
|
59
|
+
else
|
|
60
|
+
raise NotValidValueError.new("Must be one of #{SimpleParams::Params::TYPES}")
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def requirement_description
|
|
65
|
+
value = options[:optional]
|
|
66
|
+
case value
|
|
67
|
+
when true
|
|
68
|
+
""
|
|
69
|
+
when false
|
|
70
|
+
"required: true"
|
|
71
|
+
else
|
|
72
|
+
"required: true"
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def description
|
|
77
|
+
description = options[:desc] || ''
|
|
78
|
+
"desc: '#{description}'"
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
module SimpleParams
|
|
2
|
+
class ApiPieDoc
|
|
3
|
+
|
|
4
|
+
attr_accessor :base_attributes,
|
|
5
|
+
:nested_hashes,
|
|
6
|
+
:nested_attributes,
|
|
7
|
+
:docs
|
|
8
|
+
|
|
9
|
+
def initialize(simple_params)
|
|
10
|
+
self.base_attributes = simple_params.defined_attributes
|
|
11
|
+
self.nested_hashes = simple_params.nested_hashes
|
|
12
|
+
self.nested_attributes = []
|
|
13
|
+
self.docs = []
|
|
14
|
+
|
|
15
|
+
build_nested_attributes
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def build
|
|
19
|
+
base_attributes.each do |attribute|
|
|
20
|
+
docs << Attribute.new(attribute).to_s
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
nested_attributes.each do |nested_attribute|
|
|
24
|
+
docs << Attribute.new(nested_attribute).to_s
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
docs.join("\n")
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
private
|
|
31
|
+
|
|
32
|
+
def build_nested_attributes
|
|
33
|
+
nested_hashes.each do |name, parameter_set|
|
|
34
|
+
nested_attributes << { name => parameter_set.defined_attributes, options: parameter_set.options }
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
data/lib/simple_params/params.rb
CHANGED
|
@@ -7,19 +7,20 @@ module SimpleParams
|
|
|
7
7
|
include ActiveModel::Validations
|
|
8
8
|
include SimpleParams::Validations
|
|
9
9
|
|
|
10
|
+
TYPES = [
|
|
11
|
+
:integer,
|
|
12
|
+
:string,
|
|
13
|
+
:decimal,
|
|
14
|
+
:datetime,
|
|
15
|
+
:date,
|
|
16
|
+
:time,
|
|
17
|
+
:float,
|
|
18
|
+
:boolean,
|
|
19
|
+
:array,
|
|
20
|
+
:hash
|
|
21
|
+
]
|
|
22
|
+
|
|
10
23
|
class << self
|
|
11
|
-
TYPES = [
|
|
12
|
-
:integer,
|
|
13
|
-
:string,
|
|
14
|
-
:decimal,
|
|
15
|
-
:datetime,
|
|
16
|
-
:date,
|
|
17
|
-
:time,
|
|
18
|
-
:float,
|
|
19
|
-
:boolean,
|
|
20
|
-
:array,
|
|
21
|
-
:hash
|
|
22
|
-
]
|
|
23
24
|
|
|
24
25
|
TYPES.each do |sym|
|
|
25
26
|
define_method("#{sym}_param") do |name, opts={}|
|
|
@@ -27,7 +28,11 @@ module SimpleParams
|
|
|
27
28
|
end
|
|
28
29
|
end
|
|
29
30
|
|
|
30
|
-
attr_accessor :strict_enforcement
|
|
31
|
+
attr_accessor :strict_enforcement, :options
|
|
32
|
+
|
|
33
|
+
def api_pie_documentation
|
|
34
|
+
SimpleParams::ApiPieDoc.new(self).build
|
|
35
|
+
end
|
|
31
36
|
|
|
32
37
|
def strict
|
|
33
38
|
@strict_enforcement = true
|
|
@@ -44,7 +49,7 @@ module SimpleParams
|
|
|
44
49
|
|
|
45
50
|
def nested_hash(name, opts={}, &block)
|
|
46
51
|
attr_accessor name
|
|
47
|
-
nested_class = define_nested_class(&block)
|
|
52
|
+
nested_class = define_nested_class(opts, &block)
|
|
48
53
|
@nested_hashes ||= {}
|
|
49
54
|
@nested_hashes[name.to_sym] = nested_class
|
|
50
55
|
end
|
|
@@ -82,7 +87,7 @@ module SimpleParams
|
|
|
82
87
|
validates name, validations unless validations.empty?
|
|
83
88
|
end
|
|
84
89
|
|
|
85
|
-
def define_nested_class(&block)
|
|
90
|
+
def define_nested_class(options, &block)
|
|
86
91
|
Class.new(Params).tap do |klass|
|
|
87
92
|
name_function = Proc.new {
|
|
88
93
|
def self.model_name
|
|
@@ -91,6 +96,7 @@ module SimpleParams
|
|
|
91
96
|
}
|
|
92
97
|
klass.class_eval(&name_function)
|
|
93
98
|
klass.class_eval(&block)
|
|
99
|
+
klass.class_eval("self.options = options")
|
|
94
100
|
end
|
|
95
101
|
end
|
|
96
102
|
end
|
|
@@ -151,7 +157,7 @@ module SimpleParams
|
|
|
151
157
|
end
|
|
152
158
|
|
|
153
159
|
def set_accessors(params={})
|
|
154
|
-
params.each do |attribute_name, value|
|
|
160
|
+
params.each do |attribute_name, value|
|
|
155
161
|
# Don't set accessors for nested classes
|
|
156
162
|
unless value.is_a?(Hash)
|
|
157
163
|
send("#{attribute_name}=", value)
|
|
@@ -187,4 +193,4 @@ module SimpleParams
|
|
|
187
193
|
klass.new(hash)
|
|
188
194
|
end
|
|
189
195
|
end
|
|
190
|
-
end
|
|
196
|
+
end
|
data/lib/simple_params.rb
CHANGED
data/simple_params.gemspec
CHANGED
data/spec/acceptance_spec.rb
CHANGED
|
@@ -2,7 +2,7 @@ require 'spec_helper'
|
|
|
2
2
|
|
|
3
3
|
class AcceptanceParams < SimpleParams::Params
|
|
4
4
|
param :name
|
|
5
|
-
param :age, type:
|
|
5
|
+
param :age, type: :integer, optional: true
|
|
6
6
|
param :color, default: "red", validations: { inclusion: { in: ["red", "green"] }}
|
|
7
7
|
|
|
8
8
|
nested_hash :address do
|
|
@@ -23,7 +23,7 @@ describe SimpleParams::Params do
|
|
|
23
23
|
params.name = "Tom"
|
|
24
24
|
params.name.should eq("Tom")
|
|
25
25
|
end
|
|
26
|
-
|
|
26
|
+
|
|
27
27
|
it "has getter and setter methods for optional param" do
|
|
28
28
|
params.should respond_to(:age)
|
|
29
29
|
params.name.should be_nil
|
|
@@ -38,7 +38,7 @@ describe SimpleParams::Params do
|
|
|
38
38
|
params.address.street = "1 Main St."
|
|
39
39
|
params.address.street.should eq("1 Main St.")
|
|
40
40
|
end
|
|
41
|
-
|
|
41
|
+
|
|
42
42
|
it "has getter and setter methods for optional param" do
|
|
43
43
|
params.address.should respond_to(:zip_code)
|
|
44
44
|
params.address.zip_code.should be_nil
|
|
@@ -49,13 +49,13 @@ describe SimpleParams::Params do
|
|
|
49
49
|
end
|
|
50
50
|
|
|
51
51
|
describe "array syntax", array_syntax: true do
|
|
52
|
-
let(:params) do
|
|
52
|
+
let(:params) do
|
|
53
53
|
AcceptanceParams.new(
|
|
54
|
-
name: "Bill",
|
|
55
|
-
age: 30,
|
|
56
|
-
address: {
|
|
57
|
-
city: "Greenville"
|
|
58
|
-
}
|
|
54
|
+
name: "Bill",
|
|
55
|
+
age: 30,
|
|
56
|
+
address: {
|
|
57
|
+
city: "Greenville"
|
|
58
|
+
}
|
|
59
59
|
)
|
|
60
60
|
end
|
|
61
61
|
|
|
@@ -136,4 +136,24 @@ describe SimpleParams::Params do
|
|
|
136
136
|
end
|
|
137
137
|
end
|
|
138
138
|
end
|
|
139
|
-
|
|
139
|
+
|
|
140
|
+
describe "api_pie_documentation", api_pie_documentation: true do
|
|
141
|
+
it "generates valida api_pie documentation" do
|
|
142
|
+
documentation = AcceptanceParams.api_pie_documentation
|
|
143
|
+
api_docs = <<-API_PIE_DOCS
|
|
144
|
+
param :name, String, desc: '', required: true
|
|
145
|
+
param :age, Integer, desc: ''
|
|
146
|
+
param :color, String, desc: '', required: true
|
|
147
|
+
param :address, Hash, desc: '', required: true do
|
|
148
|
+
param :street, String, desc: '', required: true
|
|
149
|
+
param :city, String, desc: '', required: true
|
|
150
|
+
param :zip_code, String, desc: ''
|
|
151
|
+
param :state, String, desc: '', required: true
|
|
152
|
+
end
|
|
153
|
+
API_PIE_DOCS
|
|
154
|
+
|
|
155
|
+
expect(documentation).to be_a String
|
|
156
|
+
expect(documentation.gsub(/\s+/, "")).to eq api_docs.gsub(/\s+/, "")
|
|
157
|
+
end
|
|
158
|
+
end
|
|
159
|
+
end
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe SimpleParams::ApiPieDoc::Attribute do
|
|
4
|
+
let(:simple_param_attribute) { [:name, {:type=>:string}] }
|
|
5
|
+
let(:nested_simple_param_attribute) {
|
|
6
|
+
{:address=>
|
|
7
|
+
{:street=>{:type=>:string},
|
|
8
|
+
:city=>{:validations=>{:length=>{:in=>4..40}, :presence=>true}, :type=>:string},
|
|
9
|
+
:zip_code=>{:optional=>true, :type=>:string},
|
|
10
|
+
:state=>{:default=>"North Carolina", :type=>:string}
|
|
11
|
+
},
|
|
12
|
+
:options=>{desc: 'i like pie'}
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
let(:api_pie_doc_attribute) { described_class.new(simple_param_attribute) }
|
|
16
|
+
let(:nested_api_pie_doc_attribute) { described_class.new(nested_simple_param_attribute) }
|
|
17
|
+
|
|
18
|
+
describe '#initialize' do
|
|
19
|
+
|
|
20
|
+
specify 'should give instance an attribute' do
|
|
21
|
+
expect(api_pie_doc_attribute.attribute).to eq simple_param_attribute
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
specify 'should set nested' do
|
|
25
|
+
expect(api_pie_doc_attribute.nested).to eq false
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
describe '#name' do
|
|
30
|
+
context 'when attribute is nested' do
|
|
31
|
+
specify 'should set respond with the right name' do
|
|
32
|
+
expect(nested_api_pie_doc_attribute.name).to eq 'address'
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
context 'when attribute is not nested' do
|
|
37
|
+
specify 'should set respond with the right name' do
|
|
38
|
+
expect(api_pie_doc_attribute.name).to eq 'name'
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
describe '#options' do
|
|
44
|
+
context 'when attribute is nested' do
|
|
45
|
+
specify 'should return nested attribute options' do
|
|
46
|
+
expect(nested_api_pie_doc_attribute.options).to eq({desc: 'i like pie'})
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
context 'when attribute is not nested' do
|
|
51
|
+
specify 'should return the attributes options' do
|
|
52
|
+
expect(api_pie_doc_attribute.options).to eq({ type: :string })
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
describe '#nested?' do
|
|
58
|
+
context 'when attribute is nested' do
|
|
59
|
+
specify 'should return true' do
|
|
60
|
+
expect(nested_api_pie_doc_attribute.nested?).to eq true
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
context 'when attribute is not nested' do
|
|
65
|
+
specify 'should return false' do
|
|
66
|
+
expect(api_pie_doc_attribute.nested?).to eq false
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
describe '#to_s' do
|
|
72
|
+
context 'when attribute is nested' do
|
|
73
|
+
specify 'should return properly formatted string' do
|
|
74
|
+
expect(nested_api_pie_doc_attribute.to_s).to eq("param :address, Hash, desc: 'i like pie', required: true do\nparam :street, String, desc: '', required: true\nparam :city, String, desc: '', required: true\nparam :zip_code, String, desc: ''\nparam :state, String, desc: '', required: true\nend")
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
context 'when attribute is not nested' do
|
|
79
|
+
specify 'should return properly formatted string' do
|
|
80
|
+
expect(api_pie_doc_attribute.to_s).to eq("param :name, String, desc: '', required: true")
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
describe "#type_description" do
|
|
86
|
+
context "where attribute.options[:type] is a accepted symbol" do
|
|
87
|
+
specify "should return a formatted string with constantized version of symbol" do
|
|
88
|
+
expect(api_pie_doc_attribute.send(:type_description)).to eq "String"
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
context "where attribute.options[:type] is a accepted string" do
|
|
93
|
+
specify "should return a formatted string with constantized version of symbol" do
|
|
94
|
+
attribute = SimpleParams::ApiPieDoc::Attribute.new([:name, {:type=>'String'}])
|
|
95
|
+
expect(attribute.send(:type_description)).to eq "String"
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
context "where attribute.options[:type] is anything else" do
|
|
100
|
+
specify "should raise an error" do
|
|
101
|
+
attribute = SimpleParams::ApiPieDoc::Attribute.new([:name, {:type=>'Craziness'}])
|
|
102
|
+
expect{attribute.send(:type_description)}.to raise_error(SimpleParams::ApiPieDoc::Attribute::NotValidValueError)
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
describe "#requirement_description" do
|
|
108
|
+
context "when attribute options[:optional] is true" do
|
|
109
|
+
specify "should return a formatted string indicating the attribute is not required" do
|
|
110
|
+
attribute = SimpleParams::ApiPieDoc::Attribute.new([:name, {:optional=>true}])
|
|
111
|
+
expect(attribute.send(:requirement_description)).to eq ""
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
context "when attribute options[:optional] is false" do
|
|
116
|
+
specify "should return a formatted string indicating the attribute is required" do
|
|
117
|
+
attribute = SimpleParams::ApiPieDoc::Attribute.new([:name, {:optional=>false}])
|
|
118
|
+
expect(attribute.send(:requirement_description)).to eq "required: true"
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
context "when passed anything other than true or false" do
|
|
123
|
+
specify "should return a formatted string indicating the attribute is required" do
|
|
124
|
+
attribute = SimpleParams::ApiPieDoc::Attribute.new([:name, {:optional=>:blah}])
|
|
125
|
+
expect(attribute.send(:requirement_description)).to eq "required: true"
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
describe "#description" do
|
|
131
|
+
context "when attribute options[:desc] is not nil" do
|
|
132
|
+
specify "should use options[:desc] to populate attribute description" do
|
|
133
|
+
attribute = SimpleParams::ApiPieDoc::Attribute.new([:name, {:desc => 'I like pie'}])
|
|
134
|
+
expect(attribute.send(:description)).to eq "desc: 'I like pie'"
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
context "when attribute options[:desc] is nil" do
|
|
139
|
+
specify "should return an empty string as the description" do
|
|
140
|
+
attribute = SimpleParams::ApiPieDoc::Attribute.new([:name, {}])
|
|
141
|
+
expect(attribute.send(:description)).to eq "desc: ''"
|
|
142
|
+
end
|
|
143
|
+
end
|
|
144
|
+
end
|
|
145
|
+
end
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
require 'fixtures/dummy_params'
|
|
3
|
+
|
|
4
|
+
describe SimpleParams::ApiPieDoc do
|
|
5
|
+
|
|
6
|
+
let(:api_pie_doc) { SimpleParams::ApiPieDoc.new(DummyParams) }
|
|
7
|
+
|
|
8
|
+
describe "#initialize" do
|
|
9
|
+
specify "should give object base_attributes" do
|
|
10
|
+
expect(api_pie_doc.base_attributes).to include(name: { type: :string })
|
|
11
|
+
expect(api_pie_doc.base_attributes).to include(age: { optional: true, type: :integer })
|
|
12
|
+
expect(api_pie_doc.base_attributes.keys).to include(:amount, :color, :first_initial)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
specify "should give object nested_hashes" do
|
|
16
|
+
expect(api_pie_doc.nested_hashes.keys).to eq [:address, :phone]
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
specify "should call #build_nested_attributes" do
|
|
20
|
+
expect_any_instance_of(SimpleParams::ApiPieDoc).to receive(:build_nested_attributes)
|
|
21
|
+
api_pie_doc
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
specify "should give object nested_attributes" do
|
|
25
|
+
expect(api_pie_doc.nested_attributes.flat_map(&:keys)).to include(:address, :phone)
|
|
26
|
+
expect(api_pie_doc.nested_attributes[0].values.flat_map(&:keys)).to eq [:street, :city, :zip_code, :state]
|
|
27
|
+
expect(api_pie_doc.nested_attributes[1].values.flat_map(&:keys)).to eq [:cell_phone, :phone_number, :area_code]
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
specify "should give object docs" do
|
|
31
|
+
expect(api_pie_doc.docs).to eq []
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
describe "#build" do
|
|
36
|
+
specify "should return a string of api_pie documentation params" do
|
|
37
|
+
expect(api_pie_doc.build).to be_a String
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
class DummyParams < SimpleParams::Params
|
|
2
|
+
string_param :name
|
|
3
|
+
integer_param :age, optional: true
|
|
4
|
+
string_param :first_initial, default: lambda { |params, param| params.name[0] if params.name.present? }
|
|
5
|
+
decimal_param :amount, optional: true, default: 0.10, formatter: lambda { |params, param| param.round(2) }
|
|
6
|
+
param :color, default: "red", validations: { inclusion: { in: ["red", "green"] }}, formatter: :lower_case_colors
|
|
7
|
+
|
|
8
|
+
nested_hash :address do
|
|
9
|
+
string_param :street
|
|
10
|
+
string_param :city, validations: { length: { in: 4..40 } }
|
|
11
|
+
string_param :zip_code, optional: true, validations: { length: { in: 5..9 } }
|
|
12
|
+
param :state, default: "North Carolina", formatter: :transform_state_code
|
|
13
|
+
|
|
14
|
+
def transform_state_code(val)
|
|
15
|
+
val == "SC" ? "South Carolina" : val
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
nested_hash :phone do
|
|
20
|
+
boolean_param :cell_phone, default: true
|
|
21
|
+
string_param :phone_number, validations: { length: { in: 7..10 } }, formatter: lambda { |params, attribute| attribute.gsub(/\D/, "") }
|
|
22
|
+
string_param :area_code, default: lambda { |params, param|
|
|
23
|
+
if params.phone_number.present?
|
|
24
|
+
params.phone_number[0..2]
|
|
25
|
+
end
|
|
26
|
+
}
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def lower_case_colors(val)
|
|
30
|
+
val.downcase
|
|
31
|
+
end
|
|
32
|
+
end
|
data/spec/params_spec.rb
CHANGED
|
@@ -1,37 +1,5 @@
|
|
|
1
1
|
require 'spec_helper'
|
|
2
|
-
|
|
3
|
-
class DummyParams < SimpleParams::Params
|
|
4
|
-
string_param :name
|
|
5
|
-
integer_param :age, optional: true
|
|
6
|
-
string_param :first_initial, default: lambda { |params, param| params.name[0] if params.name.present? }
|
|
7
|
-
decimal_param :amount, optional: true, default: 0.10, formatter: lambda { |params, param| param.round(2) }
|
|
8
|
-
param :color, default: "red", validations: { inclusion: { in: ["red", "green"] }}, formatter: :lower_case_colors
|
|
9
|
-
|
|
10
|
-
nested_hash :address do
|
|
11
|
-
string_param :street
|
|
12
|
-
string_param :city, validations: { length: { in: 4..40 } }
|
|
13
|
-
string_param :zip_code, optional: true, validations: { length: { in: 5..9 } }
|
|
14
|
-
param :state, default: "North Carolina", formatter: :transform_state_code
|
|
15
|
-
|
|
16
|
-
def transform_state_code(val)
|
|
17
|
-
val == "SC" ? "South Carolina" : val
|
|
18
|
-
end
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
nested_hash :phone do
|
|
22
|
-
boolean_param :cell_phone, default: true
|
|
23
|
-
string_param :phone_number, validations: { length: { in: 7..10 } }, formatter: lambda { |params, attribute| attribute.gsub(/\D/, "") }
|
|
24
|
-
string_param :area_code, default: lambda { |params, param|
|
|
25
|
-
if params.phone_number.present?
|
|
26
|
-
params.phone_number[0..2]
|
|
27
|
-
end
|
|
28
|
-
}
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
def lower_case_colors(val)
|
|
32
|
-
val.downcase
|
|
33
|
-
end
|
|
34
|
-
end
|
|
2
|
+
require 'fixtures/dummy_params'
|
|
35
3
|
|
|
36
4
|
describe SimpleParams::Params do
|
|
37
5
|
describe "strict parameter enforcement" do
|
|
@@ -76,7 +44,7 @@ describe SimpleParams::Params do
|
|
|
76
44
|
params.name = "Tom"
|
|
77
45
|
params.name.should eq("Tom")
|
|
78
46
|
end
|
|
79
|
-
|
|
47
|
+
|
|
80
48
|
it "has getter and setter methods for optional param" do
|
|
81
49
|
params.should respond_to(:age)
|
|
82
50
|
params.name.should be_nil
|
|
@@ -91,7 +59,7 @@ describe SimpleParams::Params do
|
|
|
91
59
|
params.address.street = "1 Main St."
|
|
92
60
|
params.address.street.should eq("1 Main St.")
|
|
93
61
|
end
|
|
94
|
-
|
|
62
|
+
|
|
95
63
|
it "has getter and setter methods for optional param" do
|
|
96
64
|
params.address.should respond_to(:zip_code)
|
|
97
65
|
params.address.zip_code.should be_nil
|
|
@@ -102,13 +70,13 @@ describe SimpleParams::Params do
|
|
|
102
70
|
end
|
|
103
71
|
|
|
104
72
|
describe "array syntax", array_syntax: true do
|
|
105
|
-
let(:params) do
|
|
73
|
+
let(:params) do
|
|
106
74
|
DummyParams.new(
|
|
107
|
-
name: "Bill",
|
|
108
|
-
age: 30,
|
|
109
|
-
address: {
|
|
110
|
-
city: "Greenville"
|
|
111
|
-
}
|
|
75
|
+
name: "Bill",
|
|
76
|
+
age: 30,
|
|
77
|
+
address: {
|
|
78
|
+
city: "Greenville"
|
|
79
|
+
}
|
|
112
80
|
)
|
|
113
81
|
end
|
|
114
82
|
|
|
@@ -163,13 +131,13 @@ describe SimpleParams::Params do
|
|
|
163
131
|
it "coerces nested attributes on initialization" do
|
|
164
132
|
params = DummyParams.new(address: { zip_code: 90210 })
|
|
165
133
|
params.address.zip_code.should eq("90210")
|
|
166
|
-
end
|
|
134
|
+
end
|
|
167
135
|
|
|
168
136
|
it "coerces nested attributes from setters" do
|
|
169
137
|
params = DummyParams.new
|
|
170
138
|
params.address.zip_code = 90210
|
|
171
139
|
params.address.zip_code.should eq("90210")
|
|
172
|
-
end
|
|
140
|
+
end
|
|
173
141
|
end
|
|
174
142
|
|
|
175
143
|
describe "defaults", defaults: true do
|
|
@@ -312,4 +280,31 @@ describe SimpleParams::Params do
|
|
|
312
280
|
end
|
|
313
281
|
end
|
|
314
282
|
end
|
|
315
|
-
|
|
283
|
+
|
|
284
|
+
describe "api_pie_documentation", api_pie_documentation: true do
|
|
285
|
+
it "generates valida api_pie documentation" do
|
|
286
|
+
documentation = DummyParams.api_pie_documentation
|
|
287
|
+
api_docs = <<-API_PIE_DOCS
|
|
288
|
+
param :name, String, desc: '', required: true
|
|
289
|
+
param :age, Integer, desc: ''
|
|
290
|
+
param :first_initial, String, desc: '', required: true
|
|
291
|
+
param :amount, desc: ''
|
|
292
|
+
param :color, String, desc: '', required: true
|
|
293
|
+
param :address, Hash, desc: '', required: true do
|
|
294
|
+
param :street, String, desc: '', required: true
|
|
295
|
+
param :city, String, desc: '', required: true
|
|
296
|
+
param :zip_code, String, desc: ''
|
|
297
|
+
param :state, String, desc: '', required: true
|
|
298
|
+
end
|
|
299
|
+
param :phone, Hash, desc: '', required: true do
|
|
300
|
+
param :cell_phone, desc: '', required: true
|
|
301
|
+
param :phone_number, String, desc: '', required: true
|
|
302
|
+
param :area_code, String, desc: '', required: true
|
|
303
|
+
end
|
|
304
|
+
API_PIE_DOCS
|
|
305
|
+
|
|
306
|
+
expect(documentation).to be_a String
|
|
307
|
+
expect(documentation.gsub(/\s+/, "")).to eq api_docs.gsub(/\s+/, "")
|
|
308
|
+
end
|
|
309
|
+
end
|
|
310
|
+
end
|
data/spec/spec_helper.rb
CHANGED
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.2.
|
|
4
|
+
version: 0.0.2.pre5
|
|
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-04-01 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: activemodel
|
|
@@ -86,6 +86,20 @@ dependencies:
|
|
|
86
86
|
- - ~>
|
|
87
87
|
- !ruby/object:Gem::Version
|
|
88
88
|
version: '2.6'
|
|
89
|
+
- !ruby/object:Gem::Dependency
|
|
90
|
+
name: pry
|
|
91
|
+
requirement: !ruby/object:Gem::Requirement
|
|
92
|
+
requirements:
|
|
93
|
+
- - ! '>='
|
|
94
|
+
- !ruby/object:Gem::Version
|
|
95
|
+
version: '0'
|
|
96
|
+
type: :development
|
|
97
|
+
prerelease: false
|
|
98
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
99
|
+
requirements:
|
|
100
|
+
- - ! '>='
|
|
101
|
+
- !ruby/object:Gem::Version
|
|
102
|
+
version: '0'
|
|
89
103
|
description: Simple way to specify API params
|
|
90
104
|
email:
|
|
91
105
|
- bryce.senz@gmail.com
|
|
@@ -100,6 +114,8 @@ files:
|
|
|
100
114
|
- Gemfile.lock
|
|
101
115
|
- README.md
|
|
102
116
|
- lib/simple_params.rb
|
|
117
|
+
- lib/simple_params/api_pie_doc.rb
|
|
118
|
+
- lib/simple_params/api_pie_doc/attribute.rb
|
|
103
119
|
- lib/simple_params/attribute.rb
|
|
104
120
|
- lib/simple_params/errors.rb
|
|
105
121
|
- lib/simple_params/formatter.rb
|
|
@@ -109,8 +125,11 @@ files:
|
|
|
109
125
|
- lib/simple_params/version.rb
|
|
110
126
|
- simple_params.gemspec
|
|
111
127
|
- spec/acceptance_spec.rb
|
|
128
|
+
- spec/api_pie_doc/attribute_spec.rb
|
|
129
|
+
- spec/api_pie_doc_spec.rb
|
|
112
130
|
- spec/attribute_spec.rb
|
|
113
131
|
- spec/errors_spec.rb
|
|
132
|
+
- spec/fixtures/dummy_params.rb
|
|
114
133
|
- spec/formatter_spec.rb
|
|
115
134
|
- spec/params_spec.rb
|
|
116
135
|
- spec/spec_helper.rb
|
|
@@ -140,8 +159,11 @@ specification_version: 4
|
|
|
140
159
|
summary: A DSL for specifying params, including type coercion and validation
|
|
141
160
|
test_files:
|
|
142
161
|
- spec/acceptance_spec.rb
|
|
162
|
+
- spec/api_pie_doc/attribute_spec.rb
|
|
163
|
+
- spec/api_pie_doc_spec.rb
|
|
143
164
|
- spec/attribute_spec.rb
|
|
144
165
|
- spec/errors_spec.rb
|
|
166
|
+
- spec/fixtures/dummy_params.rb
|
|
145
167
|
- spec/formatter_spec.rb
|
|
146
168
|
- spec/params_spec.rb
|
|
147
169
|
- spec/spec_helper.rb
|