simple_params 0.0.2.pre7 → 0.0.2.pre8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- NGJkNjAzNzZhMWU3YmZjOGVhMGVlNjAxM2QyZjkwZDM2NjdiMDAxZA==
4
+ YjQ1MzU1MGIxNmU3ZTA4MGZhZGY5YTg4YjZmZTY0ZjI1NmE3MTU0OQ==
5
5
  data.tar.gz: !binary |-
6
- YTVlYWJiMmI1NjlmNWNlZjc4ZDdkZjIwNDY5NjFjNmNhODgxZDExZg==
6
+ ZmRiNWQ0ZjEwYjVmOWEzM2IzNGVhNTc1NTM1ODRjN2VlNDMwYTMwMA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- YmRiYTg4NDRiOGY0NmFjMjg1YmQ2ZTI1M2UzOTA4NDUxNmUwZjdiODNhY2Q1
10
- OTE4Mjk4OGY4ZmVlZTQ5ZTRmZTdiOTEzN2I0YjBlMDljMzIwMGVkZGM0Nzlm
11
- MmMxYzBkN2NmNzMwMzc3ZGE0MjdmZDFmM2JjMTFhNzUwNjNhMzc=
9
+ YmU0MzFlNTYwNDdkYTY2Y2YyOGFhYmJiZWJmZmQ1OTUyYWVjNDE5MjA2OWRl
10
+ ZmQzZjhlMDcxZTYxYzgxYzdkZjQwNzY0ZTJkNmIwNDE5NDdjMTRmYjYyMjMx
11
+ M2Y2NDJmZTIzYzczNDY2OTFkMzNjZjZjMmM2MzA3Yjk2MGNlOWI=
12
12
  data.tar.gz: !binary |-
13
- YmMxZGY0NjgwNDIwOWZiMTY0NGFmYTNmOWYxZTU1NjhiMGU4N2RkNWJmYWY1
14
- YTY4YjFlMzM4ZjAyMDcxYmIyODk5MzJmMzVjMzAyNmE4OWJkOTU4Mjk3Y2Iz
15
- MWZiMDUzYzIzMGQxMGNlZTYzM2Q0NmU5NjEwYTAwZTg1ZWNiYjU=
13
+ ZTg5MWUxNGFjY2VlMWNhMWNjN2EyNjRiZWI2MTEzMDgyOGIwODNjYTViNjI5
14
+ MzRjODdhNTYwMTc4ZThiY2Q4NmY3Njk3NWI5NDYwNjg4MmZlMmUyYzI2ODAw
15
+ ZjNjZWQ4NzQwZmIyZTlmYjEyOTJiNWQ1YjRiNjEzNzU2ODI4YTI=
data/.rspec CHANGED
@@ -1,2 +1,3 @@
1
1
  --color
2
2
  --format progress
3
+ --warnings
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- simple_params (0.0.2.pre6)
4
+ simple_params (0.0.2.pre7)
5
5
  activemodel (>= 3.0, < 5.0)
6
6
  virtus (>= 1.0.0)
7
7
 
@@ -1,52 +1,27 @@
1
1
  module SimpleParams
2
- class ApiPieDoc::Attribute
3
- attr_accessor :attribute, :nested, :options
4
-
5
- alias :nested? :nested
2
+ class ApiPieDoc::Attribute < ApiPieDoc::AttributeBase
6
3
 
7
4
  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]
5
+ super
6
+ self.options ||= attribute[1]
11
7
  end
12
8
 
13
9
  def name
14
- nested? ? attribute.keys.first.to_s : attribute[0].to_s
10
+ attribute[0].to_s
15
11
  end
16
12
 
17
13
  def to_s
18
14
  return nil if do_not_document?
19
-
20
- if nested?
21
- nested_description
22
- else
23
- attribute_description
24
- end
15
+ attribute_description
25
16
  end
26
17
 
27
18
  private
28
19
 
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
20
  def attribute_description
44
21
  base = "param :#{name}"
45
22
  [base, type_description, description, requirement_description].reject(&:blank?).join(", ")
46
23
  end
47
24
 
48
- NotValidValueError = Class.new(StandardError)
49
-
50
25
  def type_description
51
26
  value = options[:type]
52
27
  case value
@@ -60,22 +35,5 @@ module SimpleParams
60
35
  raise NotValidValueError.new("Must be one of #{SimpleParams::Params::TYPES}")
61
36
  end
62
37
  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
38
  end
81
39
  end
