validatable 1.3.0 → 1.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README +1 -1
- data/lib/errors.rb +31 -5
- data/lib/understandable.rb +4 -4
- data/lib/validatable_assertions.rb +5 -5
- data/lib/validations/validates_length_of.rb +8 -3
- data/rakefile.rb +1 -1
- data/test/functional/validation_assertion_collector_test.rb +1 -1
- data/test/unit/errors_test.rb +21 -0
- data/test/unit/validatable_test.rb +2 -1
- data/test/unit/validates_length_of_test.rb +16 -4
- metadata +4 -3
data/README
CHANGED
@@ -115,4 +115,4 @@ In the above example the attribute "name" is appended to the message.
|
|
115
115
|
See the tests for more examples
|
116
116
|
|
117
117
|
== Contributors
|
118
|
-
Rick Bradley, Anonymous Z, Jason Miller, Ali Aghareza, Xavier Shay
|
118
|
+
Rick Bradley, Anonymous Z, Jason Miller, Ali Aghareza, Xavier Shay, Dan Manges, Karthik Krishnan and Venkat
|
data/lib/errors.rb
CHANGED
@@ -1,9 +1,10 @@
|
|
1
1
|
module Validatable
|
2
2
|
class Errors
|
3
3
|
extend Forwardable
|
4
|
-
|
5
|
-
|
6
|
-
|
4
|
+
include Enumerable
|
5
|
+
|
6
|
+
def_delegators :errors, :clear, :each, :each_pair, :empty?, :length, :size
|
7
|
+
|
7
8
|
# call-seq: on(attribute)
|
8
9
|
#
|
9
10
|
# Returns nil, if no errors are associated with the specified attribute.
|
@@ -11,11 +12,11 @@ module Validatable
|
|
11
12
|
def on(attribute)
|
12
13
|
errors[attribute.to_sym]
|
13
14
|
end
|
14
|
-
|
15
|
+
|
15
16
|
def add(attribute, message) #:nodoc:
|
16
17
|
errors[attribute.to_sym] = message
|
17
18
|
end
|
18
|
-
|
19
|
+
|
19
20
|
def merge!(errors) #:nodoc:
|
20
21
|
errors.each_pair{|k, v| add(k,v)}
|
21
22
|
self
|
@@ -24,5 +25,30 @@ module Validatable
|
|
24
25
|
def errors #:nodoc:
|
25
26
|
@errors ||= {}
|
26
27
|
end
|
28
|
+
|
29
|
+
def count
|
30
|
+
size
|
31
|
+
end
|
32
|
+
|
33
|
+
def full_messages
|
34
|
+
full_messages = []
|
35
|
+
|
36
|
+
errors.each_key do |attribute|
|
37
|
+
errors[attribute].each do |msg|
|
38
|
+
next if msg.nil?
|
39
|
+
|
40
|
+
if attribute.to_s == "base"
|
41
|
+
full_messages << msg
|
42
|
+
else
|
43
|
+
full_messages << humanize(attribute.to_s) + " " + msg
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
full_messages
|
48
|
+
end
|
49
|
+
|
50
|
+
def humanize(lower_case_and_underscored_word)
|
51
|
+
lower_case_and_underscored_word.to_s.gsub(/_id$/, "").gsub(/_/, " ").capitalize
|
52
|
+
end
|
27
53
|
end
|
28
54
|
end
|
data/lib/understandable.rb
CHANGED
@@ -4,21 +4,21 @@ module Validatable
|
|
4
4
|
def understands(*args)
|
5
5
|
understandings.concat args
|
6
6
|
end
|
7
|
-
|
7
|
+
|
8
8
|
def understandings
|
9
9
|
@understandings ||= []
|
10
10
|
end
|
11
|
-
|
11
|
+
|
12
12
|
def all_understandings
|
13
13
|
return understandings + self.superclass.all_understandings if self.superclass.respond_to? :all_understandings
|
14
14
|
understandings
|
15
15
|
end
|
16
16
|
end
|
17
|
-
|
17
|
+
|
18
18
|
def self.included(klass)
|
19
19
|
klass.extend ClassMethods
|
20
20
|
end
|
21
|
-
|
21
|
+
|
22
22
|
def must_understand(hash)
|
23
23
|
invalid_options = hash.inject([]) do |errors, (key, value)|
|
24
24
|
errors << key.to_s unless self.class.all_understandings.include?(key)
|
@@ -1,18 +1,18 @@
|
|
1
1
|
module ValidatableAssertions
|
2
|
-
|
2
|
+
|
3
3
|
def create_message_for_assertion(assertion)
|
4
4
|
message = "#{assertion.klass} does not contain a #{assertion.validation_type} for #{assertion.attribute}"
|
5
5
|
message += " with options #{assertion.options.inspect}" unless assertion.options == {}
|
6
6
|
message
|
7
7
|
end
|
8
|
-
|
8
|
+
|
9
9
|
def create_backtrace
|
10
10
|
backtrace = caller
|
11
11
|
backtrace.shift
|
12
12
|
backtrace.shift
|
13
13
|
backtrace
|
14
14
|
end
|
15
|
-
|
15
|
+
|
16
16
|
def validation_matching_proc(assertion)
|
17
17
|
lambda do |validation|
|
18
18
|
result = assertion.validation_type === validation
|
@@ -25,13 +25,13 @@ module ValidatableAssertions
|
|
25
25
|
result
|
26
26
|
end
|
27
27
|
end
|
28
|
-
|
28
|
+
|
29
29
|
def self.included(klass)
|
30
30
|
Test::Unit::TestCase.class_eval do
|
31
31
|
def self.create_test_name(assertion)
|
32
32
|
"test#{assertion.validation_type.to_s.gsub(/Validatable::/,'').gsub(/([A-Z])/, '_\1').downcase}_#{assertion.attribute}"
|
33
33
|
end
|
34
|
-
|
34
|
+
|
35
35
|
def self.define_test_method name, &block
|
36
36
|
class_eval do
|
37
37
|
define_method name, &block
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module Validatable
|
2
2
|
class ValidatesLengthOf < ValidationBase #:nodoc:
|
3
|
-
option :minimum, :maximum, :is, :within
|
3
|
+
option :minimum, :maximum, :is, :within, :allow_nil
|
4
4
|
|
5
5
|
def message
|
6
6
|
super || "is invalid"
|
@@ -8,8 +8,13 @@ module Validatable
|
|
8
8
|
|
9
9
|
def valid?(instance)
|
10
10
|
valid = true
|
11
|
-
value = instance.send(self.attribute)
|
12
|
-
|
11
|
+
value = instance.send(self.attribute)
|
12
|
+
|
13
|
+
if value.nil?
|
14
|
+
return true if allow_nil
|
15
|
+
value = ""
|
16
|
+
end
|
17
|
+
|
13
18
|
valid &&= value.length <= maximum unless maximum.nil?
|
14
19
|
valid &&= value.length >= minimum unless minimum.nil?
|
15
20
|
valid &&= value.length == is unless is.nil?
|
data/rakefile.rb
CHANGED
@@ -29,7 +29,7 @@ Gem::manage_gems
|
|
29
29
|
specification = Gem::Specification.new do |s|
|
30
30
|
s.name = "validatable"
|
31
31
|
s.summary = "Validatable is a library for adding validations."
|
32
|
-
s.version = "1.3.
|
32
|
+
s.version = "1.3.2"
|
33
33
|
s.author = 'Jay Fields'
|
34
34
|
s.description = "Validatable is a library for adding validations."
|
35
35
|
s.email = 'validatable-developer@rubyforge.org'
|
data/test/unit/errors_test.rb
CHANGED
@@ -6,4 +6,25 @@ class ErrorsTest < Test::Unit::TestCase
|
|
6
6
|
errors.add(:attribute, "message")
|
7
7
|
errors.on(:attribute)
|
8
8
|
end
|
9
|
+
|
10
|
+
expect "Capitalized word" do
|
11
|
+
errors = Validatable::Errors.new
|
12
|
+
errors.humanize("capitalized_word")
|
13
|
+
end
|
14
|
+
|
15
|
+
expect "Capitalized word without" do
|
16
|
+
errors = Validatable::Errors.new
|
17
|
+
errors.humanize("capitalized_word_without_id")
|
18
|
+
end
|
19
|
+
|
20
|
+
expect ["A humanized message", "a base message"] do
|
21
|
+
errors = Validatable::Errors.new
|
22
|
+
errors.add(:base, "a base message")
|
23
|
+
errors.add(:a_humanized, "message")
|
24
|
+
errors.full_messages.sort
|
25
|
+
end
|
26
|
+
|
27
|
+
test "includes enumerable" do
|
28
|
+
assert_equal true, Validatable::Errors.included_modules.include?(Enumerable)
|
29
|
+
end
|
9
30
|
end
|
@@ -3,7 +3,8 @@ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
|
|
3
3
|
module Unit
|
4
4
|
class ValidatableTest < Test::Unit::TestCase
|
5
5
|
expect false do
|
6
|
-
validation = stub(:valid? => false, :should_validate? => true,
|
6
|
+
validation = stub(:valid? => false, :should_validate? => true,
|
7
|
+
:attribute => "attribute", :message => "message", :level => 1, :run_after_validate => nil)
|
7
8
|
klass = Class.new do
|
8
9
|
include Validatable
|
9
10
|
validations << validation
|
@@ -2,10 +2,9 @@ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
|
|
2
2
|
|
3
3
|
module Unit
|
4
4
|
class ValidatesLengthOfTest < Test::Unit::TestCase
|
5
|
-
|
5
|
+
expect false do
|
6
6
|
validation = Validatable::ValidatesLengthOf.new :username, :maximum => 8
|
7
|
-
|
8
|
-
assert_equal false, validation.valid?(instance)
|
7
|
+
validation.valid?(stub(:username=>"usernamefdfd"))
|
9
8
|
end
|
10
9
|
|
11
10
|
test "min length" do
|
@@ -56,8 +55,21 @@ module Unit
|
|
56
55
|
assert_equal false, validation.valid?(instance)
|
57
56
|
end
|
58
57
|
|
58
|
+
test "given nil value, should not be valid" do
|
59
|
+
validation = Validatable::ValidatesLengthOf.new :username, :within => 2..4
|
60
|
+
instance = stub(:username => nil)
|
61
|
+
assert_equal false, validation.valid?(instance)
|
62
|
+
end
|
63
|
+
|
64
|
+
|
65
|
+
test "given allow_nil is true, nil value should be valid" do
|
66
|
+
validation = Validatable::ValidatesLengthOf.new :username, :within => 2..4, :allow_nil => true
|
67
|
+
instance = stub(:username => nil)
|
68
|
+
assert_equal true, validation.valid?(instance)
|
69
|
+
end
|
70
|
+
|
59
71
|
expect true do
|
60
|
-
options = [:message, :if, :times, :level, :groups, :maximum, :minimum, :is, :within]
|
72
|
+
options = [:message, :if, :times, :level, :groups, :maximum, :minimum, :is, :within, :allow_nil]
|
61
73
|
Validatable::ValidatesLengthOf.new(:test).must_understand(options.to_blank_options_hash)
|
62
74
|
end
|
63
75
|
|
metadata
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
|
-
rubygems_version: 0.
|
2
|
+
rubygems_version: 0.9.2
|
3
3
|
specification_version: 1
|
4
4
|
name: validatable
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 1.3.
|
7
|
-
date: 2007-05-
|
6
|
+
version: 1.3.2
|
7
|
+
date: 2007-05-30 00:00:00 -04:00
|
8
8
|
summary: Validatable is a library for adding validations.
|
9
9
|
require_paths:
|
10
10
|
- lib
|
@@ -25,6 +25,7 @@ required_ruby_version: !ruby/object:Gem::Version::Requirement
|
|
25
25
|
platform: ruby
|
26
26
|
signing_key:
|
27
27
|
cert_chain:
|
28
|
+
post_install_message:
|
28
29
|
authors:
|
29
30
|
- Jay Fields
|
30
31
|
files:
|