rails_legit 0.0.2 → 0.0.3
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.
- checksums.yaml +4 -4
- data/.travis.yml +11 -0
- data/README.md +69 -62
- data/gemfiles/Gemfile.am-3.2 +6 -0
- data/lib/rails_legit/verify_date_validator.rb +1 -1
- data/lib/rails_legit/version.rb +1 -1
- data/rails_legit.gemspec +1 -1
- data/spec/support/date.rb +1 -0
- metadata +8 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1cb1c1c1a1f24385f836da4e7305f27d9075bc35
|
4
|
+
data.tar.gz: 8740159a9cc237ae6b0663d19d02c014136b8a13
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 42eca1cf50db6a14d592b47003d4456057ff4a01a73ef95ee460ffc62a7ebf57f7a96cd4d63f9179a047f7cb9b691c84f38504d95190eb2eb65b13fe6efa3d41
|
7
|
+
data.tar.gz: e86301506150cabf4b2710e31d8245b2d1bbde6f9b0e91ac6d4e6d0fb32d50edcbff988799fb39be3365e8e9cbf5b3a9c6860aadc677991c6c6d39078c61beaa
|
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -106,11 +106,11 @@ You can pass in a method as a symbol, string or a `Date` object. By default, all
|
|
106
106
|
`:current`, `:today`, `:now` are sent to the object under validation.
|
107
107
|
|
108
108
|
```ruby
|
109
|
-
validates :from_date, date: {
|
110
|
-
validates :from_date, date: {
|
111
|
-
validates :from_date, date: {
|
109
|
+
validates :from_date, date: { after: :current } # Date.current is used
|
110
|
+
validates :from_date, date: { after: :today } # Date.current is used
|
111
|
+
validates :from_date, date: { after: :now } # Date.current is used
|
112
112
|
|
113
|
-
validates :from_date, date: {
|
113
|
+
validates :from_date, date: { after: :end_date } # <EventForm Object>.end_date is used
|
114
114
|
```
|
115
115
|
|
116
116
|
Finally,
|
@@ -120,8 +120,8 @@ class EventForm
|
|
120
120
|
attr_accessor :to_date, :from_date
|
121
121
|
include ActiveModel::Model
|
122
122
|
include RailsLegit
|
123
|
-
validates :from_date, date: {
|
124
|
-
validates :to_date, date: {
|
123
|
+
validates :from_date, date: { after: :today }
|
124
|
+
validates :to_date, date: { after: :from_date }
|
125
125
|
end
|
126
126
|
```
|
127
127
|
|
@@ -132,54 +132,57 @@ of another array -- a list of valid IDs, for example. The `VerifyArray`
|
|
132
132
|
validator handles the three cases of `:in`, :not_in` and `:eq`. The
|
133
133
|
abstract functionality is as follows:
|
134
134
|
|
135
|
-
|
136
|
-
|
137
|
-
|
135
|
+
```ruby
|
136
|
+
# The simplest case would be to verify if a record attribute is an
|
137
|
+
# Array
|
138
|
+
validates [1, 2], verify_array: true # => true
|
138
139
|
|
139
|
-
|
140
|
-
|
140
|
+
# Or, you could specify options. Supported options are :in, :not_in,
|
141
|
+
# :eq
|
141
142
|
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
143
|
+
validates [1, 2, 3], verify_array: { in: [1, 2, 3, 4] } # => true
|
144
|
+
# Note: The :in and :not_in validators are not strict. That is, the
|
145
|
+
# following will work as expected. This is similar to :eq in this
|
146
|
+
# case
|
147
|
+
validates [1, 2, 3], verify_array: { in: [1, 2, 3] } # => true
|
148
|
+
validates [1, 2, 3], verify_array: { in: [1, 2] } # => false
|
148
149
|
|
149
|
-
|
150
|
-
|
150
|
+
# The ordering is not a factor
|
151
|
+
validates [3, 2, 1], verify_array: { in: [1, 2, 3, 4] } # => true
|
151
152
|
|
152
|
-
|
153
|
-
|
153
|
+
validates [1, 2], verify_array: { not_in: [4, 5] } # => true
|
154
|
+
validates [1, 2], verify_array: { not_in: [1, 5] } # => true
|
154
155
|
|
155
|
-
|
156
|
-
|
157
|
-
|
156
|
+
# :eq is a strict validator which will result true only if the given
|
157
|
+
# array contains the exact same elements (irrespective of order)
|
158
|
+
# compared to the verification array.
|
158
159
|
|
159
|
-
|
160
|
-
|
160
|
+
validates [1, 2], verify_array: { eq: [2, 1] } # => true
|
161
|
+
validates [1, 2], verify_array: { eq: [3, 1] } # => false
|
162
|
+
```
|
161
163
|
|
162
164
|
Use this in validating your models like so:
|
163
165
|
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
def valid_users_in_db
|
169
|
-
User.all.pluck(:id).map(&:to_s)
|
170
|
-
end
|
171
|
-
end
|
166
|
+
```ruby
|
167
|
+
# Specify a Symbol which is a method defined on the model
|
168
|
+
class EventInvitees < ActiveRecord::Base
|
169
|
+
validates :invited_users, verify_array: { in: :valid_users_in_db }
|
172
170
|
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
171
|
+
def valid_users_in_db
|
172
|
+
User.all.pluck(:id).map(&:to_s)
|
173
|
+
end
|
174
|
+
end
|
177
175
|
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
176
|
+
# or, specify an Array
|
177
|
+
class EventInvitees < ActiveRecord::Base
|
178
|
+
validates :invited_users, verify_array: { in: [1, 25, 155] }
|
179
|
+
end
|
182
180
|
|
181
|
+
# or, specify a Proc that returns an array
|
182
|
+
class EventInvitees < ActiveRecord::Base
|
183
|
+
validates :invited_users, verify_array: { in: ->{ [1, 25, 155] } }
|
184
|
+
end
|
185
|
+
```
|
183
186
|
|
184
187
|
The only caveat here is that the verification item (symbol/proc) should
|
185
188
|
return an Array.
|
@@ -188,38 +191,42 @@ return an Array.
|
|
188
191
|
|
189
192
|
To validate a hash, you can do like so:
|
190
193
|
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
+
```ruby
|
195
|
+
# some_hash = { one: 1, two: 2, three: 3 }
|
196
|
+
validates some_hash, verify_hash: { keys: [:one, :two, :three] } # => true
|
197
|
+
validates some_hash, verify_hash: { values: [1, 2, 3] } # => true
|
198
|
+
```
|
194
199
|
|
195
200
|
The valid options are `:keys` and `:values`. You can pass in an Array or
|
196
201
|
a Proc that evaluates to an array or a method that is defined inside the
|
197
202
|
class.
|
198
203
|
|
199
|
-
|
204
|
+
```ruby
|
205
|
+
# You can use an Array as the option's value
|
200
206
|
|
201
|
-
|
202
|
-
|
203
|
-
|
207
|
+
class User < ActiveRecord::Base
|
208
|
+
validates :settings, verify_hash: { keys: ['public_email', 'public_profile'] }
|
209
|
+
end
|
204
210
|
|
205
|
-
|
211
|
+
# You can use a Proc
|
206
212
|
|
207
|
-
|
208
|
-
|
209
|
-
|
213
|
+
class User < ActiveRecord::Base
|
214
|
+
validates :settings, verify_hash: { keys: -> { ['public_email', 'public_profile'] } }
|
215
|
+
end
|
210
216
|
|
211
|
-
|
217
|
+
# Or, you can use a Symbol if a method with that name is defined
|
212
218
|
|
213
|
-
|
214
|
-
|
219
|
+
class User < ActiveRecord::Base
|
220
|
+
validates :settings, verify_hash: { keys: :accepted_settings }
|
215
221
|
|
216
|
-
|
217
|
-
|
222
|
+
# The method can be private or a visible one
|
223
|
+
private
|
218
224
|
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
225
|
+
def accepted_settings
|
226
|
+
['public_email', 'public_profile']
|
227
|
+
end
|
228
|
+
end
|
229
|
+
```
|
223
230
|
|
224
231
|
The keys or values are checked for existence and not for equality. In
|
225
232
|
other words even if the hash in the first example in Hash section was `{
|
data/lib/rails_legit/version.rb
CHANGED
data/rails_legit.gemspec
CHANGED
@@ -21,5 +21,5 @@ Gem::Specification.new do |spec|
|
|
21
21
|
spec.add_development_dependency "bundler", "~> 1.3"
|
22
22
|
spec.add_development_dependency "rake"
|
23
23
|
spec.add_development_dependency "rspec"
|
24
|
-
spec.add_dependency "activemodel", "
|
24
|
+
spec.add_dependency "activemodel", "> 3.0"
|
25
25
|
end
|
data/spec/support/date.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails_legit
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kashyap
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-06-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -56,16 +56,16 @@ dependencies:
|
|
56
56
|
name: activemodel
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- -
|
59
|
+
- - '>'
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version:
|
61
|
+
version: '3.0'
|
62
62
|
type: :runtime
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- -
|
66
|
+
- - '>'
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version:
|
68
|
+
version: '3.0'
|
69
69
|
description: Provides a DSL for common validation formats like Date, Array, DateTime
|
70
70
|
etc.
|
71
71
|
email:
|
@@ -75,10 +75,12 @@ extensions: []
|
|
75
75
|
extra_rdoc_files: []
|
76
76
|
files:
|
77
77
|
- .gitignore
|
78
|
+
- .travis.yml
|
78
79
|
- Gemfile
|
79
80
|
- LICENSE.txt
|
80
81
|
- README.md
|
81
82
|
- Rakefile
|
83
|
+
- gemfiles/Gemfile.am-3.2
|
82
84
|
- lib/rails_legit.rb
|
83
85
|
- lib/rails_legit/verify_array_validator.rb
|
84
86
|
- lib/rails_legit/verify_date_validator.rb
|