weak_parameters 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8c85836d4a9c0cc561b4f3843fc393d0a48d4ccf
4
- data.tar.gz: d69dafbf77051a318e6bbac744436c9899050150
3
+ metadata.gz: 574d6b069ecee483c92abe98a1f0aeefc6c77ce1
4
+ data.tar.gz: 3f69d7e525b372617d94ab264dcaaa69602943df
5
5
  SHA512:
6
- metadata.gz: 14c03ed3af01ed9297b76ab93db448b72a8ffa53c1dcbc8ff6a8967641b30d9c8bd33882bc6e74cd350c9bc4f194cf87f6b1a26830a34352d8af379d9026a1b1
7
- data.tar.gz: 02f0e3e72b22e16075066a11a04a5f128e17108407dc748bbb3acc4b3c64ad4bb5377da4094955aeea5edbec3feeb1654e73d10674d64f812edef76707e54186
6
+ metadata.gz: cc55b3b93a4fd17d76fe10d17c011158c69439aa2d7f79c512588d7439122542014bb7372096e583e8f1db739ff566ef95c476757e4c8398d332c829798ff376
7
+ data.tar.gz: f1577e70e84ffdbe6bd96dd03cf85cae30f733736da65628d2e2f387ea96a73c8a9ed7484f0b4e1ea77829c92b6b1a41b3248b108d5015898483d2c7cae63bef
data/README.md CHANGED
@@ -11,8 +11,8 @@ gem "weak_parameters"
11
11
  # WeakParameters provides `validates` class method to define validations.
12
12
  class RecipesController < ApplicationController
13
13
  validates :create do
14
- string :name, required: true
15
- integer :type
14
+ string :name, required: true, except: ["charlie", "dave"]
15
+ integer :type, only: "1".."3"
16
16
  end
17
17
 
18
18
  def create
@@ -9,7 +9,7 @@ module WeakParameters
9
9
  end
10
10
 
11
11
  def validate
12
- raise_error if required? && nil? || exist? && invalid_type?
12
+ raise_error unless valid?
13
13
  end
14
14
 
15
15
  def required?
@@ -22,6 +22,19 @@ module WeakParameters
22
22
 
23
23
  private
24
24
 
25
+ def valid?
26
+ case
27
+ when required? && nil?
28
+ false
29
+ when exist? && invalid_type?
30
+ false
31
+ when exist? && exceptional?
32
+ false
33
+ else
34
+ true
35
+ end
36
+ end
37
+
25
38
  def nil?
26
39
  value.nil?
27
40
  end
@@ -30,6 +43,17 @@ module WeakParameters
30
43
  !nil?
31
44
  end
32
45
 
46
+ def exceptional?
47
+ case
48
+ when options[:only].try(:exclude?, value)
49
+ true
50
+ when options[:except].try(:include?, value)
51
+ true
52
+ else
53
+ false
54
+ end
55
+ end
56
+
33
57
  def value
34
58
  params[key]
35
59
  end
@@ -1,3 +1,3 @@
1
1
  module WeakParameters
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -1,7 +1,7 @@
1
1
  class RecipesController < ApplicationController
2
2
  validates :create do
3
- string :name, required: true
4
- integer :type
3
+ string :name, required: true, except: %w[invalid wrong]
4
+ integer :type, only: "1".."3"
5
5
  boolean :flag
6
6
  hash :config
7
7
  array :tags
@@ -78,6 +78,28 @@ describe "Recipes" do
78
78
  end
79
79
  end
80
80
 
81
+ context "with invalid param to :only condition" do
82
+ before do
83
+ params[:type] = 0
84
+ end
85
+
86
+ it "returns 400" do
87
+ post "/recipes", params
88
+ response.status.should == 400
89
+ end
90
+ end
91
+
92
+ context "with invalid param to :except condition" do
93
+ before do
94
+ params[:name] = "invalid"
95
+ end
96
+
97
+ it "returns 400" do
98
+ post "/recipes", params
99
+ response.status.should == 400
100
+ end
101
+ end
102
+
81
103
  context "with valid condition", :autodoc do
82
104
  it "creates a new recipe" do
83
105
  post "/recipes", params
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: weak_parameters
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryo Nakamura
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-06-10 00:00:00.000000000 Z
11
+ date: 2013-06-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails