subset_validator 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem 'activemodel', '~> 3.0.3', :require => 'active_model'
4
+
5
+ group :development do
6
+ gem "rspec", "~> 2.1.0"
7
+ gem "bundler", "~> 1.0.0"
8
+ gem "jeweler", "~> 1.5.1"
9
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,34 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activemodel (3.0.3)
5
+ activesupport (= 3.0.3)
6
+ builder (~> 2.1.2)
7
+ i18n (~> 0.4)
8
+ activesupport (3.0.3)
9
+ builder (2.1.2)
10
+ diff-lcs (1.1.2)
11
+ git (1.2.5)
12
+ i18n (0.4.2)
13
+ jeweler (1.5.1)
14
+ bundler (~> 1.0.0)
15
+ git (>= 1.2.5)
16
+ rake
17
+ rake (0.8.7)
18
+ rspec (2.1.0)
19
+ rspec-core (~> 2.1.0)
20
+ rspec-expectations (~> 2.1.0)
21
+ rspec-mocks (~> 2.1.0)
22
+ rspec-core (2.1.0)
23
+ rspec-expectations (2.1.0)
24
+ diff-lcs (~> 1.1.2)
25
+ rspec-mocks (2.1.0)
26
+
27
+ PLATFORMS
28
+ ruby
29
+
30
+ DEPENDENCIES
31
+ activemodel (~> 3.0.3)
32
+ bundler (~> 1.0.0)
33
+ jeweler (~> 1.5.1)
34
+ rspec (~> 2.1.0)
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Nick Hoffman
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,80 @@
1
+ = subset_validator
2
+
3
+
4
+ == Description
5
+
6
+ An ActiveModel validation for checking if an attribute's values are a subset of another set.
7
+
8
+ In other words, check that an array is a subset of another array, a hash is a subset of another hash, etc.
9
+
10
+
11
+ == Usage Example
12
+
13
+ Say you have a User model, and each user can multiple roles, like this:
14
+ class User
15
+ attr_accessor :roles
16
+
17
+ @@valid_roles = %w(admin manager developer)
18
+
19
+ def initialize(roles)
20
+ raise ArgumentError, 'The roles must be an array.' unless roles.is_a? Array
21
+
22
+ @roles = roles
23
+ end
24
+ end
25
+ You probably want your model to prevent arbitrary roles from being assigned. Eg:
26
+ the_boss = User.new %w(bonehead)
27
+ One solution is to write a custom setter method that checks if each element of the _roles_ array is contained in <i>@@valid_roles</i> . That's a pain in the ass, especially if you need to do it more than once.
28
+
29
+ Enter SubsetValidator:
30
+ require 'subset_validator'
31
+
32
+ class User
33
+ include ActiveModel::Validations
34
+
35
+ attr_accessor :roles
36
+
37
+ @@valid_roles = %w(admin manager developer)
38
+
39
+ validates :roles, :subset => {:in => @@valid_roles}
40
+
41
+ def initialize(roles)
42
+ raise ArgumentError, 'The roles must be an array.' unless roles.is_a? Array
43
+
44
+ @roles = roles
45
+ end
46
+ end
47
+ That one validation takes care of it all for you:
48
+ the_boss = User.new %w(bonehead)
49
+ => #<User:0x9980a04 @roles=["bonehead"]>
50
+
51
+ the_boss.valid?
52
+ => false
53
+ the_boss.errors
54
+ => {:roles=>["contains an invalid value"]}
55
+
56
+ the_boss.roles = %w(manager)
57
+ => ["manager"]
58
+ the_boss.valid?
59
+ => true
60
+
61
+
62
+ SubsetValidator works on more than just arrays, too. In fact, it'll work on anything, provided that:
63
+ * the attribute's value can be anything that responds to <i>#each</i> ;
64
+ * the set of valid values can be anything that responds to <i>#include?</i> .
65
+
66
+
67
+ == Contributing to subset_validator
68
+
69
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
70
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
71
+ * Fork the project
72
+ * Start a feature/bugfix branch
73
+ * Commit and push until you are happy with your contribution
74
+ * Make sure to add RSpec specs for it. This is important so I don't break it in a future version unintentionally.
75
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate that to its own commit so I can cherry-pick around it.
76
+
77
+ == Copyright
78
+
79
+ Copyright (c) 2010 Nick Hoffman. See LICENSE.txt for further details.
80
+
data/Rakefile ADDED
@@ -0,0 +1,46 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ gem.name = "subset_validator"
15
+ gem.homepage = "http://github.com/nickhoffman/subset_validator"
16
+ gem.license = "MIT"
17
+ gem.summary = %Q{An ActiveModel validation for checking if an attribute's values are a subset of another set.}
18
+ gem.description = %Q{An ActiveModel validation for checking if an attribute's values are a subset of another set.}
19
+ gem.email = "nick@deadorange.com"
20
+ gem.authors = ["Nick Hoffman"]
21
+
22
+ gem.add_dependency 'activemodel', '~> 3.0.3'
23
+
24
+ gem.add_development_dependency "rspec", "~> 2.1.0"
25
+ gem.add_development_dependency "bundler", "~> 1.0.0"
26
+ gem.add_development_dependency "jeweler", "~> 1.5.1"
27
+ end
28
+ Jeweler::RubygemsDotOrgTasks.new
29
+
30
+ require 'rspec/core'
31
+ require 'rspec/core/rake_task'
32
+ RSpec::Core::RakeTask.new(:spec) do |spec|
33
+ spec.pattern = FileList['spec/**/*_spec.rb']
34
+ end
35
+
36
+ task :default => :spec
37
+
38
+ require 'rake/rdoctask'
39
+ Rake::RDocTask.new do |rdoc|
40
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
41
+
42
+ rdoc.rdoc_dir = 'rdoc'
43
+ rdoc.title = "subset_validator #{version}"
44
+ rdoc.rdoc_files.include('README*')
45
+ rdoc.rdoc_files.include('lib/**/*.rb')
46
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.3.0
@@ -0,0 +1,22 @@
1
+ class SubsetValidator < ActiveModel::EachValidator
2
+ @@error_message = 'contains an invalid value'
3
+
4
+ def validate_each(record, attribute, value)
5
+ unless options[:in].respond_to? :include?
6
+ raise ArgumentError, 'An object with the method include? must be supplied as the :in option of the configuration hash'
7
+ end
8
+
9
+ unless value.respond_to? :each
10
+ raise ArgumentError, 'The attribute being validated must respond to #each'
11
+ end
12
+
13
+ error_message = options[:message] || @@error_message
14
+
15
+ value.each do |element|
16
+ unless options[:in].include? element
17
+ record.errors.add attribute, error_message
18
+ break
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,13 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'active_model'
5
+ require 'subset_validator'
6
+
7
+ # Requires supporting files with custom matchers and macros, etc,
8
+ # in ./support/ and its subdirectories.
9
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
10
+
11
+ RSpec.configure do |config|
12
+
13
+ end
@@ -0,0 +1,78 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ class UserTest
4
+ include ActiveModel::Validations
5
+
6
+ @@valid_values = %w(echidna wombat tuatara)
7
+
8
+ attr_accessor :missing_in
9
+ validates :missing_in, :allow_blank => true, :subset => true
10
+
11
+ attr_accessor :doesnt_respond_to_each
12
+ validates :doesnt_respond_to_each, :allow_blank => true, :subset => {:in => @@valid_values}
13
+
14
+ attr_accessor :contains_invalid_value
15
+ validates :contains_invalid_value,
16
+ :allow_blank => true,
17
+ :subset => {:in => @@valid_values}
18
+
19
+ attr_accessor :custom_error_message
20
+ validates :custom_error_message,
21
+ :allow_blank => true,
22
+ :subset => {:in => @@valid_values, :message => 'a custom error message'}
23
+
24
+ def initialize(params = {})
25
+ params.each {|k, v| send "#{k}=", v}
26
+ end
27
+ end
28
+
29
+ describe SubsetValidator do
30
+ it 'inherits from ActiveModel::EachValidator' do
31
+ SubsetValidator.superclass.should equal ActiveModel::EachValidator
32
+ end
33
+
34
+ describe 'class variables' do
35
+ it 'has @@error_message' do
36
+ SubsetValidator.class_variables.should include :@@error_message
37
+ end
38
+ end
39
+
40
+ describe '#validate_each' do
41
+ describe "when the configuration hash's :in element is invalid" do
42
+ it 'raises an error' do
43
+ expect {UserTest.new(:missing_in => 'fails').valid?}.to raise_error(
44
+ ArgumentError,
45
+ 'An object with the method include? must be supplied as the :in option of the configuration hash'
46
+ )
47
+ end
48
+ end
49
+
50
+ describe "when the attribute doesn't respond to #each" do
51
+ it 'raises an error' do
52
+ expect {UserTest.new(:doesnt_respond_to_each => 'fails').valid?}.to raise_error(
53
+ ArgumentError,
54
+ 'The attribute being validated must respond to #each'
55
+ )
56
+ end
57
+ end
58
+
59
+ describe 'when the attribute contains an invalid value' do
60
+ it 'adds an error on the attribute' do
61
+ u = UserTest.new :contains_invalid_value => %w(asdf)
62
+ u.valid?
63
+
64
+ u.errors[:contains_invalid_value].count.should equal 1
65
+ u.errors[:contains_invalid_value].first.should == 'contains an invalid value'
66
+ end
67
+
68
+ describe 'when a custom error message is given' do
69
+ it 'uses the custom error message' do
70
+ u = UserTest.new :custom_error_message => %w(foobar)
71
+ u.valid?
72
+
73
+ u.errors[:custom_error_message].first.should == 'a custom error message'
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,77 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{subset_validator}
8
+ s.version = "0.3.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Nick Hoffman"]
12
+ s.date = %q{2010-11-22}
13
+ s.description = %q{An ActiveModel validation for checking if an attribute's values are a subset of another set.}
14
+ s.email = %q{nick@deadorange.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".rspec",
22
+ "Gemfile",
23
+ "Gemfile.lock",
24
+ "LICENSE.txt",
25
+ "README.rdoc",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "lib/subset_validator.rb",
29
+ "spec/spec_helper.rb",
30
+ "spec/subset_validator_spec.rb",
31
+ "subset_validator.gemspec"
32
+ ]
33
+ s.homepage = %q{http://github.com/nickhoffman/subset_validator}
34
+ s.licenses = ["MIT"]
35
+ s.require_paths = ["lib"]
36
+ s.rubygems_version = %q{1.3.7}
37
+ s.summary = %q{An ActiveModel validation for checking if an attribute's values are a subset of another set.}
38
+ s.test_files = [
39
+ "spec/spec_helper.rb",
40
+ "spec/subset_validator_spec.rb"
41
+ ]
42
+
43
+ if s.respond_to? :specification_version then
44
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
45
+ s.specification_version = 3
46
+
47
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
48
+ s.add_runtime_dependency(%q<activemodel>, ["~> 3.0.3"])
49
+ s.add_development_dependency(%q<rspec>, ["~> 2.1.0"])
50
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
51
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.1"])
52
+ s.add_runtime_dependency(%q<activemodel>, ["~> 3.0.3"])
53
+ s.add_development_dependency(%q<rspec>, ["~> 2.1.0"])
54
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
55
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.1"])
56
+ else
57
+ s.add_dependency(%q<activemodel>, ["~> 3.0.3"])
58
+ s.add_dependency(%q<rspec>, ["~> 2.1.0"])
59
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
60
+ s.add_dependency(%q<jeweler>, ["~> 1.5.1"])
61
+ s.add_dependency(%q<activemodel>, ["~> 3.0.3"])
62
+ s.add_dependency(%q<rspec>, ["~> 2.1.0"])
63
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
64
+ s.add_dependency(%q<jeweler>, ["~> 1.5.1"])
65
+ end
66
+ else
67
+ s.add_dependency(%q<activemodel>, ["~> 3.0.3"])
68
+ s.add_dependency(%q<rspec>, ["~> 2.1.0"])
69
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
70
+ s.add_dependency(%q<jeweler>, ["~> 1.5.1"])
71
+ s.add_dependency(%q<activemodel>, ["~> 3.0.3"])
72
+ s.add_dependency(%q<rspec>, ["~> 2.1.0"])
73
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
74
+ s.add_dependency(%q<jeweler>, ["~> 1.5.1"])
75
+ end
76
+ end
77
+
metadata ADDED
@@ -0,0 +1,197 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: subset_validator
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 3
8
+ - 0
9
+ version: 0.3.0
10
+ platform: ruby
11
+ authors:
12
+ - Nick Hoffman
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-11-22 00:00:00 -05:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: activemodel
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 3
29
+ - 0
30
+ - 3
31
+ version: 3.0.3
32
+ type: :runtime
33
+ prerelease: false
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: rspec
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ~>
41
+ - !ruby/object:Gem::Version
42
+ segments:
43
+ - 2
44
+ - 1
45
+ - 0
46
+ version: 2.1.0
47
+ type: :development
48
+ prerelease: false
49
+ version_requirements: *id002
50
+ - !ruby/object:Gem::Dependency
51
+ name: bundler
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ~>
56
+ - !ruby/object:Gem::Version
57
+ segments:
58
+ - 1
59
+ - 0
60
+ - 0
61
+ version: 1.0.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: *id003
65
+ - !ruby/object:Gem::Dependency
66
+ name: jeweler
67
+ requirement: &id004 !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ~>
71
+ - !ruby/object:Gem::Version
72
+ segments:
73
+ - 1
74
+ - 5
75
+ - 1
76
+ version: 1.5.1
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *id004
80
+ - !ruby/object:Gem::Dependency
81
+ name: activemodel
82
+ requirement: &id005 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ~>
86
+ - !ruby/object:Gem::Version
87
+ segments:
88
+ - 3
89
+ - 0
90
+ - 3
91
+ version: 3.0.3
92
+ type: :runtime
93
+ prerelease: false
94
+ version_requirements: *id005
95
+ - !ruby/object:Gem::Dependency
96
+ name: rspec
97
+ requirement: &id006 !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ~>
101
+ - !ruby/object:Gem::Version
102
+ segments:
103
+ - 2
104
+ - 1
105
+ - 0
106
+ version: 2.1.0
107
+ type: :development
108
+ prerelease: false
109
+ version_requirements: *id006
110
+ - !ruby/object:Gem::Dependency
111
+ name: bundler
112
+ requirement: &id007 !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ~>
116
+ - !ruby/object:Gem::Version
117
+ segments:
118
+ - 1
119
+ - 0
120
+ - 0
121
+ version: 1.0.0
122
+ type: :development
123
+ prerelease: false
124
+ version_requirements: *id007
125
+ - !ruby/object:Gem::Dependency
126
+ name: jeweler
127
+ requirement: &id008 !ruby/object:Gem::Requirement
128
+ none: false
129
+ requirements:
130
+ - - ~>
131
+ - !ruby/object:Gem::Version
132
+ segments:
133
+ - 1
134
+ - 5
135
+ - 1
136
+ version: 1.5.1
137
+ type: :development
138
+ prerelease: false
139
+ version_requirements: *id008
140
+ description: An ActiveModel validation for checking if an attribute's values are a subset of another set.
141
+ email: nick@deadorange.com
142
+ executables: []
143
+
144
+ extensions: []
145
+
146
+ extra_rdoc_files:
147
+ - LICENSE.txt
148
+ - README.rdoc
149
+ files:
150
+ - .document
151
+ - .rspec
152
+ - Gemfile
153
+ - Gemfile.lock
154
+ - LICENSE.txt
155
+ - README.rdoc
156
+ - Rakefile
157
+ - VERSION
158
+ - lib/subset_validator.rb
159
+ - spec/spec_helper.rb
160
+ - spec/subset_validator_spec.rb
161
+ - subset_validator.gemspec
162
+ has_rdoc: true
163
+ homepage: http://github.com/nickhoffman/subset_validator
164
+ licenses:
165
+ - MIT
166
+ post_install_message:
167
+ rdoc_options: []
168
+
169
+ require_paths:
170
+ - lib
171
+ required_ruby_version: !ruby/object:Gem::Requirement
172
+ none: false
173
+ requirements:
174
+ - - ">="
175
+ - !ruby/object:Gem::Version
176
+ hash: -489382847
177
+ segments:
178
+ - 0
179
+ version: "0"
180
+ required_rubygems_version: !ruby/object:Gem::Requirement
181
+ none: false
182
+ requirements:
183
+ - - ">="
184
+ - !ruby/object:Gem::Version
185
+ segments:
186
+ - 0
187
+ version: "0"
188
+ requirements: []
189
+
190
+ rubyforge_project:
191
+ rubygems_version: 1.3.7
192
+ signing_key:
193
+ specification_version: 3
194
+ summary: An ActiveModel validation for checking if an attribute's values are a subset of another set.
195
+ test_files:
196
+ - spec/spec_helper.rb
197
+ - spec/subset_validator_spec.rb