valid_attribute 1.0.0 → 1.1.0

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/HISTORY CHANGED
@@ -1,3 +1,9 @@
1
+ == 1.1.0
2
+ #when is not necessary [davisre]
3
+
4
+ == 1.0.0
5
+ Gold!
6
+
1
7
  == 0.2.1
2
8
  Better value quoting using #inspect
3
9
 
data/README.markdown CHANGED
@@ -11,7 +11,7 @@ Supported ORMs
11
11
 
12
12
  ## Installation ##
13
13
 
14
- If you're using `RSpec` just add the `valid_attribute` to your `Gemfile`
14
+ If you're using `RSpec` just add the `valid_attribute` to your `Gemfile` AFTER rspec gem.
15
15
 
16
16
  ```ruby
17
17
  gem 'valid_attribute'
@@ -63,10 +63,13 @@ describe User do
63
63
  it { should have_valid(:password).when('password') }
64
64
  it { should_not have_valid(:password).when(nil) }
65
65
  end
66
+
67
+ # Using .when is optional. Without it, the existing value is examined.
68
+ it { should_not have_valid(:email) }
66
69
  end
67
70
 
68
71
  # TestUnit
69
- require 'should/context'
72
+ require 'shoulda/context'
70
73
  class UserTest < Test::Unit::TestCase
71
74
  # The .when method can take any number of values that you want to pass
72
75
  should have_valid(:email).when('test@test.com', 'test+spam@gmail.com')
@@ -74,12 +77,15 @@ class UserTest < Test::Unit::TestCase
74
77
  should have_valid(:name).when('TestName')
75
78
  should_not have_valid(:name).when('Test')
76
79
 
77
- # Because 'shoulda-context' works off the the 'subject' we can set other values if necessary for a given validation test
80
+ # Because 'shoulda-context' works off the 'subject' we can set other values if necessary for a given validation test
78
81
  context 'password' do
79
82
  subject { User.new(:password_confirmation => 'password') }
80
83
  should have_valid(:password).when('password')
81
84
  should_not have_valid(:password).when(nil)
82
85
  end
86
+
87
+ # Using .when is optional. Without it, the existing value is examined.
88
+ should_not have_valid(:email)
83
89
  end
84
90
  ```
85
91
 
@@ -106,8 +112,6 @@ class User
106
112
  end
