valid_attribute 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/HISTORY +3 -0
- data/lib/valid_attribute/version.rb +1 -1
- data/lib/valid_attribute.rb +16 -8
- data/spec/valid_attribute_spec.rb +22 -12
- metadata +2 -2
data/HISTORY
CHANGED
data/lib/valid_attribute.rb
CHANGED
@@ -19,9 +19,7 @@ module ValidAttribute
|
|
19
19
|
attr_accessor :attr, :values, :subject, :failed_values, :passed_values
|
20
20
|
|
21
21
|
def initialize(attr)
|
22
|
-
self.attr
|
23
|
-
self.failed_values = []
|
24
|
-
self.passed_values = []
|
22
|
+
self.attr = attr
|
25
23
|
end
|
26
24
|
|
27
25
|
def when(*values)
|
@@ -46,11 +44,25 @@ module ValidAttribute
|
|
46
44
|
end
|
47
45
|
|
48
46
|
def matches?(subject)
|
47
|
+
check_values(subject)
|
48
|
+
failed_values.empty?
|
49
|
+
end
|
50
|
+
|
51
|
+
def does_not_match?(subject)
|
52
|
+
check_values(subject)
|
53
|
+
passed_values.empty?
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
|
58
|
+
def check_values(subject)
|
49
59
|
unless values
|
50
60
|
raise ::ValidAttribute::NoValues, "you need to set the values with .when on the matcher (ex. it { should have_valid(:name).when('Brian') })"
|
51
61
|
end
|
52
62
|
|
53
|
-
self.subject
|
63
|
+
self.subject = subject
|
64
|
+
self.failed_values = []
|
65
|
+
self.passed_values = []
|
54
66
|
|
55
67
|
values.each do |value|
|
56
68
|
subject.send("#{attr}=", value)
|
@@ -61,12 +73,8 @@ module ValidAttribute
|
|
61
73
|
self.passed_values << value
|
62
74
|
end
|
63
75
|
end
|
64
|
-
|
65
|
-
failed_values.empty?
|
66
76
|
end
|
67
77
|
|
68
|
-
private
|
69
|
-
|
70
78
|
def quote_values(values)
|
71
79
|
values.map { |value| value.is_a?(String) ? "'#{value}'" : value }.join(', ')
|
72
80
|
end
|
@@ -37,14 +37,19 @@ describe 'ValidAttribute' do
|
|
37
37
|
|
38
38
|
describe 'data is first invalid then invalid' do
|
39
39
|
before do
|
40
|
-
@user.stubs(:valid?).returns(false).then.returns(true).then.returns(false)
|
41
|
-
@user.stubs(:errors).returns({:name => []}).then.returns({})
|
42
|
-
@matcher
|
43
|
-
@
|
40
|
+
@user.stubs(:valid?).returns(false).then.returns(true).then.returns(false).then.returns(true)
|
41
|
+
@user.stubs(:errors).returns({:name => []}).then.returns({}).then.returns({:name => []}).then.returns({})
|
42
|
+
@matcher = @should.have_valid(:name).when('abc', 123)
|
43
|
+
@matches = @matcher.matches?(@user)
|
44
|
+
@does_not_match = @matcher.does_not_match?(@user)
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'matches? returns false' do
|
48
|
+
@matches.should be_false
|
44
49
|
end
|
45
50
|
|
46
|
-
it 'returns false' do
|
47
|
-
@
|
51
|
+
it 'does_not_match? returns false' do
|
52
|
+
@does_not_match.should be_false
|
48
53
|
end
|
49
54
|
|
50
55
|
it 'has a failure message of the failed values' do
|
@@ -58,14 +63,19 @@ describe 'ValidAttribute' do
|
|
58
63
|
|
59
64
|
describe 'data is first invalid then valid then invalid then valid' do
|
60
65
|
before do
|
61
|
-
@user.stubs(:valid?).returns(false).then.returns(true).then.returns(false)
|
62
|
-
@user.stubs(:errors).returns({:name => []}).then.returns({}).then.returns({:name => []}).then.returns({})
|
63
|
-
@matcher
|
64
|
-
@
|
66
|
+
@user.stubs(:valid?).returns(false).then.returns(true).then.returns(false).then.returns(false).then.returns(true).then.returns(false)
|
67
|
+
@user.stubs(:errors).returns({:name => []}).then.returns({}).then.returns({:name => []}).then.returns({}).then.returns({:name => []}).then.returns({}).then.returns({:name => []}).then.returns({})
|
68
|
+
@matcher = @should.have_valid(:name).when('abc', 123, 456, 'def')
|
69
|
+
@matches = @matcher.matches?(@user)
|
70
|
+
@does_not_match = @matcher.does_not_match?(@user)
|
71
|
+
end
|
72
|
+
|
73
|
+
it 'matches? returns false' do
|
74
|
+
@matches.should be_false
|
65
75
|
end
|
66
76
|
|
67
|
-
it 'returns
|
68
|
-
@
|
77
|
+
it 'does_not_match? returns true' do
|
78
|
+
@does_not_match.should be_false
|
69
79
|
end
|
70
80
|
|
71
81
|
it 'has a failure message of the failed values' do
|
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.4
|
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-
|
13
|
+
date: 2011-05-05 00:00:00 -04:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|