valid_attribute 0.0.1 → 0.0.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/.gitignore +2 -0
- data/HISTORY +3 -0
- data/README.markdown +7 -8
- data/lib/valid_attribute/version.rb +1 -1
- data/lib/valid_attribute.rb +16 -3
- data/spec/valid_attribute_spec.rb +18 -11
- metadata +3 -2
data/HISTORY
ADDED
data/README.markdown
CHANGED
@@ -27,18 +27,17 @@ Instead of having validation specific matchers ValidAttribute only cares if the
|
|
27
27
|
end
|
28
28
|
|
29
29
|
describe User do
|
30
|
-
# The .
|
31
|
-
it { should have_valid(:email).
|
32
|
-
it { should_not have_valid(:email).
|
33
|
-
it { should have_valid(:name).
|
34
|
-
it { should_not have_valid(:name).
|
30
|
+
# The .when method can take any number of values that you want to pass
|
31
|
+
it { should have_valid(:email).when('test@test.com', 'test+spam@gmail.com') }
|
32
|
+
it { should_not have_valid(:email).when('fail', 123).message('invalid email format') }
|
33
|
+
it { should have_valid(:name).when('TestName')
|
34
|
+
it { should_not have_valid(:name).when('Test')
|
35
35
|
|
36
36
|
# Because 'should' works off the the 'subject' in RSpec we can set other values if necessary for a given validation test
|
37
37
|
describe 'password' do
|
38
38
|
before { subject.password_confirmation = 'password' }
|
39
|
-
it { should have_valid(:password).
|
40
|
-
|
41
|
-
it { should_not have_valid(:password) }
|
39
|
+
it { should have_valid(:password).when('password') }
|
40
|
+
it { should_not have_valid(:password).when(nil) }
|
42
41
|
end
|
43
42
|
end
|
44
43
|
|
data/lib/valid_attribute.rb
CHANGED
@@ -1,5 +1,15 @@
|
|
1
1
|
module ValidAttribute
|
2
|
-
|
2
|
+
class NoValues < StandardError; end
|
3
|
+
# Test if an attribute is valid
|
4
|
+
#
|
5
|
+
# examples:
|
6
|
+
# it { should have_valid(:name).when('Brian') }
|
7
|
+
# it { should_not have_valid(:name).message("can't be blank") }
|
8
|
+
# it { should have_valid(:email).when('test@test.com', 'test+spam@gmail.com') }
|
9
|
+
#
|
10
|
+
# @param [Symbol]
|
11
|
+
#
|
12
|
+
# @return [ValidAttribute::ValidAttributeMatcher]
|
3
13
|
def have_valid(attr)
|
4
14
|
ValidAttributeMatcher.new(attr)
|
5
15
|
end
|
@@ -9,10 +19,9 @@ module ValidAttribute
|
|
9
19
|
|
10
20
|
def initialize(attr)
|
11
21
|
self.attr = attr
|
12
|
-
self.values = [nil]
|
13
22
|
end
|
14
23
|
|
15
|
-
def
|
24
|
+
def when(*values)
|
16
25
|
self.values = values
|
17
26
|
self
|
18
27
|
end
|
@@ -45,6 +54,10 @@ module ValidAttribute
|
|
45
54
|
end
|
46
55
|
|
47
56
|
def matches?(subject)
|
57
|
+
unless values
|
58
|
+
raise ::ValidAttribute::NoValues, "you need to set the values with .when on the matcher (ex. it { should have_valid(:name).when('Brian') })"
|
59
|
+
end
|
60
|
+
|
48
61
|
self.subject = subject
|
49
62
|
|
50
63
|
values.each do |value|
|
@@ -29,12 +29,12 @@ describe 'ValidAttribute' do
|
|
29
29
|
end
|
30
30
|
|
31
31
|
it 'passes with no value set' do
|
32
|
-
matcher = @should.have_valid(:name)
|
32
|
+
matcher = @should.have_valid(:name).when(nil)
|
33
33
|
matcher.matches?(@user).should be_true
|
34
34
|
end
|
35
35
|
|
36
36
|
it 'passes with values set' do
|
37
|
-
matcher = @should.have_valid(:name).
|
37
|
+
matcher = @should.have_valid(:name).when('Brian', 'Stephanie')
|
38
38
|
matcher.matches?(@user).should be_true
|
39
39
|
end
|
40
40
|
end
|
@@ -46,17 +46,17 @@ describe 'ValidAttribute' do
|
|
46
46
|
end
|
47
47
|
|
48
48
|
it 'returns false when no message passed' do
|
49
|
-
matcher = @should.have_valid(:name)
|
49
|
+
matcher = @should.have_valid(:name).when(nil)
|
50
50
|
matcher.matches?(@user).should be_false
|
51
51
|
end
|
52
52
|
|
53
53
|
it 'returns true when wrong message is passed' do
|
54
|
-
matcher = @should.have_valid(:name).message('wrong message')
|
54
|
+
matcher = @should.have_valid(:name).when(nil).message('wrong message')
|
55
55
|
matcher.matches?(@user).should_not be_false
|
56
56
|
end
|
57
57
|
|
58
58
|
it 'returns false when correct message is passed' do
|
59
|
-
matcher = @should.have_valid(:name).message('is not valid')
|
59
|
+
matcher = @should.have_valid(:name).when(nil).message('is not valid')
|
60
60
|
matcher.matches?(@user).should be_false
|
61
61
|
end
|
62
62
|
end
|
@@ -68,7 +68,7 @@ describe 'ValidAttribute' do
|
|
68
68
|
end
|
69
69
|
|
70
70
|
it 'returns false' do
|
71
|
-
matcher = @should.have_valid(:name).
|
71
|
+
matcher = @should.have_valid(:name).when('true', 'false')
|
72
72
|
matcher.matches?(@user).should be_false
|
73
73
|
end
|
74
74
|
end
|
@@ -80,13 +80,13 @@ describe 'ValidAttribute' do
|
|
80
80
|
end
|
81
81
|
|
82
82
|
it 'has a message for string values' do
|
83
|
-
matcher = @should.have_valid(:name).
|
83
|
+
matcher = @should.have_valid(:name).when('Brian')
|
84
84
|
matcher.matches?(@user)
|
85
85
|
matcher.failure_message.should == " expected User#name to accept a value of 'Brian'"
|
86
86
|
end
|
87
87
|
|
88
88
|
it 'has a message for non string values' do
|
89
|
-
matcher = @should.have_valid(:name).
|
89
|
+
matcher = @should.have_valid(:name).when(123)
|
90
90
|
matcher.matches?(@user)
|
91
91
|
matcher.failure_message.should == " expected User#name to accept a value of 123"
|
92
92
|
end
|
@@ -99,22 +99,29 @@ describe 'ValidAttribute' do
|
|
99
99
|
end
|
100
100
|
|
101
101
|
it 'has a message for string values' do
|
102
|
-
matcher = @should.have_valid(:name).
|
102
|
+
matcher = @should.have_valid(:name).when('Brian')
|
103
103
|
matcher.matches?(@user)
|
104
104
|
matcher.negative_failure_message.should == " expected User#name to not accept a value of 'Brian'"
|
105
105
|
end
|
106
106
|
|
107
107
|
it 'has a message for non string values' do
|
108
|
-
matcher = @should.have_valid(:name).
|
108
|
+
matcher = @should.have_valid(:name).when(123)
|
109
109
|
matcher.matches?(@user)
|
110
110
|
matcher.negative_failure_message.should == " expected User#name to not accept a value of 123"
|
111
111
|
end
|
112
112
|
|
113
113
|
it 'includes the validation message' do
|
114
|
-
matcher = @should.have_valid(:name).
|
114
|
+
matcher = @should.have_valid(:name).when('Brian').message('is not valid')
|
115
115
|
matcher.matches?(@user)
|
116
116
|
matcher.negative_failure_message.should == " expected User#name to not accept a value of 'Brian' with a message of 'is not valid'"
|
117
117
|
end
|
118
118
|
end
|
119
119
|
|
120
|
+
it 'requires .when to always be used' do
|
121
|
+
matcher = @should.have_valid(:name)
|
122
|
+
expect do
|
123
|
+
matcher.matches?(@user)
|
124
|
+
end.to raise_error ValidAttribute::NoValues, "you need to set the values with .when on the matcher (ex. it { should have_valid(:name).when('Brian') })"
|
125
|
+
end
|
126
|
+
|
120
127
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: valid_attribute
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0.
|
5
|
+
version: 0.0.2
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Brian Cardarella
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-04-
|
13
|
+
date: 2011-04-05 00:00:00 -04:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
@@ -60,6 +60,7 @@ files:
|
|
60
60
|
- .rspec
|
61
61
|
- .rvmrc
|
62
62
|
- Gemfile
|
63
|
+
- HISTORY
|
63
64
|
- README.markdown
|
64
65
|
- Rakefile
|
65
66
|
- lib/valid_attribute.rb
|