simple_params 1.5.2 → 1.6.0

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
- NDE2OTkyZTFiMjYzYTQ1NmVlNzI4OWUyNzM3MGMyZjEyNzRjOGUyNg==
4
+ NjA4YzU5ZjQ2NTRjMmYwNmViZWQ3N2ZhZTM3ZTE2Y2Q4ZGQyMWViNA==
5
5
  data.tar.gz: !binary |-
6
- NTMxMGU3ZWE5NTY4ZmI2OGMyZDk0ZjZiOTIxNGJmMmI0NjgxNjRhMw==
6
+ M2FkYjI0NzcxZjgzODA0Y2JhZDcyODM4MzZkYzM1YWUyNjM4MjI5ZA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- OTJmMzRhNTFlOWI0YjAwZjU1N2E1NDhjYTkzZWJjZTczMWMxODQyOWRkNmYw
10
- NjRhZjU2OTNlNDI4MzAwZGM0ZTJiMTNhMWEyMDEzODE0MzBkZWMxZmE0YTA0
11
- ZjI0MmJhNGY1MWZkZGE2NTQwMDZkMWZkNGQyNGE1NzE3YTdhZTY=
9
+ YmM2MmNiMDg5YmY5YzUxNGIwMjU1MzU4NGIyMDdjZjkxZjE0MDgxOTFmYzM1
10
+ YWM4NWE0MzkzNmFhOTFmNDk4ZDlkNjJlOTYwZTEwMmNjOGFiYjFjOThlZjY0
11
+ YjYzNGEyYTdhNzIyNmEwMzY0YmUyYWVhMzhlNjRhZmQ5ZmE1ZmQ=
12
12
  data.tar.gz: !binary |-
13
- ZGZkZmI0NmI4NzY3MWJhYWQwNjRiZDk1NDBkOWNjNDk2ODc3OTFmYzQwYzIz
14
- N2JmNDk5MTZkYzZiNDE5MjI2ZDJhYTc5ZTFlODk3MmJjMjllZTA5NzBmNDYy
15
- NzY2NTJhNWMzZTc5N2FlMzRiM2YxMWRlNjUwZjkxM2U0OTJjOTk=
13
+ OTdlMjA1ZmM1MjliODZkYzEyYTdhNzYyNjdkZWRjMWZjZTY1NjVhYjllYTM2
14
+ NmQyMDI4ZjEwY2I0MWMxMThjNmJjMjUwYzQ2NzhiZjRjYjQxODdiMzMwMmRm
15
+ YzgzNDZjMTY5YWUyMmZmYjVhNWM4MWM1OGQ5ZTM3ZTY4ZmQzNGE=
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- simple_params (1.5.1)
4
+ simple_params (1.5.2)
5
5
  activemodel (>= 3.0, < 5.0)
6
6
  shoulda-matchers (~> 2.8)
7
7
  virtus (>= 1.0.0)
@@ -1,6 +1,7 @@
1
1
  module SimpleParams
2
2
  module HasAttributes
3
3
  extend ActiveSupport::Concern
4
+
4
5
  included do
5
6
  def attributes
6
7
  (defined_attributes.keys + nested_classes.keys).flatten
@@ -30,8 +30,8 @@ module SimpleParams
30
30
  end
31
31
 
32
32
  def add_validations(name, opts = {})
33
- validation_string = ValidationBuilder.new(name, opts).validation_string
34
- eval(validation_string)
33
+ validations = ValidationBuilder.new(opts).build
34
+ validates name, validations unless validations.empty?
35
35
  end
36
36
 
37
37
  def nested_classes
@@ -1,31 +1,33 @@
1
1
  module SimpleParams
2
2
  class ValidationBuilder
3
- def initialize(name, opts={})
4
- @name = name
3
+ def initialize(opts={})
5
4
  @opts = opts
5
+ @validations = opts[:validations] || {}
6
6
  end
7
7
 
8
- def validation_string
9
- validations = @opts[:validations] || {}
10
- has_default = @opts.has_key?(:default) # checking has_key? because :default may be nil
11
- optional = @opts[:optional]
12
- if !validations.empty?
13
- if optional || has_default
14
- validations.merge!(allow_nil: true)
15
- else
16
- validations.merge!(presence: true)
8
+ def build
9
+ if allow_nil?
10
+ unless @validations.empty?
11
+ @validations.merge!(allow_nil: true)
17
12
  end
18
13
  else
19
- if !optional && !has_default
20
- validations.merge!(presence: true)
21
- end
14
+ @validations.merge!(presence: true)
22
15
  end
23
16
 
24
- if validations.empty?
25
- return ''
26
- else
27
- return "validates :#{@name.to_sym}, #{validations}"
28
- end
17
+ @validations
18
+ end
19
+
20
+ private
21
+ def has_default?
22
+ @opts.has_key?(:default)
23
+ end
24
+
25
+ def optional?
26
+ !!@opts[:optional]
27
+ end
28
+
29
+ def allow_nil?
30
+ optional? || has_default?
29
31
  end
30
32
  end
31
33
  end
@@ -1,3 +1,3 @@
1
1
  module SimpleParams
2
- VERSION = "1.5.2"
2
+ VERSION = "1.6.0"
3
3
  end
@@ -6,6 +6,13 @@ class AcceptanceParams < SimpleParams::Params
6
6
  param :reference, type: :object, optional: true
7
7
  param :name
8
8
  param :date_of_birth, type: :date, optional: true