107
113
  ```
108
114
 
109
- Other than that everything should work!
110
-
111
115
  ## Legal ##
112
116
 
113
117
  Brian Cardarella &copy; 2011
@@ -21,9 +21,9 @@ module ValidAttribute
21
21
 
22
22
  def negative_failure_message
23
23
  if passed_values.size == 1
24
- " expected #{subject.class}##{attr} to not accept the value: #{quote_values(passed_values)}"
24
+ " expected #{subject.class}##{attr} to reject the value: #{quote_values(passed_values)}"
25
25
  else
26
- " expected #{subject.class}##{attr} to not accept the values: #{quote_values(passed_values)}"
26
+ " expected #{subject.class}##{attr} to reject the values: #{quote_values(passed_values)}"
27
27
  end
28
28
  end
29
29
 
@@ -44,23 +44,35 @@ module ValidAttribute
44
44
  private
45
45
 
46
46
  def check_values(subject)
47
- unless values
48
- raise ::ValidAttribute::NoValues, "you need to set the values with .when on the matcher. Example: have_valid(:name).when('Brian')"
49
- end
50
-
51
47
  self.subject = subject
52
48
  self.failed_values = []
53
49
  self.passed_values = []
54
50
 
51
+ if values
52
+ check_specified_values
53
+ else
54
+ check_existing_value
55
+ end
56
+ end
57
+
58
+ def check_specified_values
55
59
  values.each do |value|
56
- subject.send("#{attr}=", value)
57
- subject.valid?
58
-
59
- if invalid_attribute?(subject, attr)
60
- self.failed_values << value
61
- else
62
- self.passed_values << value
63
- end
60
+ check_value value
61
+ end
62
+ end
63
+
64
+ def check_existing_value
65
+ check_value subject.send("#{attr}")
66
+ end
67
+
68
+ def check_value(value)
69
+ subject.send("#{attr}=", value)
70
+ subject.valid?
71
+
72
+ if invalid_attribute?(subject, attr)
73
+ self.failed_values << value
74
+ else
75
+ self.passed_values << value
64
76
  end
65
77
  end
66
78
 
@@ -1,3 +1,3 @@
1
1
  module ValidAttribute
2
- VERSION = '1.0.0'
2
+ VERSION = '1.1.0'
3
3
  end
@@ -31,7 +31,7 @@ describe 'ValidAttribute' do
31
31
  describe 'messages' do
32
32
  it '#negative_failue_message' do
33
33
  @matcher.matches?(@user)
34
- @matcher.negative_failure_message.should == " expected User#name to not accept the values: \"abc\", 123"
34
+ @matcher.negative_failure_message.should == " expected User#name to reject the values: \"abc\", 123"
35
35
  end
36
36
  end
37
37
  end
@@ -82,7 +82,7 @@ describe 'ValidAttribute' do
82
82
 
83
83
  it '#negative_failure_message' do
84
84
  @matcher.matches?(@user)
85
- @matcher.negative_failure_message.should == " expected User#name to not accept the value: \"abc\""
85
+ @matcher.negative_failure_message.should == " expected User#name to reject the value: \"abc\""
86
86
  end
87
87
 
88
88
  it '#description' do
@@ -90,13 +90,55 @@ describe 'ValidAttribute' do
90
90
  end
91
91
  end
92
92
  end
93
- end
94
93
 
95
- it 'requires .when to always be used' do
96
- matcher = @should.have_valid(:name)
97
- expect do
98
- matcher.matches?(@user)
99
- end.to raise_error ValidAttribute::NoValues, "you need to set the values with .when on the matcher. Example: have_valid(:name).when('Brian')"
94
+ context 'no values are specified with .when' do
95
+ context 'data is valid' do
96
+ before do
97
+ @user.stubs(:valid?).returns(true)
98
+ @user.stubs(:name).returns(:abc)
99
+ @matcher = @should.have_valid(:name)
100
+ end
101
+
102
+ it 'matches? returns true' do
103
+ @matcher.matches?(@user).should be_true
104
+ end
105
+
106
+ it 'does_not_match? returns false' do
107
+ @matcher.does_not_match?(@user).should be_false
108
+ end
109
+
110
+ describe 'messages' do
111
+ it '#negative_failue_message' do
112
+ @matcher.matches?(@user)
113
+ @matcher.negative_failure_message.should == " expected User#name to reject the value: :abc"
114
+ end
115
+ end
116
+ end
117
+
118
+ context 'data is invalid' do
119
+ before do
120
+ @user.stubs(:valid?).returns(false)
121
+ @user.stubs(:errors).returns({:name => ["can't be a symbol"]})
122
+ @user.stubs(:name).returns(:abc)
123
+ @matcher = @should.have_valid(:name)
124
+ end
125
+
126
+ it 'matches? returns false' do
127
+ @matcher.matches?(@user).should be_false
128
+ end
129
+
130
+ it 'does_not_match? returns true' do
131
+ @matcher.does_not_match?(@user).should be_true
132
+ end
133
+
134
+ describe 'messages' do
135
+ it '#failue_message' do
136
+ @matcher.matches?(@user)
137
+ @matcher.failure_message.should == " expected User#name to accept the value: :abc"
138
+ end
139
+ end
140
+ end
141
+ end
100
142
  end
101
143
 
102
144
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: valid_attribute
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 1.0.0
5
+ version: 1.1.0
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-06-12 00:00:00 -04:00
13
+ date: 2011-07-08 00:00:00 -04:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency