valid_attribute 0.2.1 → 1.0.0.rc.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -1,79 +1,110 @@
1
1
  # ValidAttribute #
2
2
 
3
- ValidAttribute is a minimalist framework for validation BDD.
3
+ ValidAttribute is a minimalist matcher for validation BDD.
4
+
5
+ Supported ORMs
6
+
7
+ * ActiveModel based (ActiveRecord >= 3.0, Mongoid >= 2.0, MongoMapper >= 0.9
8
+ * ActiveRecord <= 2.3
9
+ * DataMapper
10
+ * Custom (with compatible API, see below)
4
11
 
5
12
  ## Installation ##
6
13
 
7
14
  If you're using `RSpec` just add the `valid_attribute` to your `Gemfile`
8
15
 
9
- gem 'valid_attribute'
16
+ ```ruby
17
+ gem 'valid_attribute'
18
+ ```
10
19
 
11
20
  Then add it to your `spec_helper.rb`
12
21
 
13
- require 'valid_attribute'
22
+ ```ruby
23
+ require 'valid_attribute'
24
+ ```
14
25
 
15
26
  or if you're using `Test::Unit`, you must use [Thoughtbot](http://thoughtbot.com)'s [shoulda-context](https://github.com/thoughtbot/shoulda-context)
16
27
 
17
- # Gemfile
18
- gem 'shoulda-context'
28
+ ```ruby
29
+ # Gemfile
30
+ gem 'shoulda-context'
19
31
 
20
- # test_helper.rb
21
- require 'shoulda-context'
22
- require 'valid_attribute'
32
+ # test_helper.rb
33
+ require 'shoulda-context'
34
+ require 'valid_attribute'
35
+ ```
23
36
 
24
37
  ## Usage ##
25
38
 
26
39
  Instead of having validation specific matchers `ValidAttribute` only cares if the attribute is valid under certain circumstances
27
40
 
28
- class User
29
- include ActiveModel::Validations
30
-
31
- attr_accessor :email, :name, :password
32
-
33
- validates :email, :format => { :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i }
34
- validates :name, :length => { :minimum => 5 }
35
- validates :password, :confirmation => true, :presence => true
36
- end
37
-
38
- # RSpec
39
- describe User do
40
- # The .when method can take any number of values that you want to pass
41
- it { should have_valid(:email).when('test@test.com', 'test+spam@gmail.com') }
42
- it { should_not have_valid(:email).when('fail', 123) }
43
- it { should have_valid(:name).when('TestName') }
44
- it { should_not have_valid(:name).when('Test') }
45
-
46
- # Because 'should' works off the the 'subject' in RSpec we can set other values if necessary for a given validation test
47
- describe 'password' do
48
- before { subject.password_confirmation = 'password' }
49
- it { should have_valid(:password).when('password') }
50
- it { should_not have_valid(:password).when(nil) }
51
- end
52
- end
53
-
54
- # TestUnit
55
- require 'should/context'
56
- class UserTest < Test::Unit::TestCase
57
- # The .when method can take any number of values that you want to pass
58
- should have_valid(:email).when('test@test.com', 'test+spam@gmail.com')
59
- should_not have_valid(:email).when('fail', 123)
60
- should have_valid(:name).when('TestName')
61
- should_not have_valid(:name).when('Test')
62
-
63
- # Because 'shoulda-context' works off the the 'subject' we can set other values if necessary for a given validation test
64
- context 'password' do
65
- subject { User.new(:password_confirmation => 'password') }
66
- should have_valid(:password).when('password')
67
- should_not have_valid(:password).when(nil)
68
- end
69
- end
70
-
71
- ## Non-ActiveModel models ##
41
+ ```ruby
42
+ class User
43
+ include ActiveModel::Validations
44
+
45
+ attr_accessor :email, :name, :password
46
+
47
+ validates :email, :format => { :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i }
48
+ validates :name, :length => { :minimum => 5 }
49
+ validates :password, :confirmation => true, :presence => true
50
+ end
51
+
52
+ # RSpec
53
+ describe User do
54
+ # The .when method can take any number of values that you want to pass
55
+ it { should have_valid(:email).when('test@test.com', 'test+spam@gmail.com') }
56
+ it { should_not have_valid(:email).when('fail', 123) }
57
+ it { should have_valid(:name).when('TestName') }
58
+ it { should_not have_valid(:name).when('Test') }
59
+
60
+ # Because 'should' works off the the 'subject' in RSpec we can set other values if necessary for a given validation test
61
+ describe 'password' do
62
+ before { subject.password_confirmation = 'password' }
63
+ it { should have_valid(:password).when('password') }
64
+ it { should_not have_valid(:password).when(nil) }
65
+ end
66
+ end
67
+
68
+ # TestUnit
69
+ require 'should/context'
70
+ class UserTest < Test::Unit::TestCase
71
+ # The .when method can take any number of values that you want to pass
72
+ should have_valid(:email).when('test@test.com', 'test+spam@gmail.com')
73
+ should_not have_valid(:email).when('fail', 123)
74
+ should have_valid(:name).when('TestName')
75
+ should_not have_valid(:name).when('Test')
76
+
77
+ # Because 'shoulda-context' works off the the 'subject' we can set other values if necessary for a given validation test
78
+ context 'password' do
79
+ subject { User.new(:password_confirmation => 'password') }
80
+ should have_valid(:password).when('password')
81
+ should_not have_valid(:password).when(nil)
82
+ end
83
+ end
84
+ ```
85
+
86
+ ## Custom Models ##
72
87
 
73
88
  Your model should respond to the following methods:
74
89
 
75
90
  * `valid?` - only used to generate errors on the model
76
- * `errors` - should be a collection of attributes that have validation errors.
91
+ * `errors` - should be a Hash of attributes that have the invalid attributes as keys.
92
+
93
+ The following would be a compatible (albeit unrealistic) model:
94
+
95
+ ```ruby
96
+ class User
97
+ attr_accessor :name
98
+
99
+ def errors
100
+ {:name => ["can't be blank"]}
101
+ end
102
+
103
+ def valid?
104
+ false
105
+ end
106
+ end
107
+ ```
77
108
 
78
109
  Other than that everything should work!
79
110
 
@@ -55,7 +55,8 @@ module ValidAttribute
55
55
  values.each do |value|
56
56
  subject.send("#{attr}=", value)
57
57
  subject.valid?
58
- if subject.errors.key?(attr)
58
+
59
+ if invalid_attribute?(subject, attr)
59
60
  self.failed_values << value
60
61
  else
61
62
  self.passed_values << value
@@ -63,6 +64,11 @@ module ValidAttribute
63
64
  end
64
65
  end
65
66
 
67
+ def invalid_attribute?(subject, attr)
68
+ errors = subject.errors[attr]
69
+ errors.respond_to?(:empty?) ? !errors.empty? : !!errors
70
+ end
71
+
66
72
  def quote_values(values)
67
73
  values.map { |value| value.inspect }.join(', ')
68
74
  end
@@ -1,3 +1,3 @@
1
1
  module ValidAttribute
2
- VERSION = "0.2.1"
2
+ VERSION = "1.0.0.rc.1"
3
3
  end
data/spec/user.rb CHANGED
@@ -6,7 +6,7 @@ class User
6
6
  @error ||= {}
7
7
  end
8
8
 
9
- def self.model_name
10
- 'User'
9
+ def valid?
10
+ errors.empty?
11
11
  end
12
12
  end
@@ -39,7 +39,7 @@ describe 'ValidAttribute' do
39
39
  context 'data is invalid' do
40
40
  before do
41
41
  @user.stubs(:valid?).returns(false)
42
- @user.stubs(:errors).returns({:name => []})
42
+ @user.stubs(:errors).returns({:name => ["can't be blank"]})
43
43
  @matcher = @should.have_valid(:name).when(:abc, nil)
44
44
  end
45
45
 
@@ -62,7 +62,7 @@ describe 'ValidAttribute' do
62
62
  context 'data is valid then invalid' do
63
63
  before do
64
64
  @user.stubs(:valid?).returns(true).then.returns(false)
65
- @user.stubs(:errors).returns({}).then.returns({:name => []})
65
+ @user.stubs(:errors).returns({}).then.returns({:name => ["can't be blank"]})
66
66
  @matcher = @should.have_valid(:name).when('abc', 123)
67
67
  end
68
68
 
@@ -9,8 +9,8 @@ Gem::Specification.new do |s|
9
9
  s.authors = ["Brian Cardarella"]
10
10
  s.email = ["bcardarella@gmail.com"]
11
11
  s.homepage = "https://github.com/bcardarella/valid_attribute"
12
- s.summary = %q{Minimalist validation matcher framework}
13
- s.description = %q{Minimalist validation matcher framework}
12
+ s.summary = %q{Minimalist validation matcher}
13
+ s.description = %q{Minimalist validation matcher}
14
14
 
15
15
  s.rubyforge_project = "valid_attribute"
16
16
 
metadata CHANGED
@@ -1,8 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: valid_attribute
3
3
  version: !ruby/object:Gem::Version
4
- prerelease:
5
- version: 0.2.1
4
+ prerelease: 6
5
+ version: 1.0.0.rc.1
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-05-28 00:00:00 -04:00
13
+ date: 2011-06-06 00:00:00 -04:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -46,7 +46,7 @@ dependencies:
46
46
  version: "0"
47
47
  type: :development
48
48
  version_requirements: *id003
49
- description: Minimalist validation matcher framework
49
+ description: Minimalist validation matcher
50
50
  email:
51
51
  - bcardarella@gmail.com
52
52
  executables: []
@@ -92,16 +92,16 @@ required_ruby_version: !ruby/object:Gem::Requirement
92
92
  required_rubygems_version: !ruby/object:Gem::Requirement
93
93
  none: false
94
94
  requirements:
95
- - - ">="
95
+ - - ">"
96
96
  - !ruby/object:Gem::Version
97
- version: "0"
97
+ version: 1.3.1
98
98
  requirements: []
99
99
 
100
100
  rubyforge_project: valid_attribute
101
- rubygems_version: 1.5.3
101
+ rubygems_version: 1.3.8
102
102
  signing_key:
103
103
  specification_version: 3
104
- summary: Minimalist validation matcher framework
104
+ summary: Minimalist validation matcher
105
105
  test_files:
106
106
  - spec/spec_helper.rb
107
107
  - spec/test_unit_spec.rb