not-naughty 0.6.0 → 0.6.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +5 -0
- data/Rakefile +2 -2
- data/lib/not_naughty/builder.rb +10 -11
- data/lib/not_naughty/validations/confirmation_validation.rb +20 -12
- data/lib/not_naughty/validations/format_validation.rb +35 -19
- data/lib/not_naughty/validations/length_validation.rb +38 -42
- data/lib/not_naughty/validations/numericality_validation.rb +5 -5
- data/lib/not_naughty/validations/presence_validation.rb +5 -4
- data/spec/spec_helper.rb +4 -1
- data/spec/validations_spec.rb +2 -2
- metadata +2 -2
data/CHANGELOG.rdoc
CHANGED
data/Rakefile
CHANGED
@@ -9,7 +9,7 @@ include FileUtils
|
|
9
9
|
# Configuration
|
10
10
|
##############################################################################
|
11
11
|
NAME = "not-naughty"
|
12
|
-
VERS = "0.6.
|
12
|
+
VERS = "0.6.1"
|
13
13
|
CLEAN.include %w[ pkg doc coverage ]
|
14
14
|
RDOC_OPTS = [
|
15
15
|
"--quiet",
|
@@ -39,7 +39,7 @@ task :doc_rforge => [:doc]
|
|
39
39
|
|
40
40
|
desc "Update docs and upload to rubyforge.org"
|
41
41
|
task :doc_rforge do
|
42
|
-
sh %{scp -r doc
|
42
|
+
sh %{scp -r doc/* boof@rubyforge.org:/var/www/gforge-projects/not-naughty}
|
43
43
|
end
|
44
44
|
|
45
45
|
##############################################################################
|
data/lib/not_naughty/builder.rb
CHANGED
@@ -40,26 +40,25 @@ module NotNaughty
|
|
40
40
|
|
41
41
|
class ValidationDelegator < SimpleDelegator #:nodoc:all
|
42
42
|
def initialize(receiver, *params)
|
43
|
-
@_sd_obj_opts
|
44
|
-
@
|
43
|
+
@_sd_obj_opts = params.extract_options!
|
44
|
+
@_sd_obj_attributes = params
|
45
45
|
|
46
46
|
super receiver
|
47
47
|
end
|
48
48
|
def method_missing(method_sym, *params) #:nodoc:
|
49
|
-
|
50
|
-
|
51
|
-
|
49
|
+
valid = @_sd_obj_opts.merge params.extract_options!
|
50
|
+
valid = Marshal.load Marshal.dump(valid)
|
51
|
+
|
52
|
+
method_sym, params = unless @_sd_obj_attributes.empty?
|
53
|
+
[:"validates_#{ method_sym }_of", @_sd_obj_attributes + [valid]]
|
52
54
|
else
|
53
|
-
|
54
|
-
[:"validates_#{method_sym}", params + [opts]]
|
55
|
+
[:"validates_#{ method_sym }", params + [valid]]
|
55
56
|
end
|
56
57
|
|
57
58
|
if @_sd_obj.respond_to? method_sym
|
58
|
-
@_sd_obj.send(method_sym, *params)
|
59
|
-
return true
|
59
|
+
@_sd_obj.send(method_sym, *params); true
|
60
60
|
else
|
61
|
-
raise NoMethodError,
|
62
|
-
"unable to evaluate ´#{method_sym}(*#{params.inspect})'"
|
61
|
+
raise NoMethodError, "unable to evaluate ´#{method_sym}(#{ params.map {|p|p.inspect} * ','})'"
|
63
62
|
end
|
64
63
|
end
|
65
64
|
end
|
@@ -20,22 +20,30 @@ module NotNaughty
|
|
20
20
|
# obj.errors.on(:to_s).any? # => true
|
21
21
|
class ConfirmationValidation < Validation
|
22
22
|
|
23
|
-
def initialize(
|
24
|
-
|
23
|
+
def initialize(valid, attributes) #:nodoc:
|
24
|
+
valid[:message] ||= '%s could not be confirmed.'
|
25
25
|
|
26
|
-
if
|
27
|
-
|
28
|
-
super
|
29
|
-
o.errors.add a, __message unless
|
30
|
-
v.send __allow or o.send(:"#{a}_confirmation").eql? v
|
31
|
-
end
|
26
|
+
if valid[:allow_blank] || valid[:allow_nil]
|
27
|
+
valid[:allow] = valid[:allow_blank] ? :blank? : :nil?
|
28
|
+
super valid, attributes, &confirmation_block_with_exception(valid)
|
32
29
|
else
|
33
|
-
super
|
34
|
-
o.errors.add a, __message unless
|
35
|
-
o.send(:"#{a}_confirmation").eql? v
|
36
|
-
end
|
30
|
+
super valid, attributes, &confirmation_block(valid)
|
37
31
|
end
|
38
32
|
end
|
39
33
|
|
34
|
+
protected
|
35
|
+
def confirmation_block_with_exception(valid)
|
36
|
+
proc do |obj, attr, value|
|
37
|
+
value.send valid[:allow] or
|
38
|
+
obj.send(:"#{ attr }_confirmation") == value or
|
39
|
+
obj.errors.add attr, valid[:message]
|
40
|
+
end
|
41
|
+
end
|
42
|
+
def confirmation_block(valid)
|
43
|
+
proc do |obj, attr, value|
|
44
|
+
obj.send(:"#{ attr }_confirmation") == value or
|
45
|
+
obj.errors.add attr, valid[:message]
|
46
|
+
end
|
47
|
+
end
|
40
48
|
end
|
41
49
|
end
|
@@ -16,38 +16,54 @@ module NotNaughty
|
|
16
16
|
# obj = 'abc'
|
17
17
|
# def obj.errors() @errors ||= NotNauthy::Errors.new end
|
18
18
|
#
|
19
|
-
# FormatValidation.new({:with => /[a-z]/}, :to_s).call obj, :to_s,
|
19
|
+
# FormatValidation.new({:with => /[a-z]/}, :to_s).call obj, :to_s, obj
|
20
20
|
# obj.errors.on(:to_s).any? # => false
|
21
21
|
#
|
22
|
-
# FormatValidation.new({:with => /[A-Z]/}, :to_s).call obj, :to_s,
|
22
|
+
# FormatValidation.new({:with => /[A-Z]/}, :to_s).call obj, :to_s, obj
|
23
23
|
# obj.errors.on(:to_s) # => ["Format of to_s does not match."]
|
24
|
+
#
|
25
|
+
# <b>Use predefined format expressions:</b>
|
26
|
+
#
|
27
|
+
# obj = 'Valid Address <foo@bar.baz>'
|
28
|
+
# def obj.errors() @errors ||= NotNauthy::Errors.new end
|
29
|
+
#
|
30
|
+
# FormatValidation.new({:with => :email}, :to_s).call obj, :to_s, obj
|
31
|
+
# obj.errors.on(:to_s).any? # => false
|
24
32
|
class FormatValidation < Validation
|
25
33
|
|
26
34
|
# Predefined matchers.
|
27
|
-
|
35
|
+
PREDEFINED = {
|
28
36
|
:email => /[a-z0-9!#\$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#\$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/,
|
29
37
|
:ip => /(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/
|
30
38
|
}
|
31
39
|
|
32
|
-
def initialize(
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
msg = opts[:message] || '%s does not match format.'
|
40
|
+
def initialize(valid, attributes) #:nodoc:
|
41
|
+
valid[:with] = PREDEFINED.fetch valid[:with] if valid[:with].is_a? Symbol
|
42
|
+
valid[:with].respond_to? :match or
|
43
|
+
raise ArgumentError, "#{ valid[:with].inspect } doesn't :match"
|
38
44
|
|
39
|
-
|
40
|
-
pass = opts[:allow_blank] ? :blank? : :nil?
|
45
|
+
valid[:message] ||= '%s does not match format.'
|
41
46
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
end
|
47
|
+
if valid[:allow_blank] || valid[:allow_nil]
|
48
|
+
valid[:allow] = valid[:allow_blank] ? :blank? : :nil?
|
49
|
+
super valid, attributes, &matching_block_with_exception(valid)
|
46
50
|
else
|
47
|
-
super
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
+
super valid, attributes, &matching_block(valid)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
protected
|
56
|
+
def matching_block_with_exception(valid)
|
57
|
+
proc do |obj, attr, value|
|
58
|
+
value.send valid[:allow] or
|
59
|
+
valid[:with].match value or
|
60
|
+
obj.errors.add attr, valid[:message]
|
61
|
+
end
|
62
|
+
end
|
63
|
+
def matching_block(valid)
|
64
|
+
proc do |obj, attr, value|
|
65
|
+
valid[:with].match value or
|
66
|
+
obj.errors.add attr, valid[:message]
|
51
67
|
end
|
52
68
|
end
|
53
69
|
|
@@ -35,60 +35,56 @@ module NotNaughty
|
|
35
35
|
# LengthValidation.new({:within => 1..4}, :to_a).
|
36
36
|
# call obj, :to_a, %w[a sentence with five words]
|
37
37
|
# obj.errors.on(:to_s).any? # => true
|
38
|
+
#
|
39
|
+
# I'm sorry for using eval here...
|
38
40
|
class LengthValidation < Validation
|
39
41
|
|
40
|
-
|
42
|
+
TEMPLATE = <<-BLOCK
|
43
|
+
proc do |obj, attr, value|
|
44
|
+
value.%s or %s value.length or
|
45
|
+
obj.errors.add attr, %s
|
46
|
+
end
|
47
|
+
BLOCK
|
41
48
|
|
42
|
-
|
49
|
+
def initialize(valid, attributes) #:nodoc:
|
50
|
+
super valid, attributes, &build_block(valid)
|
51
|
+
end
|
43
52
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
53
|
+
protected
|
54
|
+
def build_block(valid) #:nodoc:
|
55
|
+
if valid[:is] then is_block valid
|
56
|
+
elsif valid[:within] or valid[:minimum] && valid[:maximum] then within_block valid
|
57
|
+
elsif valid[:minimum] then minimum_block valid
|
58
|
+
elsif valid[:maximum] then maximum_block valid
|
48
59
|
else
|
49
|
-
|
50
|
-
block[o, a, v] unless v.nil?
|
51
|
-
end
|
60
|
+
raise ArgumentError, 'no boundary given'
|
52
61
|
end
|
53
62
|
end
|
54
63
|
|
55
|
-
|
56
|
-
|
57
|
-
if __length = opts[:is]
|
58
|
-
__message = opts[:message] ||
|
59
|
-
"Length of %s is not equal to #{__length}."
|
60
|
-
proc do |o, a, v|
|
61
|
-
o.errors.add a, __message unless __length.eql? v.length
|
62
|
-
end
|
63
|
-
elsif opts[:within] or opts[:minimum] && opts[:maximum]
|
64
|
-
__range = opts[:within]
|
65
|
-
__range ||= Range.new opts[:minimum], opts[:maximum]
|
64
|
+
def is_block(valid)
|
65
|
+
valid[:message] ||= "Length of %s is not equal to #{ valid[:is] }."
|
66
66
|
|
67
|
-
|
68
|
-
|
67
|
+
eval TEMPLATE % [ valid[:allow_blank] ? :blank? : :nil?,
|
68
|
+
"#{ valid[:is].inspect } ==", valid[:message].inspect ]
|
69
|
+
end
|
70
|
+
def within_block(valid)
|
71
|
+
valid[:within] ||= Range.new valid[:minimum], valid[:maximum]
|
72
|
+
valid[:message] ||= "Length of %s is not within #{ valid[:within].min } and #{ valid[:within].max }."
|
69
73
|
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
__message = opts[:message] ||
|
76
|
-
"Length of %s is smaller than #{__boundary}."
|
74
|
+
eval TEMPLATE % [ valid[:allow_blank] ? :blank? : :nil?,
|
75
|
+
"(#{ valid[:within].inspect }).include?", valid[:message].inspect ]
|
76
|
+
end
|
77
|
+
def minimum_block(valid)
|
78
|
+
valid[:message] ||= "Length of %s is less than #{ valid[:minimum] }."
|
77
79
|
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
__message = opts[:message] ||
|
84
|
-
"Length of %s is greater than #{__boundary}."
|
80
|
+
eval TEMPLATE % [ valid[:allow_blank] ? :blank? : :nil?,
|
81
|
+
"#{ valid[:minimum].inspect } <=", valid[:message].inspect ]
|
82
|
+
end
|
83
|
+
def maximum_block(valid)
|
84
|
+
valid[:message] ||= "Length of %s is greater than #{ valid[:maximum] }."
|
85
85
|
|
86
|
-
|
87
|
-
|
88
|
-
end
|
89
|
-
else
|
90
|
-
raise ArgumentError, 'no boundary given'
|
91
|
-
end
|
86
|
+
eval TEMPLATE % [ valid[:allow_blank] ? :blank? : :nil?,
|
87
|
+
"#{ valid[:maximum].inspect } >=", valid[:message].inspect ]
|
92
88
|
end
|
93
89
|
|
94
90
|
end
|
@@ -27,16 +27,16 @@ module NotNaughty
|
|
27
27
|
# obj.errors.on(:to_s).any? # => true
|
28
28
|
class NumericalityValidation < FormatValidation
|
29
29
|
|
30
|
-
def initialize(
|
31
|
-
|
32
|
-
|
30
|
+
def initialize(valid, attributes) #:nodoc:
|
31
|
+
valid[:with] = if valid[:only_integer]
|
32
|
+
valid[:message] ||= '%s is not an integer.'
|
33
33
|
/^[+-]?\d+$/
|
34
34
|
else
|
35
|
-
|
35
|
+
valid[:message] ||= '%s is not a number.'
|
36
36
|
/^[+-]?\d*\.?\d+$/
|
37
37
|
end
|
38
38
|
|
39
|
-
super
|
39
|
+
super valid, attributes
|
40
40
|
end
|
41
41
|
|
42
42
|
end
|
@@ -19,11 +19,12 @@ module NotNaughty
|
|
19
19
|
# obj.errors.on(:to_s) # => ["To s is not present."]
|
20
20
|
class PresenceValidation < Validation
|
21
21
|
|
22
|
-
def initialize(
|
23
|
-
|
22
|
+
def initialize(valid, attributes) #:nodoc:
|
23
|
+
valid[:message] ||= '%s is not present.'
|
24
24
|
|
25
|
-
super
|
26
|
-
|
25
|
+
super valid, attributes do |obj, attr, value|
|
26
|
+
value.blank? and
|
27
|
+
obj.errors.add attr, valid[:message]
|
27
28
|
end
|
28
29
|
end
|
29
30
|
|
data/spec/spec_helper.rb
CHANGED
data/spec/validations_spec.rb
CHANGED
@@ -94,10 +94,10 @@ describe FormatExample do
|
|
94
94
|
|
95
95
|
before(:each) { @example = FormatExample.clone }
|
96
96
|
|
97
|
-
it "claims to match 99% of all e-mail addresses out there..." do
|
97
|
+
it "claims to match 99.9% of all e-mail addresses out there..." do
|
98
98
|
# Regexp was taken from: http://www.regular-expressions.info/email.html
|
99
99
|
@example.validates_format_of :email,
|
100
|
-
:with =>
|
100
|
+
:with => :email
|
101
101
|
@example.new('"Foo Bar" <foo@bar.com>').should be_valid
|
102
102
|
@example.new('foo@bar.com').should be_valid
|
103
103
|
@example.new('foobarcom').should_not be_valid
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: not-naughty
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.
|
4
|
+
version: 0.6.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- "Florian A\xC3\x9Fmann"
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-10-
|
12
|
+
date: 2008-10-10 00:00:00 +02:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|