@@ -0,0 +1,34 @@
1
+ module SimpleParams
2
+ class ApiPieDoc::AttributeBase
3
+ attr_accessor :attribute, :options
4
+
5
+ def initialize(simple_params_attribute)
6
+ self.attribute = simple_params_attribute
7
+ end
8
+
9
+ private
10
+
11
+ def do_not_document?
12
+ options[:document].eql?(false)
13
+ end
14
+
15
+ NotValidValueError = Class.new(StandardError)
16
+
17
+ def requirement_description
18
+ value = options[:optional]
19
+ case value
20
+ when true
21
+ "required: false"
22
+ when false
23
+ "required: true"
24
+ else
25
+ "required: true"
26
+ end
27
+ end
28
+
29
+ def description
30
+ description = options[:desc] || ''
31
+ "desc: '#{description}'"
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,31 @@
1
+ module SimpleParams
2
+ class ApiPieDoc::NestedAttribute < ApiPieDoc::AttributeBase
3
+
4
+ attr_accessor :attributes
5
+
6
+ def initialize(simple_params_attribute)
7
+ super
8
+ self.attributes = attribute.values[0].map { |attribute| ApiPieDoc::Attribute.new(attribute) }
9
+ self.options ||= attribute.delete(:options) || attribute[1]
10
+ end
11
+
12
+ def name
13
+ attribute.keys.first.to_s
14
+ end
15
+
16
+ def to_s
17
+ return nil if do_not_document?
18
+ nested_description
19
+ end
20
+
21
+ private
22
+
23
+ def nested_description
24
+ start = "param :#{name}, Hash, #{description}, #{requirement_description} do"
25
+ attribute_descriptors = []
26
+ attributes.each { |attribute| attribute_descriptors << attribute.to_s }
27
+ finish = "end"
28
+ [start, attribute_descriptors, finish].flatten.join("\n")
29
+ end
30
+ end
31
+ end
@@ -21,7 +21,7 @@ module SimpleParams
21
21
  end
22
22
 
23
23
  nested_attributes.each do |nested_attribute|
24
- docs << Attribute.new(nested_attribute).to_s
24
+ docs << NestedAttribute.new(nested_attribute).to_s
25
25
  end
26
26
 
27
27
  docs.join("\n")
@@ -88,7 +88,7 @@ module SimpleParams
88
88
  self.messages.dup
89
89
  end
90
90
 
91
- nested_messages = @nested_attributes.map do |attribute|
91
+ @nested_attributes.map do |attribute|
92
92
  errors = nested_error_messages(attribute, full_messages)
93
93
  unless errors.empty?
94
94
  messages.merge!(attribute.to_sym => errors)
@@ -97,13 +97,18 @@ module SimpleParams
97
97
  messages
98
98
  end
99
99
 
100
+ def to_s(full_messages = false)
101
+ array = to_a
102
+ array.join(', ')
103
+ end
104
+
100
105
  private
101
106
  def nested_error_messages(attribute, full_messages = false)
102
107
  if fetch_nested_attribute(attribute)
103
108
  if full_messages
104
109
  errors = fetch_nested_attribute(attribute).errors
105
- errors.messages.each_with_object({}) do |(attribute, array), messages|
106
- messages[attribute] = array.map { |message| errors.full_message(attribute, message) }
110
+ errors.messages.each_with_object({}) do |(attr, array), messages|
111
+ messages[attr] = array.map { |message| errors.full_message(attr, message) }
107
112
  end
108
113
  else
109
114
  fetch_nested_attribute(attribute).errors.messages.dup
@@ -58,7 +58,7 @@ module SimpleParams
58
58
  alias_method :nested, :nested_hash
59
59
 
60
60
  def nested_hashes
61
- @nested_hashes || {}
61
+ @nested_hashes ||= {}
62
62
  end
63
63
 
64
64
  def defined_attributes
@@ -190,13 +190,13 @@ module SimpleParams
190
190
  end
191
191
 
192
192
  def define_anonymous_class(hash)
193
- klass = Class.new(Params).tap do |klass|
193
+ klass = Class.new(Params).tap do |k|
194
194
  name_function = Proc.new {
195
195
  def self.model_name
196
196
  ActiveModel::Name.new(self, nil, "temp")
197
197
  end
198
198
  }
199
- klass.class_eval(&name_function)
199
+ k.class_eval(&name_function)
200
200
  end
201
201
  klass.allow_undefined_params
202
202
  klass.new(hash)
@@ -1,3 +1,3 @@
1
1
  module SimpleParams
2
- VERSION = "0.0.2.pre7"
2
+ VERSION = "0.0.2.pre8"
3
3
  end
data/lib/simple_params.rb CHANGED
@@ -6,4 +6,6 @@ require 'simple_params/validations'
6
6
  require 'simple_params/params'
7
7
  require 'simple_params/simple_params_error'
8
8
  require 'simple_params/api_pie_doc'
9
+ require 'simple_params/api_pie_doc/attribute_base'
9
10
  require 'simple_params/api_pie_doc/attribute'
11
+ require 'simple_params/api_pie_doc/nested_attribute'
@@ -157,14 +157,14 @@ describe SimpleParams::Params do
157
157
  it "generates valida api_pie documentation" do
158
158
  documentation = AcceptanceParams.api_pie_documentation
159
159
  api_docs = <<-API_PIE_DOCS
160
- param:reference, Object, desc:''
160
+ param:reference, Object, desc:'', required: false
161
161
  param :name, String, desc: '', required: true
162
- param :age, Integer, desc: ''
162
+ param :age, Integer, desc: '', required: false
163
163
  param :color, String, desc: '', required: true
164
164
  param :address, Hash, desc: '', required: true do
165
165
  param :street, String, desc: '', required: true
166
166
  param :city, String, desc: '', required: true
167
- param :zip_code, String, desc: ''
167
+ param :zip_code, String, desc: '', required: false
168
168
  param :state, String, desc: '', required: true
169
169
  end
170
170
  API_PIE_DOCS
@@ -2,83 +2,35 @@ require 'spec_helper'
2
2
 
3
3
  describe SimpleParams::ApiPieDoc::Attribute do
4
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
5
  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
6
 
18
- describe '#initialize' do
7
+ it_behaves_like 'a base attribute'
19
8
 
9
+ describe '#initialize' do
20
10
  specify 'should give instance an attribute' do
21
11
  expect(api_pie_doc_attribute.attribute).to eq simple_param_attribute
22
12
  end
23
13
 
24
- specify 'should set nested' do
25
- expect(api_pie_doc_attribute.nested).to eq false
14
+ specify 'should give instance some options' do
15
+ expect(api_pie_doc_attribute.options).to eq({ type: :string })
26
16
  end
27
17
  end
28
18
 
29
19
  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
20
+ specify 'should set respond with the right name' do
21
+ expect(api_pie_doc_attribute.name).to eq 'name'
40
22
  end
41
23
  end
42
24
 
43
25
  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
26
+ specify 'should return the attributes options' do
27
+ expect(api_pie_doc_attribute.options).to eq({ type: :string })
68
28
  end
69
29
  end
70
30
 
71
31
  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
32
+ specify 'should return properly formatted string' do
33
+ expect(api_pie_doc_attribute.to_s).to eq("param :name, String, desc: '', required: true")
82
34
  end
83
35
  end
84
36
 
@@ -103,43 +55,4 @@ describe SimpleParams::ApiPieDoc::Attribute do
103
55
  end
104
56
  end
105
57
  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