9
+ param :content, optional: true, validations: { length: {
10
+ minimum: 20,
11
+ maximum: 40,
12
+ tokenizer: lambda { |str| str.split(/\s+/) },
13
+ too_short: "must have at least %{count} words",
14
+ too_long: "must have at most %{count} words"
15
+ } }
9
16
  param :current_time, type: :datetime, optional: true
10
17
  param :age, type: :integer, optional: true, validations: { inclusion: { in: 18..100 } }
11
18
  param :color, default: "red", validations: { inclusion: { in: ["red", "green"] }}
@@ -116,6 +123,7 @@ describe SimpleParams::Params do
116
123
  reference: nil,
117
124
  name: "Tom",
118
125
  date_of_birth: nil,
126
+ content: nil,
119
127
  current_time: nil,
120
128
  age: nil,
121
129
  color: "red",
@@ -179,6 +187,10 @@ describe SimpleParams::Params do
179
187
  "0" => { name: "Paws" },
180
188
  "1" => { name: "Turbo", _destroy: "1" },
181
189
  "2" => { name: "Felix" }
190
+ },
191
+ birds: {
192
+ "0" => { name: "Birdy" },
193
+ "1" => { name: "Tweety", _destroy: "1" }
182
194
  }
183
195
  }
184
196
  end
@@ -192,6 +204,10 @@ describe SimpleParams::Params do
192
204
  it "builds correct number of cats" do
193
205
  subject.cats.count.should eq(2)
194
206
  end
207
+
208
+ it "builds correct number of birds" do
209
+ subject.birds.count.should eq(1)
210
+ end
195
211
  end
196
212
  end
197
213
 
@@ -263,7 +279,7 @@ describe SimpleParams::Params do
263
279
  describe "attributes", attributes: true do
264
280
  it "returns array of attribute symbols" do
265
281
  params = AcceptanceParams.new
266
- params.attributes.should eq([:reference, :name, :date_of_birth, :current_time, :age, :color, :sibling_names, :address, :phone, :dogs, :cats, :birds])
282
+ params.attributes.should eq([:reference, :name, :date_of_birth, :content, :current_time, :age, :color, :sibling_names, :address, :phone, :dogs, :cats, :birds])
267
283
  end
268
284
 
269
285
  it "returns array of attribute symbols for nested class" do
@@ -626,6 +642,7 @@ describe SimpleParams::Params do
626
642
  param:reference, Object, desc:'', required: false
627
643
  param :name, String, desc: '', required: true
628
644
  param :date_of_birth, Date, desc: '', required: false
645
+ param:content,String,desc:'',required:false
629
646
  param :current_time, desc: '', required: false
630
647
  param :age, Integer, desc: '', required: false
631
648
  param :color, String, desc: '', required: false
@@ -1,29 +1,28 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe SimpleParams::ValidationBuilder do
4
- let(:name) { "my_attribute" }
5
-
6
4
  context "with blank opts" do
7
- let(:builder) { described_class.new(name) }
5
+ let(:builder) { described_class.new }
8
6
 
9
- it "has correct validation string" do
10
- builder.validation_string.should eq(
11
- 'validates :my_attribute, {:presence=>true}'
7
+ it "has correct validations" do
8
+ builder.build.should eq(
9
+ { presence: true }
12
10
  )
13
11
  end
14
12
  end
15
13
 
16
- context "with optional" do
14
+ context "with only optional" do
17
15
  let(:opts) do
18
16
  {
19
- optional: true
17
+ optional: true,
18
+ validations: nil
20
19
  }
21
20
  end
22
21
 
23
- let(:builder) { described_class.new(name, opts) }
22
+ let(:builder) { described_class.new(opts) }
24
23
 
25
- it "has correct validation string" do
26
- builder.validation_string.should eq('')
24
+ it "has correct validations" do
25
+ builder.build.should eq({})
27
26
  end
28
27
  end
29
28
 
@@ -34,25 +33,48 @@ describe SimpleParams::ValidationBuilder do
34
33
  }
35
34
  end
36
35
 
37
- let(:builder) { described_class.new(name, opts) }
36
+ let(:builder) { described_class.new(opts) }
38
37
 
39
- it "has correct validation string" do
40
- builder.validation_string.should eq('')
38
+ it "has correct validations" do
39
+ builder.build.should eq( {})
41
40
  end
42
41
  end
43
42
 
44
- context "with other validations" do
43
+ context "with other validations (simple)" do
45
44
  let(:opts) do
46
45
  {
47
46
  validations: { presence: true, length: { in: [0..20]} }
48
47
  }
49
48
  end
50
49
 
51
- let(:builder) { described_class.new(name, opts) }
50
+ let(:builder) { described_class.new(opts) }
51
+
52
+ it "has correct validations" do
53
+ builder.build.should eq(
54
+ { presence: true, length: { in: [0..20] } }
55
+ )
56
+ end
57
+ end
58
+
59
+ context "with other validations (complex)" do
60
+ let(:stored_proc) do
61
+ lambda { |str| str.split(/\s+/) }
62
+ end
63
+
64
+ let(:opts) do
65
+ {
66
+ validations: { presence: true, length: {
67
+ minimum: 2, tokenizer: stored_proc
68
+ }
69
+ }
70
+ }
71
+ end
72
+
73
+ let(:builder) { described_class.new(opts) }
52
74
 
53
- it "has correct validation string" do
54
- builder.validation_string.should eq(
55
- 'validates :my_attribute, {:presence=>true, :length=>{:in=>[0..20]}}'
75
+ it "has correct validations" do
76
+ builder.build.should eq(
77
+ { presence: true, length: { minimum: 2, tokenizer: stored_proc } }
56
78
  )
57
79
  end
58
80
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_params
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.2
4
+ version: 1.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - brycesenz