active_subset_validator 0.0.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
data/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ ## v1.0.0
2
+
3
+ * Production release
4
+ * Some tests were refactored
5
+
1
6
  ## v0.0.1
2
7
 
3
8
  * Initial release
9
+
data/Gemfile CHANGED
@@ -2,3 +2,7 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in active_subset_validator.gemspec
4
4
  gemspec
5
+
6
+ group :test do
7
+ gem 'coveralls', require: false
8
+ end
data/README.md CHANGED
@@ -1,6 +1,9 @@
1
1
  # ActiveSubsetValidator
2
2
  [![Code Climate](https://codeclimate.com/github/paulnsorensen/active_subset_validator.png)](https://codeclimate.com/github/paulnsorensen/active_subset_validator)
3
3
  [![Dependency Status](https://gemnasium.com/paulnsorensen/active_subset_validator.png)](https://gemnasium.com/paulnsorensen/active_subset_validator)
4
+ [![Gem Version](https://badge.fury.io/rb/active_subset_validator.png)](http://badge.fury.io/rb/active_subset_validator)
5
+ [![Build Status](https://travis-ci.org/paulnsorensen/active_subset_validator.png)](https://travis-ci.org/paulnsorensen/active_subset_validator)
6
+ [![Coverage Status](https://coveralls.io/repos/paulnsorensen/active_subset_validator/badge.png?branch=master)](https://coveralls.io/r/paulnsorensen/active_subset_validator?branch=master)
4
7
 
5
8
  Provides subset validation for serialized arrays or sets in Active Record.
6
9
  Checks whether given values for a serialized array or set are a subset of
@@ -22,9 +25,48 @@ Or install it yourself as:
22
25
 
23
26
  ## Usage
24
27
 
25
- class Foo < ActiveRecord::Base
26
- validates :bars, subset: { of: %w(some list of strings) }
28
+ Add to your models like so:
29
+
30
+ ```ruby
31
+ class Article < ActiveRecord::Base
32
+ validates :categories, subset: { of: %w(foo bar baz ) }
33
+ end
34
+ ```
35
+ The `:of` parameter may contain an `Array` or `Set` or a `Proc` or `lambda` that returns either type.
36
+
37
+ The validator in this gem is a subclass `ActiveModel::EachValidator` so you should have all the nifty options to pass to the `validates` method as well.
38
+
39
+ ```ruby
40
+ class Comment < ActiveRecord::Base
41
+ attr_accessible :content, :tags,
42
+ validates :tags,
43
+ subset: { of: %w(some list of strings) },
44
+ if: ->(comment) { comment.some_method? },
45
+ allow_nil: false
46
+ end
47
+ ```
48
+
49
+ ## Testing
50
+
51
+ You may write your tests similar to the following to ensure that the subset
52
+ validator is working. Here's an example using [FactoryGirl](https://github.com/thoughtbot/factory_girl) and [RSpec](https://github.com/rspec/rspec):
53
+
54
+ ```ruby
55
+ it "ensures times_taken contains only values within the proper set" do
56
+ med = build(:medication)
57
+ med.times_taken = %w(as_needed breakfast lunch dinner bedtime)
58
+ med.should have(:no).errors_on(:times_taken)
59
+ med.times_taken << "midnight"
60
+ med.errors_on(:times_taken).should include("is not a subset of the list")
27
61
  end
62
+ ```
63
+
64
+ Alternatively, if you use [shoulda-matchers](https://github.com/thoughtbot/shoulda-matchers), you could do this:
65
+
66
+ ```ruby
67
+ it { should allow_value(%w(good-val-0 good-val-1)).for(foo) }
68
+ it { should_not allow_value(%w(bad-val-0 bad-val-1)).for(:foo) }
69
+ ```
28
70
 
29
71
  ## Contributing
30
72
 
@@ -1,3 +1,3 @@
1
1
  module ActiveSubsetValidator
2
- VERSION = "0.0.1"
2
+ VERSION = "1.0.0"
3
3
  end
@@ -68,8 +68,7 @@ describe SubsetValidator do
68
68
  Comment.validates :tags, subset: { of: Proc.new { |r| r.valid_set } }
69
69
  comment = Comment.new
70
70
  comment.tags = Set.new %w(python java)
71
- comment.valid?
72
- comment.errors[:tags].first.should eq "is not a subset of the list"
71
+ expect(comment.errors_on(:tags)).to include "is not a subset of the list"
73
72
  end
74
73
 
75
74
  it "adds an error for the invalid attribute when :of is a Set" do
@@ -77,8 +76,7 @@ describe SubsetValidator do
77
76
  Comment.validates :tags, subset: { of: Set.new([1,2,3,5]) }
78
77
  comment = Comment.new
79
78
  comment.tags = Set.new [2,4]
80
- comment.valid?
81
- comment.errors[:tags].first.should eq "is not a subset of the list"
79
+ expect(comment.errors_on(:tags)).to include "is not a subset of the list"
82
80
  end
83
81
 
84
82
  it "adds an error for the invalid attribute when :of is an Array" do
@@ -86,8 +84,7 @@ describe SubsetValidator do
86
84
  Comment.validates :tags, subset: { of: %w(Atlanta Cleveland Chicago) }
87
85
  comment = Comment.new
88
86
  comment.tags = %w(Chicago Boston)
89
- comment.valid?
90
- comment.errors[:tags].first.should eq "is not a subset of the list"
87
+ expect(comment.errors_on(:tags)).to include "is not a subset of the list"
91
88
  end
92
89
 
93
90
  it "adds a custom error message for an invalid attribute when passed" do
@@ -98,8 +95,7 @@ describe SubsetValidator do
98
95
  }
99
96
  comment = Comment.new
100
97
  comment.tags = %w(Chicago Boston)
101
- comment.valid?
102
- comment.errors[:tags].first.should eq "contains an out-of-range city"
98
+ expect(comment.errors_on(:tags)).to include "contains an out-of-range city"
103
99
  end
104
100
 
105
101
  it "correctly validates a serialized Set when :of is a Set" do
@@ -110,7 +106,7 @@ describe SubsetValidator do
110
106
  Comment.validates :tags, subset: { of: Set.new(%w(java scala python)) }
111
107
  comment = Comment.new
112
108
  comment.tags = Set.new %w(python java)
113
- comment.valid?.should eq true
109
+ expect(comment).to have(:no).errors_on(:tags)
114
110
  end
115
111
 
116
112
  it "correctly validates a serialized Set when :of is a Proc" do
@@ -121,7 +117,7 @@ describe SubsetValidator do
121
117
  Comment.validates :tags, subset: { of: Proc.new { |r| r.valid_set } }
122
118
  comment = Comment.new
123
119
  comment.tags = Set.new %w(python ruby)
124
- comment.valid?.should eq true
120
+ expect(comment).to have(:no).errors_on(:tags)
125
121
  end
126
122
 
127
123
  it "correctly validates a serialized Array when :of is a Array" do
@@ -129,7 +125,7 @@ describe SubsetValidator do
129
125
  Comment.validates :tags, subset: { of: %w(Atlanta Boston Chicago) }
130
126
  comment = Comment.new
131
127
  comment.tags = %w(Chicago Boston)
132
- comment.valid?.should eq true
128
+ expect(comment).to have(:no).errors_on(:tags)
133
129
  end
134
130
 
135
131
  it "correctly validates a serialized Array when :of is a Proc" do
@@ -137,7 +133,7 @@ describe SubsetValidator do
137
133
  Comment.validates :tags, subset: { of: Proc.new { [1,3,9,27,81] } }
138
134
  comment = Comment.new
139
135
  comment.tags = [9,27]
140
- comment.valid?.should eq true
136
+ expect(comment).to have(:no).errors_on(:tags)
141
137
  end
142
138
 
143
139
  it "allows nil if :allow_nil is true (default)" do
@@ -145,7 +141,7 @@ describe SubsetValidator do
145
141
  Comment.validates :tags, subset: { of: [1,3,9,27,81] }
146
142
  comment = Comment.new
147
143
  comment.tags = nil
148
- comment.valid?.should eq true
144
+ expect(comment).to have(:no).errors_on(:tags)
149
145
  end
150
146
 
151
147
  it "doesn't allow nil if :allow_nil is false" do
@@ -153,8 +149,7 @@ describe SubsetValidator do
153
149
  Comment.validates :tags, subset: { of: [1,3,9,27,81], allow_nil: false }
154
150
  comment = Comment.new
155
151
  comment.tags = nil
156
- comment.valid?
157
- comment.errors[:tags].first.should eq "cannot be nil"
152
+ expect(comment.errors_on(:tags)).to include "cannot be nil"
158
153
  end
159
154
  end
160
155
  end
@@ -1,51 +1,51 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe ActiveSubsetValidator do
4
- describe "#is_a_set?" do
4
+ describe ".is_a_set?" do
5
5
  it "returns true if an array is a set" do
6
- ActiveSubsetValidator.is_a_set?((1..20).to_a).should eq(true)
6
+ expect(ActiveSubsetValidator.is_a_set?((1..20).to_a)).to eq(true)
7
7
  end
8
8
 
9
9
  it "returns false if an array has duplicate values" do
10
- ActiveSubsetValidator.is_a_set?(%w(foo foo bar baz)).should eq(false)
10
+ expect(ActiveSubsetValidator.is_a_set?(%w(foo foo bar baz))).to eq(false)
11
11
  end
12
12
 
13
13
  it "returns true if a Set is a set" do
14
- ActiveSubsetValidator.is_a_set?(Set.new [1,2,4]).should eq(true)
14
+ expect(ActiveSubsetValidator.is_a_set?(Set.new [1,2,4])).to eq(true)
15
15
  end
16
16
 
17
17
  it "returns true if a proc that is passed returns a proper array" do
18
- ActiveSubsetValidator.is_a_set?(Proc.new { %w(foo bar baz) }).should eq(true)
18
+ expect(ActiveSubsetValidator.is_a_set?(Proc.new { %w(foo bar baz) })).to eq(true)
19
19
  end
20
20
 
21
21
  it "returns false if a proc that is passed returns an improper array" do
22
- ActiveSubsetValidator.is_a_set?(Proc.new { %w(foo foo bar baz) }).should eq(false)
22
+ expect(ActiveSubsetValidator.is_a_set?(Proc.new { %w(foo foo bar baz) })).to eq(false)
23
23
  end
24
24
 
25
25
  it "returns true if a proc that is passed returns a set" do
26
- ActiveSubsetValidator.is_a_set?(Proc.new { Set.new [1,2,4] }).should eq(true)
26
+ expect(ActiveSubsetValidator.is_a_set?(Proc.new { Set.new [1,2,4] })).to eq(true)
27
27
  end
28
28
 
29
29
  it "returns true for valid procs with arguments" do
30
- ActiveSubsetValidator.is_a_set?(Proc.new { |a| Set.new a }, [1,2,3]).should eq(true)
30
+ expect(ActiveSubsetValidator.is_a_set?(Proc.new { |a| Set.new a }, [1,2,3])).to eq(true)
31
31
  end
32
32
  end
33
33
 
34
- describe "#set_difference" do
34
+ describe ".set_difference" do
35
35
  it "returns [] if the first array is a subset of the second array" do
36
- ActiveSubsetValidator.set_difference(%w(foo bar), %w(foo bar baz)).should eq([])
36
+ expect(ActiveSubsetValidator.set_difference(%w(foo bar), %w(foo bar baz))).to eq([])
37
37
  end
38
38
 
39
39
  it "returns [] if the first and second arrays have the same values" do
40
- ActiveSubsetValidator.set_difference([1.0, 2.4, 3.9], [2.4, 1.0, 3.9]).should eq([])
40
+ expect(ActiveSubsetValidator.set_difference([1.0, 2.4, 3.9], [2.4, 1.0, 3.9])).to eq([])
41
41
  end
42
42
 
43
43
  it "returns the set difference when the first array has more values" do
44
- ActiveSubsetValidator.set_difference(%w(foo bar baz beans), %w(foo bar baz)).should eq(%w(beans))
44
+ expect(ActiveSubsetValidator.set_difference(%w(foo bar baz beans), %w(foo bar baz))).to eq(%w(beans))
45
45
  end
46
46
 
47
47
  it "returns the set difference when the first array is outside the set of the second" do
48
- ActiveSubsetValidator.set_difference(%w(beans), %w(foo bar baz)).should eq(%w(beans))
48
+ expect(ActiveSubsetValidator.set_difference(%w(beans), %w(foo bar baz))).to eq(%w(beans))
49
49
  end
50
50
  end
51
51
  end
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,6 @@
1
+ require 'coveralls'
2
+ Coveralls.wear!
3
+
1
4
  require 'active_subset_validator'
2
5
  require 'support/active_record'
3
6
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_subset_validator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 1.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-08-14 00:00:00.000000000 Z
12
+ date: 2013-08-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -116,6 +116,7 @@ extra_rdoc_files: []
116
116
  files:
117
117
  - .gitignore
118
118
  - .rspec
119
+ - .travis.yml
119
120
  - CHANGELOG.md
120
121
  - Gemfile
121
122
  - LICENSE.txt
@@ -145,7 +146,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
145
146
  version: '0'
146
147
  segments:
147
148
  - 0
148
- hash: -4092742916326351878
149
+ hash: 892968742125388242
149
150
  required_rubygems_version: !ruby/object:Gem::Requirement
150
151
  none: false
151
152
  requirements:
@@ -154,7 +155,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
154
155
  version: '0'
155
156
  segments:
156
157
  - 0
157
- hash: -4092742916326351878
158
+ hash: 892968742125388242
158
159
  requirements: []
159
160
  rubyforge_project:
160
161
  rubygems_version: 1.8.25