58
  end
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+
3
+ describe SimpleParams::ApiPieDoc::NestedAttribute do
4
+
5
+ let(: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(:nested_attribute) { described_class.new(simple_param_attribute) }
16
+
17
+ it_behaves_like 'a base attribute'
18
+
19
+ describe '#initialize' do
20
+ specify 'should give instance an attribute' do
21
+ expect(nested_attribute.attribute).to eq simple_param_attribute
22
+ end
23
+
24
+ specify 'should give instance options' do
25
+ expect(nested_attribute.options).to eq({ desc: 'i like pie' })
26
+ end
27
+ end
28
+
29
+ describe '#name' do
30
+ specify 'should set respond with the right name' do
31
+ expect(nested_attribute.name).to eq 'address'
32
+ end
33
+ end
34
+
35
+ describe '#options' do
36
+ specify 'should return nested attribute options' do
37
+ expect(nested_attribute.options).to eq({desc: 'i like pie'})
38
+ end
39
+ end
40
+
41
+ describe '#to_s' do
42
+ specify 'should return properly formatted string' do
43
+ expect(nested_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: '', required: false\nparam :state, String, desc: '', required: true\nend")
44
+ end
45
+ end
46
+ end
data/spec/errors_spec.rb CHANGED
@@ -276,6 +276,22 @@ describe SimpleParams::Errors do
276
276
  end
277
277
  end
278
278
 
279
+ describe "#to_s" do
280
+ it "to_a returns the list of errors with complete messages containing the attribute names" do
281
+ person = Person.new
282
+ person.errors.add(:name, "can not be blank")
283
+ person.errors.add(:name, "can not be nil")
284
+ person.errors.to_s.should eq("name can not be blank, name can not be nil")
285
+ end
286
+
287
+ it "handles nested attributes" do
288
+ person = Person.new
289
+ person.errors.add(:name, "can not be blank")
290
+ person.dog.errors.add(:breed, "can not be nil")
291
+ person.errors.to_s.should eq("name can not be blank, dog breed can not be nil")
292
+ end
293
+ end
294
+
279
295
  describe "#to_hash" do
280
296
  it "to_hash returns the error messages hash" do
281
297
  person = Person.new
data/spec/params_spec.rb CHANGED
@@ -293,14 +293,14 @@ describe SimpleParams::Params do
293
293
  documentation = DummyParams.api_pie_documentation
294
294
  api_docs = <<-API_PIE_DOCS
295
295
  param :name, String, desc: '', required: true
296
- param :age, Integer, desc: ''
296
+ param :age, Integer, desc: '', required: false
297
297
  param :first_initial, String, desc: '', required: true
298
- param :amount, desc: ''
298
+ param :amount, desc: '', required: false
299
299
  param :color, String, desc: '', required: true
300
300
  param :address, Hash, desc: '', required: true do
301
301
  param :street, String, desc: '', required: true
302
302
  param :city, String, desc: '', required: true
303
- param :zip_code, String, desc: ''
303
+ param :zip_code, String, desc: '', required: false
304
304
  param :state, String, desc: '', required: true
305
305
  end
306
306
  param :phone, Hash, desc: '', required: true do
data/spec/spec_helper.rb CHANGED
@@ -3,7 +3,7 @@ require 'bundler/setup'
3
3
  require 'simple_params'
4
4
  require 'pry'
5
5
 
6
- Dir[("../spec/support/**/*.rb")].each {|f| require f}
6
+ Dir[File.join('.', 'spec', 'support', '**', '*.rb')].each {|f| require f}
7
7
 
8
8
  I18n.config.enforce_available_locales = true
9
9
 
@@ -0,0 +1,19 @@
1
+ shared_examples "a base attribute" do
2
+
3
+ let(:base_attribute) { described_class.new(simple_param_attribute) }
4
+
5
+ describe 'base_attribute' do
6
+ specify 'should respond to do_not_document' do
7
+ expect(base_attribute.respond_to?(:do_not_document?, true)).to be_truthy
8
+ end
9
+
10
+ specify 'should respond to description' do
11
+ expect(base_attribute.respond_to?(:requirement_description, true)).to be_truthy
12
+ end
13
+
14
+ specify 'should respond to description' do
15
+ expect(base_attribute.respond_to?(:description, true)).to be_truthy
16
+ end
17
+ end
18
+ end
19
+
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.pre7
4
+ version: 0.0.2.pre8
5
5
  platform: ruby
6
6
  authors:
7
7
  - brycesenz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-02 00:00:00.000000000 Z
11
+ date: 2015-04-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -116,6 +116,8 @@ files:
116
116
  - lib/simple_params.rb
117
117
  - lib/simple_params/api_pie_doc.rb
118
118
  - lib/simple_params/api_pie_doc/attribute.rb
119
+ - lib/simple_params/api_pie_doc/attribute_base.rb
120
+ - lib/simple_params/api_pie_doc/nested_attribute.rb
119
121
  - lib/simple_params/attribute.rb
120
122
  - lib/simple_params/errors.rb
121
123
  - lib/simple_params/formatter.rb
@@ -126,6 +128,7 @@ files:
126
128
  - simple_params.gemspec
127
129
  - spec/acceptance_spec.rb
128
130
  - spec/api_pie_doc/attribute_spec.rb
131
+ - spec/api_pie_doc/nested_attribute_spec.rb
129
132
  - spec/api_pie_doc_spec.rb
130
133
  - spec/attribute_spec.rb
131
134
  - spec/errors_spec.rb
@@ -133,6 +136,7 @@ files:
133
136
  - spec/formatter_spec.rb
134
137
  - spec/params_spec.rb
135
138
  - spec/spec_helper.rb
139
+ - spec/support/shared_examples/base_attribute.rb
136
140
  homepage: https://github.com/brycesenz/simple_params
137
141
  licenses:
138
142
  - MIT
@@ -160,6 +164,7 @@ summary: A DSL for specifying params, including type coercion and validation
160
164
  test_files:
161
165
  - spec/acceptance_spec.rb
162
166
  - spec/api_pie_doc/attribute_spec.rb
167
+ - spec/api_pie_doc/nested_attribute_spec.rb
163
168
  - spec/api_pie_doc_spec.rb
164
169
  - spec/attribute_spec.rb
165
170
  - spec/errors_spec.rb
@@ -167,3 +172,4 @@ test_files:
167
172
  - spec/formatter_spec.rb
168
173
  - spec/params_spec.rb
169
174
  - spec/spec_helper.rb
175
+ - spec/support/shared_examples/base_attribute.rb