valid 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,24 @@
1
+ module Validation
2
+ module Rule
3
+ class Matches
4
+ attr_writer :obj
5
+
6
+ def initialize(matcher_field)
7
+ @matcher_field = matcher_field
8
+ end
9
+
10
+ def error_key
11
+ :matches
12
+ end
13
+
14
+ def params
15
+ @matcher_field
16
+ end
17
+
18
+ def valid_value?(value)
19
+ matcher_value = @obj.send(@matcher_field)
20
+ matcher_value == value
21
+ end
22
+ end
23
+ end
24
+ end
@@ -16,18 +16,21 @@ module Validation
16
16
 
17
17
  begin
18
18
  if rule.respond_to?(:each_pair)
19
- rule.each_pair do |key, value|
20
- r = Validation::Rule.const_get(camelize(key))
21
- rules[field] << r.new(value)
22
- end
19
+ add_parameterized_rule(field, rule)
23
20
  elsif rule.respond_to?(:each)
24
21
  rule.each do |r|
25
- r = Validation::Rule.const_get(camelize(r))
26
- rules[field] << r.new
22
+ if r.respond_to?(:each_pair)
23
+ add_parameterized_rule(field, r)
24
+ else
25
+ r = Validation::Rule.const_get(camelize(r)).new
26
+ add_object_to_rule(r)
27
+ rules[field] << r
28
+ end
27
29
  end
28
30
  else
29
- rule = Validation::Rule.const_get(camelize(rule))
30
- rules[field] << rule.new
31
+ rule = Validation::Rule.const_get(camelize(rule)).new
32
+ add_object_to_rule(rule)
33
+ rules[field] << rule
31
34
  end
32
35
  rescue NameError => e
33
36
  raise InvalidRule
@@ -56,6 +59,20 @@ module Validation
56
59
 
57
60
  protected
58
61
 
62
+ def add_parameterized_rule(field, rule)
63
+ rule.each_pair do |key, value|
64
+ r = Validation::Rule.const_get(camelize(key)).new(value)
65
+ add_object_to_rule(r)
66
+ rules[field] << r
67
+ end
68
+ end
69
+
70
+ def add_object_to_rule(rule)
71
+ if rule.respond_to?(:obj=)
72
+ rule.obj = @obj
73
+ end
74
+ end
75
+
59
76
  def camelize(term)
60
77
  string = term.to_s
61
78
  string = string.sub(/^[a-z\d]*/) { $&.capitalize }
@@ -0,0 +1,3 @@
1
+ module Validation
2
+ VERSION = '0.2.0'
3
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: valid
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-16 00:00:00.000000000 Z
12
+ date: 2012-06-17 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description:
15
15
  email:
@@ -21,10 +21,12 @@ files:
21
21
  - lib/validation.rb
22
22
  - lib/validation/rule/email.rb
23
23
  - lib/validation/rule/not_empty.rb
24
+ - lib/validation/rule/matches.rb
24
25
  - lib/validation/rule/numeric.rb
25
26
  - lib/validation/rule/length.rb
26
27
  - lib/validation/validator.rb
27
- homepage: https://github.com/zombor/Validator
28
+ - lib/validation/version.rb
29
+ homepage: https://github.com/zombor/validation
28
30
  licenses: []
29
31
  post_install_message:
30
32
  rdoc_options: []