simple_params 1.0.1 → 1.0.2

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
- ZTliMTVhZjE0OTJmN2M3MDgxYTJkZjQ2NTZmNTc1NWRmYzkyOWFmOQ==
4
+ ZmI5M2JjM2JhNGYxNjMwZmM4MGZmODdiZjM3ZDU4MGNmYWVjZWQxMA==
5
5
  data.tar.gz: !binary |-
6
- MzZjMDI5OTIyZjgzMjkzMDI0MGQ2ODAwMDc3ODZmYjBhMzkzMTBkMA==
6
+ OTA3NDYyNWZhMmUxODA5ZTc4ZjRkZGJiMjQ3MzQzZDdiZDI2YTY0NQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- MTJhNGIxYjEyNzg5ZDgyZmUwYzk2YjcyYTAwZDUxY2M3NTExZjY2MTUyZDc5
10
- MGQ1ZWViODEzZjJlMGFjOWI4ODAwOTVhYjNlZWE4ZDYzY2RlMmIzZTVkOTZh
11
- MjRhNjIyZDNjMzBlZmI3YmFjM2Q5YmVmYjE1NWIxMzdjYWJiMzg=
9
+ YTc5NWUzOTk0MmNiZTc2ZTY1MTk5OTgwYzg5YWI4ZWU3MTM1ODAzMzM1OWVm
10
+ OTA1MGM4OGJhYzYwNmNjNDJiZGRhMWEyYTM5YTRjNGI0OTJlOTRiMDJiMGU5
11
+ OGE2YTY0MzM2ODY4NmU0MzQ5Nzc4ZTVjYjcxM2JkNmE2ZjVlYmU=
12
12
  data.tar.gz: !binary |-
13
- MDAyODRiN2ZhZGY5YTg5NjM2M2NjMTZhZGFjYjhjN2EyMDkzZGM0NTgyZWIx
14
- M2JjZjMwODU2YTFmOTkzNDAzYTQwY2RiZWM4NGRiYWJjZmM5MTQ2MTM2YWFj
15
- ZDM3NjJlMmVmNDY3MDZiMmU4MDgxODhlYjAwZjMwNzI3Y2NlNjk=
13
+ ZDMyYzYxZjU5MmViYTdhNWE3MDczYzAzMmIyODY1NGY1MGQxZDhhMmMzY2Q5
14
+ M2E1MmQ1MmMzZTFhMDRjNjQxZWM3ZDRjZjljZGQ2YmYzYzc2YTIzNDAyODdk
15
+ OGQyNWExMjAzMTEzOTAyNzQ2ZGFiMjRkOGYxYzFiNTVjYjljM2Q=
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- simple_params (1.0.0)
4
+ simple_params (1.0.1)
5
5
  activemodel (>= 3.0, < 5.0)
6
6
  shoulda-matchers (~> 2.8)
7
7
  virtus (>= 1.0.0)
@@ -103,6 +103,9 @@ module SimpleParams
103
103
  def define_nested_class(name, options, &block)
104
104
  klass_name = name.to_s.split('_').collect(&:capitalize).join
105
105
  Class.new(Params).tap do |klass|
106
+ # def self.model_name
107
+ # ActiveModel::Name.new(self)
108
+ # end
106
109
  klass.class_eval(&block)
107
110
  klass.class_eval("self.options = #{options}")
108
111
  self.const_set(klass_name, klass)
@@ -217,8 +220,12 @@ module SimpleParams
217
220
  def define_anonymous_class(name, hash)
218
221
  klass_name = name.to_s.split('_').collect(&:capitalize).join
219
222
  anonymous_klass = Class.new(Params).tap do |klass|
220
- # Have to undefine, then redefine, this const in order to avoid warnings.
221
- self.class.send(:remove_const, klass_name) if self.class.const_defined?(klass_name)
223
+ if self.class.const_defined?(klass_name)
224
+ begin
225
+ self.class.send(:remove_const, klass_name)
226
+ rescue NameError
227
+ end
228
+ end
222
229
  self.class.const_set(klass_name, klass)
223
230
  end
224
231
  anonymous_klass.allow_undefined_params
@@ -5,13 +5,14 @@ module SimpleParams
5
5
  end
6
6
 
7
7
  class OptionalParameterMatcher < ValidationMatcher
8
- attr_accessor :default_value, :attribute, :allowed_values
8
+ attr_accessor :default_value, :attribute, :allowed_values, :disallowed_values
9
9
 
10
10
  def initialize(attribute)
11
11
  super(attribute)
12
12
  @default_value = nil
13
13
  @attribute = attribute
14
14
  @allowed_values = nil
15
+ @disallowed_values = nil
15
16
  end
16
17
 
17
18
  def with_default(value)
@@ -24,6 +25,11 @@ module SimpleParams
24
25
  self
25
26
  end
26
27
 
28
+ def with_disallowed_values(*values)
29
+ @disallowed_values = values
30
+ self
31
+ end
32
+
27
33
  def matches?(subject)
28
34
  super(subject)
29
35
 
@@ -31,6 +37,8 @@ module SimpleParams
31
37
  matches_default_value?
32
38
  elsif @allowed_values
33
39
  allows_value_of(nil) && matches_allowed_values?
40
+ elsif @disallowed_values
41
+ allows_value_of(nil) && matches_disallowed_values?
34
42
  else
35
43
  allows_value_of(nil)
36
44
  end
@@ -60,6 +68,12 @@ module SimpleParams
60
68
  allows_value_of(value)
61
69
  end
62
70
  end
71
+
72
+ def matches_disallowed_values?
73
+ disallowed_values.all? do |value|
74
+ disallows_value_of(value)
75
+ end
76
+ end
63
77
  end
64
78
  end
65
79
  end
@@ -5,13 +5,14 @@ module SimpleParams
5
5
  end
6
6
 
7
7
  class RequiredParameterMatcher < ValidationMatcher
8
- attr_accessor :default_value, :attribute, :allowed_values
8
+ attr_accessor :default_value, :attribute, :allowed_values, :disallowed_values
9
9
 
10
10
  def initialize(attribute)
11
11
  super(attribute)
12
12
  @default_value = nil
13
13
  @attribute = attribute
14
14
  @allowed_values = nil
15
+ @disallowed_values = nil
15
16
  end
16
17
 
17
18
  def with_default(value)
@@ -24,6 +25,11 @@ module SimpleParams
24
25
  self
25
26
  end
26
27
 
28
+ def with_disallowed_values(*values)
29
+ @disallowed_values = values
30
+ self
31
+ end
32
+
27
33
  def matches?(subject)
28
34
  super(subject)
29
35
  @expected_message ||= :blank
@@ -32,6 +38,8 @@ module SimpleParams
32
38
  matches_default_value?
33
39
  elsif @allowed_values
34
40
  disallows_value_of(nil) && matches_allowed_values?
41
+ elsif @disallowed_values
42
+ matches_disallowed_values?
35
43
  else
36
44
  disallows_value_of(nil, @expected_message)
37
45
  end
@@ -60,6 +68,12 @@ module SimpleParams
60
68
  allows_value_of(value)
61
69
  end
62
70
  end
71
+
72
+ def matches_disallowed_values?
73
+ disallowed_values.all? do |value|
74
+ disallows_value_of(value)
75
+ end
76
+ end
63
77
  end
64
78
  end
65
79
  end
@@ -1,3 +1,3 @@
1
1
  module SimpleParams
2
- VERSION = "1.0.1"
2
+ VERSION = "1.0.2"
3
3
  end
@@ -1,6 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  class AcceptanceParams < SimpleParams::Params
4
+ allow_undefined_params
4
5
  param :reference, type: :object, optional: true
5
6
  param :name
6
7
  param :age, type: :integer, optional: true, validations: { inclusion: { in: 18..100 } }
@@ -23,6 +24,14 @@ class AcceptanceParams < SimpleParams::Params
23
24
  end
24
25
 
25
26
  describe SimpleParams::Params do
27
+ describe "model_name", model_name: true do
28
+ it "has an ActiveModel name" do
29
+ params = AcceptanceParams.new
30
+ params.class.model_name.should be_a(ActiveModel::Name)
31
+ params.class.model_name.to_s.should eq("AcceptanceParams")
32
+ end
33
+ end
34
+
26
35
  describe "original_params", original_params: true do
27
36
  it "returns symbolized params hash" do
28
37
  params = AcceptanceParams.new(name: "Tom", address: { "street" => "1 Main St."} )
@@ -49,6 +58,12 @@ describe SimpleParams::Params do
49
58
  name.should eq("AcceptanceParams::Address")
