weak_parameters 0.0.7 → 0.0.8

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: f1eda85eff3c4bef895416ca359efa1dd90db8f7
4
- data.tar.gz: 8ef1d0d971591b665066b042a32b88c530353ade
3
+ metadata.gz: f112bfd3d9b173060049795b5fc59f3453edc245
4
+ data.tar.gz: 3fd97028246f58ab071b810b5e239fba042d8074
5
5
  SHA512:
6
- metadata.gz: 8c15d0bcf364fb6dc89be768bd71bb9e6d3c96be1b2cd24b7d32f04749ef774b74741a6d6b606ee525734cf759768ac7882a0588be5b9b965ea1aeb7991b65fb
7
- data.tar.gz: d505f087124d6e89059dd9941e1a438e4d58527bda9d86e2b7ee6b158cbd81a9c9caae42b4f43c3f6c8bbb190e19324c147670bc58adacb604e1d9dec3bae5d4
6
+ metadata.gz: 534fce296c74c8af8e006cb63f953d6c2e594939054d63aa24ad0791402dcec12c56880e81d4f7732f40a13cd8bcdc1521ec0d08a1de52dbb21ba7de84d31d3a
7
+ data.tar.gz: 314aba9cb4a7561c2253c8d10c536a9e3e951e23bbb9673a90a3aef2ff7317922a035e7126ede67eb2f13fb6c09a541b929c21929dd7880f057e35806118610c
data/README.md CHANGED
@@ -13,6 +13,9 @@ class RecipesController < ApplicationController
13
13
  validates :create do
14
14
  string :name, required: true, except: ["charlie", "dave"]
15
15
  integer :type, only: 1..3
16
+ string :quantity do |value|
17
+ value =~ /\A\d+(?:\.\d+)g\z/
18
+ end
16
19
  end
17
20
 
18
21
  def create
@@ -36,6 +39,7 @@ irb(main):005:0> app.post "/recipes", name: "alice", type: "bob"
36
39
 
37
40
  ### Available validators
38
41
  * array
42
+ * float
39
43
  * hash
40
44
  * integer
41
45
  * string
@@ -4,6 +4,7 @@ require "active_support/hash_with_indifferent_access"
4
4
  require "weak_parameters/base_validator"
5
5
  require "weak_parameters/array_validator"
6
6
  require "weak_parameters/boolean_validator"
7
+ require "weak_parameters/float_validator"
7
8
  require "weak_parameters/hash_validator"
8
9
  require "weak_parameters/integer_validator"
9
10
  require "weak_parameters/string_validator"
@@ -1,11 +1,12 @@
1
1
  module WeakParameters
2
2
  class BaseValidator
3
- attr_reader :params, :key, :options
3
+ attr_reader :params, :key, :options, :block
4
4
 
5
- def initialize(params, key, options = {})
5
+ def initialize(params, key, options = {}, &block)
6
6
  @params = params
7
7
  @key = key
8
8
  @options = options
9
+ @block = block
9
10
  end
10
11
 
11
12
  def validate
@@ -30,6 +31,8 @@ module WeakParameters
30
31
  false
31
32
  when exist? && exceptional?
32
33
  false
34
+ when exist? && block && !block.call(value)
35
+ false
33
36
  else
34
37
  true
35
38
  end
@@ -0,0 +1,17 @@
1
+ module WeakParameters
2
+ class FloatValidator < WeakParameters::BaseValidator
3
+ def valid_type?
4
+ /\A-?\d+(?:\.\d+)?\z/ === params[key].to_s
5
+ end
6
+
7
+ def error_message
8
+ "params[#{key.inspect}] must be a Float"
9
+ end
10
+
11
+ private
12
+
13
+ def value
14
+ super.presence.try(:to_f)
15
+ end
16
+ end
17
+ end
@@ -17,24 +17,28 @@ module WeakParameters
17
17
 
18
18
  private
19
19
 
20
- def string(key, options = {})
21
- validators << WeakParameters::StringValidator.new(params, key, options)
20
+ def string(key, options = {}, &block)
21
+ validators << WeakParameters::StringValidator.new(params, key, options, &block)
22
22
  end
23
23
 
24
- def integer(key, options = {})
25
- validators << WeakParameters::IntegerValidator.new(params, key, options)
24
+ def integer(key, options = {}, &block)
25
+ validators << WeakParameters::IntegerValidator.new(params, key, options, &block)
26
26
  end
27
27
 
28
- def boolean(key, options = {})
29
- validators << WeakParameters::BooleanValidator.new(params, key, options)
28
+ def boolean(key, options = {}, &block)
29
+ validators << WeakParameters::BooleanValidator.new(params, key, options, &block)
30
30
  end
31
31
 
32
- def hash(key, options = {})
33
- validators << WeakParameters::HashValidator.new(params, key, options)
32
+ def hash(key, options = {}, &block)
33
+ validators << WeakParameters::HashValidator.new(params, key, options, &block)
34
34
  end
35
35
 
36
- def array(key, options = {})
37
- validators << WeakParameters::ArrayValidator.new(params, key, options)
36
+ def array(key, options = {}, &block)
37
+ validators << WeakParameters::ArrayValidator.new(params, key, options, &block)
38
+ end
39
+
40
+ def float(key, options = {})
41
+ validators << WeakParameters::FloatValidator.new(params, key, options, &block)
38
42
  end
39
43
  end
40
44
  end
@@ -1,3 +1,3 @@
1
1
  module WeakParameters
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  end
@@ -6,6 +6,10 @@ class RecipesController < ApplicationController
6
6
  boolean :flag
7
7
  hash :config
8
8
  array :tags
9
+ float :rate
10
+ string :zip_code do |value|
11
+ value =~ /\A\d{3}-\d{4}\z/
12
+ end
9
13
  end
10
14
 
11
15
  def create
@@ -9,6 +9,7 @@ describe "Recipes" do
9
9
  flag: true,
10
10
  config: {},
11
11
  tags: [],
12
+ zip_code: "123-4567",
12
13
  }
13
14
  end
14
15
 
@@ -79,6 +80,28 @@ describe "Recipes" do
79
80
  end
80
81
  end
81
82
 
83
+ context "with wrong float param" do
84
+ before do
85
+ params[:rate] = "-x"
86
+ end
87
+
88
+ it "returns 400" do
89
+ post "/recipes", params
90
+ response.status.should == 400
91
+ end
92
+ end
93
+
94
+ context "with block failure" do
95
+ before do
96
+ params[:zip_code] = "123-456"
97
+ end
98
+
99
+ it "returns 400" do
100
+ post "/recipes", params
101
+ response.status.should == 400
102
+ end
103
+ end
104
+
82
105
  context "without non-required param" do
83
106
  before do
84
107
  params.delete(:type)
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.7
4
+ version: 0.0.8
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-08-13 00:00:00.000000000 Z
11
+ date: 2013-08-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -83,6 +83,7 @@ files:
83
83
  - lib/weak_parameters/base_validator.rb
84
84
  - lib/weak_parameters/boolean_validator.rb
85
85
  - lib/weak_parameters/controller.rb
86
+ - lib/weak_parameters/float_validator.rb
86
87
  - lib/weak_parameters/hash_validator.rb
87
88
  - lib/weak_parameters/integer_validator.rb
88
89
  - lib/weak_parameters/string_validator.rb