simple_validation 0.1.1 → 0.1.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/CHANGELOG.md +5 -0
- data/README.md +3 -1
- data/lib/simple_validation.rb +71 -2
- data/lib/simple_validation/version.rb +8 -1
- data/spec/simple_validation_spec.rb +7 -0
- data/spec/spec_helper.rb +7 -0
- metadata +1 -1
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -32,7 +32,7 @@ class AlienNumber
|
|
32
32
|
end
|
33
33
|
|
34
34
|
def value
|
35
|
-
@digits.reduce(
|
35
|
+
@digits.reduce(0) {|result, digit| result + digit}
|
36
36
|
end
|
37
37
|
|
38
38
|
private
|
@@ -54,11 +54,13 @@ valid_number = AlienNumber.new(1, 2, 3)
|
|
54
54
|
valid_number.valid? # true
|
55
55
|
valid_number.invalid? # false
|
56
56
|
valid_number.errors # []
|
57
|
+
valid_number.value # 6
|
57
58
|
|
58
59
|
invalid_number = AlienNumber.new(-1, 12)
|
59
60
|
invalid_number.valid? # false
|
60
61
|
invalid_number.invalid? # true
|
61
62
|
invalid_number.errors # ["-1 is negative", "12 is greater than 9"]
|
63
|
+
invalid_number.value # 11
|
62
64
|
```
|
63
65
|
|
64
66
|
Running tests
|
data/lib/simple_validation.rb
CHANGED
@@ -1,20 +1,85 @@
|
|
1
|
+
# simple_validation.rb
|
2
|
+
#
|
3
|
+
# Created by Chirantan Mitra on 2012-11-20
|
4
|
+
# Copyright 2012. All rights reserved
|
5
|
+
#
|
6
|
+
# See LICENSE for license
|
7
|
+
#
|
8
|
+
# This module adds methods to easily validate a plain ruby object
|
9
|
+
#
|
10
|
+
# Example:
|
11
|
+
# require "simple_validation"
|
12
|
+
#
|
13
|
+
# class AlienNumber
|
14
|
+
# include SimpleValidation
|
15
|
+
#
|
16
|
+
# validate :digits_are_positive
|
17
|
+
# validate :digits_are_less_than_ten
|
18
|
+
#
|
19
|
+
# def initialize(*digits)
|
20
|
+
# @digits = digits
|
21
|
+
# end
|
22
|
+
#
|
23
|
+
# def value
|
24
|
+
# @digits.reduce(0) {|result, digit| result + digit}
|
25
|
+
# end
|
26
|
+
#
|
27
|
+
# private
|
28
|
+
#
|
29
|
+
# def digits_are_positive
|
30
|
+
# @digits.each do |digit|
|
31
|
+
# add_error("#{digit} is negative") unless digit > 0
|
32
|
+
# end
|
33
|
+
# end
|
34
|
+
#
|
35
|
+
# def digits_are_less_than_ten
|
36
|
+
# @digits.each do |digit|
|
37
|
+
# add_error("#{digit} is greater than 9") unless digit < 10
|
38
|
+
# end
|
39
|
+
# end
|
40
|
+
# end
|
41
|
+
#
|
42
|
+
# valid_number = AlienNumber.new(1, 2, 3)
|
43
|
+
# valid_number.valid? # true
|
44
|
+
# valid_number.invalid? # false
|
45
|
+
# valid_number.errors # []
|
46
|
+
# valid_number.value # 6
|
47
|
+
#
|
48
|
+
# invalid_number = AlienNumber.new(-1, 12)
|
49
|
+
# invalid_number.valid? # false
|
50
|
+
# invalid_number.invalid? # true
|
51
|
+
# invalid_number.errors # ["-1 is negative", "12 is greater than 9"]
|
52
|
+
# invalid_number.value # 11
|
53
|
+
|
1
54
|
module SimpleValidation
|
2
|
-
def self.included(base)
|
55
|
+
def self.included(base) # :nodoc:
|
3
56
|
base.class_eval do
|
4
57
|
base.extend SimpleValidation::ClassMethods
|
5
58
|
end
|
6
59
|
end
|
7
60
|
|
8
61
|
module ClassMethods
|
62
|
+
# Add a validation method
|
63
|
+
# The object to validate be able to invoke the method supplied
|
64
|
+
#
|
65
|
+
# Example:
|
66
|
+
# class AlienNumber
|
67
|
+
# include SimpleValidation
|
68
|
+
#
|
69
|
+
# validate :digits_are_positive
|
70
|
+
# validate :digits_are_less_than_ten
|
71
|
+
# end
|
9
72
|
def validate(method_name)
|
10
73
|
validation_methods << method_name
|
11
74
|
end
|
12
75
|
|
13
|
-
|
76
|
+
# The list of all method names that validate the object
|
77
|
+
def validation_methods # :nodoc:
|
14
78
|
@validation_methods ||= []
|
15
79
|
end
|
16
80
|
end
|
17
81
|
|
82
|
+
# Runs all validations and returns _true_ if the object is valid
|
18
83
|
def valid?
|
19
84
|
self.class.validation_methods.each do |method_name|
|
20
85
|
send method_name
|
@@ -23,18 +88,22 @@ module SimpleValidation
|
|
23
88
|
errors.empty?
|
24
89
|
end
|
25
90
|
|
91
|
+
# Runs all validations and returns _true_ if the object is invalid
|
26
92
|
def invalid?
|
27
93
|
not valid?
|
28
94
|
end
|
29
95
|
|
96
|
+
# Adds an error to the errors collection
|
30
97
|
def add_error(error)
|
31
98
|
errors << error unless errors.include?(error)
|
32
99
|
end
|
33
100
|
|
101
|
+
# Adds an array of errors to the errors collection
|
34
102
|
def add_errors(more_errors)
|
35
103
|
errors.concat(more_errors)
|
36
104
|
end
|
37
105
|
|
106
|
+
# Returns an array of the current errors
|
38
107
|
def errors
|
39
108
|
@errors ||= []
|
40
109
|
end
|
data/spec/spec_helper.rb
CHANGED