50
59
  end
51
60
 
61
+ it "has correct model_name" do
62
+ nested = AcceptanceParams.new.address
63
+ name = nested.class.model_name.to_s
64
+ name.should eq("AcceptanceParams::Address")
65
+ end
66
+
52
67
  it "names nested class model_class correctly" do
53
68
  nested = AcceptanceParams.new.address
54
69
  name = nested.class.name
@@ -243,6 +258,26 @@ describe SimpleParams::Params do
243
258
  end
244
259
  end
245
260
 
261
+ describe "anonymous params", anonymous_params: true do
262
+ it "accepts anonymous params with simple values" do
263
+ params = AcceptanceParams.new(random: "some_other_value")
264
+ params.random.should eq("some_other_value")
265
+ end
266
+
267
+ it "accepts anonymous params hashes and creates Params class" do
268
+ params = AcceptanceParams.new(random: { a: "1", b: "2"})
269
+ params.random.should be_a(SimpleParams::Params)
270
+ params.random.a.should eq("1")
271
+ params.random.b.should eq("2")
272
+ end
273
+
274
+ it "accepts anonymous params hashes and names class correctly" do
275
+ params = AcceptanceParams.new(random: { a: "1", b: "2"})
276
+ params.random.class.name.to_s.should eq("AcceptanceParams::Random")
277
+ params.random.class.model_name.to_s.should eq("AcceptanceParams::Random")
278
+ end
279
+ end
280
+
246
281
  describe "api_pie_documentation", api_pie_documentation: true do
247
282
  it "generates valida api_pie documentation" do
248
283
  documentation = AcceptanceParams.api_pie_documentation
@@ -7,6 +7,7 @@ describe SimpleParams::ValidationMatchers::OptionalParameterMatcher do
7
7
  param :title, optional: true, default: "programmer"
8
8
  param :account_type, default: "checking", validations: { inclusion: { in: ["checking", "savings"] }}
9
9
  param :account_status, optional: true, validations: { inclusion: { in: ["active", "inactive"] }}
10
+ param :username, type: :string, default: "test", validations: { exclusion: { in: ['admin', 'demo'] } }
10
11
  end
11
12
 
12
13
  subject { OptionalParameterMatcher.new }
@@ -16,4 +17,7 @@ describe SimpleParams::ValidationMatchers::OptionalParameterMatcher do
16
17
  it { should have_optional_parameter(:title).with_default("programmer") }
17
18
  it { should have_optional_parameter(:account_status).with_allowed_values("active", "inactive") }
18
19
  it { should have_optional_parameter(:account_type).with_default("checking").with_allowed_values("checking", "savings") }
20
+ it { should have_optional_parameter(:account_type).with_disallowed_values("admin", "demo") }
21
+ it { should have_optional_parameter(:username).with_default("test").with_allowed_values("myuser", "kitten") }
22
+ it { should have_optional_parameter(:username).with_disallowed_values("admin", "demo") }
19
23
  end
@@ -7,6 +7,7 @@ describe SimpleParams::ValidationMatchers::RequiredParameterMatcher do
7
7
  param :title, default: "programmer"
8
8
  param :account_type, validations: { inclusion: { in: ["checking", "savings"] }}
9
9
  param :account_status, default: "active", validations: { inclusion: { in: ["active", "inactive"] }}
10
+ param :username, type: :string, validations: { exclusion: { in: ['admin', 'demo'] } }
10
11
  end
11
12
 
12
13
  subject { RequiredParameterMatcherTestClass.new }
@@ -17,4 +18,7 @@ describe SimpleParams::ValidationMatchers::RequiredParameterMatcher do
17
18
  it { should have_required_parameter(:title).with_default("programmer") }
18
19
  it { should have_required_parameter(:account_type).with_allowed_values("checking", "savings") }
19
20
  it { should have_required_parameter(:account_status).with_default("active").with_allowed_values("active", "inactive") }
21
+ it { should have_required_parameter(:account_status).with_disallowed_values("admin", "demo") }
22
+ it { should have_required_parameter(:username).with_allowed_values("myuser", "kitten") }
23
+ it { should have_required_parameter(:username).with_disallowed_values("admin", "demo") }
20
24
  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.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - brycesenz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-08 00:00:00.000000000 Z
11
+ date: 2015-06-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel