weak_parameters 0.1.1 → 0.1.2

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: efcb865d817a6867afa772ec6f3dfe729edd1376
4
- data.tar.gz: 4e7b85548130ba79ea37ac9e01128a146a4a575a
3
+ metadata.gz: 26e7c722507f3503ec86ccf9125412e4ffa92d80
4
+ data.tar.gz: 2d12877fdf00a549ef15a33c3586d14b88dcc71e
5
5
  SHA512:
6
- metadata.gz: c67f0ed28ba05e9c3b0bbb95f1470647c984cb7382371aaf9d759fb393a600c46b1997043e258f68c76cffb91a4472a55aa33a765fbbe011893e3a36bd30c999
7
- data.tar.gz: 049f8b27c064d73b8bd07914393f1dba7ea73eec7e5d4541ab09630d9064e9b7b14f9abdf16c2f7fc5d1f515c589c2cbd3927df7340659336f8044ac9b0c42d2
6
+ metadata.gz: 618f2b0a8f715c18636238b4e279a0cef462098313a2b5fac7fda66aa6c54df3d292a67b75c4c76e8f98af7bb10cf105f14204cc02791f3f3af1c18e0063617e
7
+ data.tar.gz: 551e7ebf28056014418d229c41a9ac10b285fe8e30bc356a08b9f1b2a42f7336539f0672f3b440d2d08f09cd70796bd650162317cc2b8eb6abc7761bb0a93a86
@@ -1,16 +1,16 @@
1
1
  module WeakParameters
2
2
  class BaseValidator
3
- attr_reader :params, :key, :options, :block
3
+ attr_reader :controller, :key, :options, :block
4
4
 
5
- def initialize(params, key, options = {}, &block)
6
- @params = params
5
+ def initialize(controller, key, options = {}, &block)
6
+ @controller = controller
7
7
  @key = key
8
8
  @options = options
9
9
  @block = block
10
10
  end
11
11
 
12
12
  def validate
13
- raise_error unless valid?
13
+ handle_failure unless valid?
14
14
  end
15
15
 
16
16
  def required?
@@ -57,10 +57,22 @@ module WeakParameters
57
57
  end
58
58
  end
59
59
 
60
+ def params
61
+ controller.params
62
+ end
63
+
60
64
  def value
61
65
  params[key]
62
66
  end
63
67
 
68
+ def handle_failure
69
+ if has_handler?
70
+ controller.send(options[:handler])
71
+ else
72
+ raise_error
73
+ end
74
+ end
75
+
64
76
  def raise_error
65
77
  raise WeakParameters::ValidationError, error_message
66
78
  end
@@ -76,5 +88,9 @@ module WeakParameters
76
88
  def invalid_type?
77
89
  !valid_type?
78
90
  end
91
+
92
+ def has_handler?
93
+ !!options[:handler]
94
+ end
79
95
  end
80
96
  end
@@ -2,7 +2,7 @@ module WeakParameters
2
2
  module Controller
3
3
  def validates(action_name, &block)
4
4
  before_filter only: action_name do
5
- validator = WeakParameters::Validator.new(params, &block)
5
+ validator = WeakParameters::Validator.new(self, &block)
6
6
  WeakParameters.stats[params[:controller]][params[:action]] = validator
7
7
  WeakParameters.stats[params[:controller]][params[:action]].validate
8
8
  end
@@ -1,9 +1,9 @@
1
1
  module WeakParameters
2
2
  class Validator
3
- attr_reader :block, :params
3
+ attr_reader :block, :controller
4
4
 
5
- def initialize(params, &block)
6
- @params = params
5
+ def initialize(controller, &block)
6
+ @controller = controller
7
7
  instance_eval(&block)
8
8
  end
9
9
 
@@ -18,31 +18,31 @@ module WeakParameters
18
18
  private
19
19
 
20
20
  def any(key, options = {}, &block)
21
- validators << WeakParameters::AnyValidator.new(params, key, options, &block)
21
+ validators << WeakParameters::AnyValidator.new(controller, key, options, &block)
22
22
  end
23
23
 
24
24
  def string(key, options = {}, &block)
25
- validators << WeakParameters::StringValidator.new(params, key, options, &block)
25
+ validators << WeakParameters::StringValidator.new(controller, key, options, &block)
26
26
  end
27
27
 
28
28
  def integer(key, options = {}, &block)
29
- validators << WeakParameters::IntegerValidator.new(params, key, options, &block)
29
+ validators << WeakParameters::IntegerValidator.new(controller, key, options, &block)
30
30
  end
31
31
 
32
32
  def boolean(key, options = {}, &block)
33
- validators << WeakParameters::BooleanValidator.new(params, key, options, &block)
33
+ validators << WeakParameters::BooleanValidator.new(controller, key, options, &block)
34
34
  end
35
35
 
36
36
  def hash(key, options = {}, &block)
37
- validators << WeakParameters::HashValidator.new(params, key, options, &block)
37
+ validators << WeakParameters::HashValidator.new(controller, key, options, &block)
38
38
  end
39
39
 
40
40
  def array(key, options = {}, &block)
41
- validators << WeakParameters::ArrayValidator.new(params, key, options, &block)
41
+ validators << WeakParameters::ArrayValidator.new(controller, key, options, &block)
42
42
  end
43
43
 
44
- def float(key, options = {})
45
- validators << WeakParameters::FloatValidator.new(params, key, options, &block)
44
+ def float(key, options = {}, &block)
45
+ validators << WeakParameters::FloatValidator.new(controller, key, options, &block)
46
46
  end
47
47
  end
48
48
  end
@@ -1,3 +1,3 @@
1
1
  module WeakParameters
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
@@ -8,6 +8,7 @@ class RecipesController < ApplicationController
8
8
  hash :config
9
9
  array :tags
10
10
  float :rate
11
+ integer :custom, only: 0..1, handler: :render_error
11
12
  string :zip_code do |value|
12
13
  value =~ /\A\d{3}-\d{4}\z/
13
14
  end
@@ -16,4 +17,10 @@ class RecipesController < ApplicationController
16
17
  def create
17
18
  head 201
18
19
  end
20
+
21
+ private
22
+
23
+ def render_error
24
+ head 403
25
+ end
19
26
  end
@@ -11,6 +11,7 @@ describe "Recipes" do
11
11
  config: {},
12
12
  tags: [],
13
13
  zip_code: "123-4567",
14
+ custom: 0,
14
15
  }
15
16
  end
16
17
 
@@ -116,5 +117,15 @@ describe "Recipes" do
116
117
  context "with valid condition", :autodoc do
117
118
  include_examples "201"
118
119
  end
120
+
121
+ context "with custom handler option" do
122
+ before do
123
+ params[:custom] = "invalid"
124
+ end
125
+ it "delegates to specified method" do
126
+ post "/recipes", params
127
+ response.status.should == 403
128
+ end
129
+ end
119
130
  end
120
131
  end
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.1.1
4
+ version: 0.1.2
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-09-12 00:00:00.000000000 Z
11
+ date: 2013-10-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -191,3 +191,4 @@ test_files:
191
191
  - spec/dummy/script/rails
192
192
  - spec/requests/recipes_spec.rb
193
193
  - spec/spec_helper.rb
194
+ has_rdoc: