rails_legit 0.0.1 → 0.0.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 855b56aca820aa680b2a95ec3572d28a617d06bf
4
- data.tar.gz: 41482d5be38796fff7d213e38dd5c1d7e04958a7
3
+ metadata.gz: e68fe7cabdf5878a6f9deaf9d0e7f0c2efb3e11e
4
+ data.tar.gz: cd7bb79827297860febee58476ca90540573c883
5
5
  SHA512:
6
- metadata.gz: e33a786477a8919de7133ed63008aa73e06050b143aeb285148c737a064ecb25fd86ce3de0d4afbd1aff8320ef4db3dc9173cd45b731bac618e5f88e02618ec3
7
- data.tar.gz: 1a5dd6eb87d1847579fa906da2b1c22ef45ca74d01789391d68ac5c35364e8897079c6096c3926d608560675f505e6b0a20727fa0cd042074b4094475f8ca13a
6
+ metadata.gz: 2e4cb7d0d52ed1f427006ce9fc3f220777ef83fc772038a8d05aa3fe087453d31fcd3ec220a915c103bdcacb782694285fd708da0002846ff595bc9ebcb71c61
7
+ data.tar.gz: 7e6fc9d98bde417a6911ca836ebd1b97f0098d969d1618f84fe63e0cd4b07080fe10e1ca4b3de3f92983523b848e428b77ccca086e92e8e3e32c139711f9e3f7
data/README.md CHANGED
@@ -44,23 +44,27 @@ can create events where they can specify the start times and the end times
44
44
  of the event. `EventForm` is such a class that is a non-persistent model of
45
45
  the event record.
46
46
 
47
- class Event < ActiveRecord::Base
48
- # has the following attributes:
49
- # name
50
- # description
51
- # to
52
- # from
53
- end
47
+ ```ruby
48
+ class Event < ActiveRecord::Base
49
+ # has the following attributes:
50
+ # name
51
+ # description
52
+ # to
53
+ # from
54
+ end
55
+ ```
54
56
 
55
57
  The `to` and `from` are DateTime objects that get stored in the DB. And the
56
58
  corresponding ActiveModel form: (assuming Rails 4)
57
59
 
58
- class EventForm
59
- include ActiveModel::Model
60
- attr_accessor :name, :description, :to, :from
61
- attr_accessor :to_date, :to_time
62
- attr_accessor :from_date, :from_time
63
- end
60
+ ```ruby
61
+ class EventForm
62
+ include ActiveModel::Model
63
+ attr_accessor :name, :description, :to, :from
64
+ attr_accessor :to_date, :to_time
65
+ attr_accessor :from_date, :from_time
66
+ end
67
+ ```
64
68
 
65
69
  The `:to_date`, `:to_time`, `:from_date`, `:from_time` are the attributes
66
70
  for the data that is returned from the UI where there are separate inputs
@@ -69,17 +73,21 @@ for start date, end date, start time and end time.
69
73
  To use the `DateValidator`, include the `RailsLegit` module in the
70
74
  `EventForm` class:
71
75
 
72
- class EventForm
73
- # unchanged from above
74
- include RailsLegit
75
- end
76
+ ```ruby
77
+ class EventForm
78
+ # unchanged from above
79
+ include RailsLegit
80
+ end
81
+ ```
76
82
 
77
83
  Adding validations is as simple as:
78
84
 
79
- class EventForm
80
- # unchanged from above
81
- validates :from_date, :to_date, verify_date: true
82
- end
85
+ ```ruby
86
+ class EventForm
87
+ # unchanged from above
88
+ validates :from_date, :to_date, date: true
89
+ end
90
+ ```
83
91
 
84
92
  Currently supported validation syntax elements are:
85
93
 
@@ -97,22 +105,127 @@ The valid options are `:before`, `:after`, `:on_or_before`, `:on_or_after`, `:on
97
105
  You can pass in a method as a symbol, string or a `Date` object. By default, all symbols except
98
106
  `:current`, `:today`, `:now` are sent to the object under validation.
99
107
 
100
- validates :from_date, verify_date: { before: :current } # Date.current is used
101
- validates :from_date, verify_date: { before: :today } # Date.current is used
102
- validates :from_date, verify_date: { before: :now } # Date.current is used
108
+ ```ruby
109
+ validates :from_date, date: { greater_than: :current } # Date.current is used
110
+ validates :from_date, date: { greater_than: :today } # Date.current is used
111
+ validates :from_date, date: { greater_than: :now } # Date.current is used
103
112
 
104
- validates :from_date, verify_date: { before: :end_date } # <EventForm Object>.end_date is used
113
+ validates :from_date, date: { greater_than: :end_date } # <EventForm Object>.end_date is used
114
+ ```
105
115
 
106
116
  Finally,
107
117
 
108
- class EventForm
109
- attr_accessor :to_date, :from_date
110
- include ActiveModel::Model
111
- include RailsLegit
112
- validates :from_date, verify_date: { before: :today }
113
- validates :to_date, verify_date: { before: :from_date }
118
+ ```ruby
119
+ class EventForm
120
+ attr_accessor :to_date, :from_date
121
+ include ActiveModel::Model
122
+ include RailsLegit
123
+ validates :from_date, date: { greater_than: :today }
124
+ validates :to_date, date: { greater_than: :from_date }
125
+ end
126
+ ```
127
+
128
+ ### Array Validator
129
+
130
+ It is quite common to check whether an array of attributes is a subset
131
+ of another array -- a list of valid IDs, for example. The `VerifyArray`
132
+ validator handles the three cases of `:in`, :not_in` and `:eq`. The
133
+ abstract functionality is as follows:
134
+
135
+ # The simplest case would be to verify if a record attribute is an
136
+ # Array
137
+ validates [1, 2], verify_array: true # => true
138
+
139
+ # Or, you could specify options. Supported options are :in, :not_in,
140
+ # :eq
141
+
142
+ validates [1, 2, 3], verify_array: { in: [1, 2, 3, 4] } # => true
143
+ # Note: The :in and :not_in validators are not strict. That is, the
144
+ # following will work as expected. This is similar to :eq in this
145
+ # case
146
+ validates [1, 2, 3], verify_array: { in: [1, 2, 3] } # => true
147
+ validates [1, 2, 3], verify_array: { in: [1, 2] } # => false
148
+
149
+ # The ordering is not a factor
150
+ validates [3, 2, 1], verify_array: { in: [1, 2, 3, 4] } # => true
151
+
152
+ validates [1, 2], verify_array: { not_in: [4, 5] } # => true
153
+ validates [1, 2], verify_array: { not_in: [1, 5] } # => true
154
+
155
+ # :eq is a strict validator which will result true only if the given
156
+ # array contains the exact same elements (irrespective of order)
157
+ # compared to the verification array.
158
+
159
+ validates [1, 2], verify_array: { eq: [2, 1] } # => true
160
+ validates [1, 2], verify_array: { eq: [3, 1] } # => false
161
+
162
+ Use this in validating your models like so:
163
+
164
+ # Specify a Symbol which is a method defined on the model
165
+ class EventInvitees < ActiveRecord::Base
166
+ validates :invited_users, verify_array: { in: :valid_users_in_db }
167
+
168
+ def valid_users_in_db
169
+ User.all.pluck(:id).map(&:to_s)
170
+ end
171
+ end
172
+
173
+ # or, specify an Array
174
+ class EventInvitees < ActiveRecord::Base
175
+ validates :invited_users, verify_array: { in: [1, 25, 155] }
176
+ end
177
+
178
+ # or, specify a Proc that returns an array
179
+ class EventInvitees < ActiveRecord::Base
180
+ validates :invited_users, verify_array: { in: ->{ [1, 25, 155] } }
181
+ end
182
+
183
+
184
+ The only caveat here is that the verification item (symbol/proc) should
185
+ return an Array.
186
+
187
+ ### Hash Validator
188
+
189
+ To validate a hash, you can do like so:
190
+
191
+ # some_hash = { one: 1, two: 2, three: 3 }
192
+ validates some_hash, verify_hash: { keys: [:one, :two, :three] } # => true
193
+ validates some_hash, verify_hash: { values: [1, 2, 3] } # => true
194
+
195
+ The valid options are `:keys` and `:values`. You can pass in an Array or
196
+ a Proc that evaluates to an array or a method that is defined inside the
197
+ class.
198
+
199
+ # You can use an Array as the option's value
200
+
201
+ class User < ActiveRecord::Base
202
+ validates :settings, verify_hash: { keys: ['public_email', 'public_profile'] }
114
203
  end
115
204
 
205
+ # You can use a Proc
206
+
207
+ class User < ActiveRecord::Base
208
+ validates :settings, verify_hash: { keys: -> { ['public_email', 'public_profile'] } }
209
+ end
210
+
211
+ # Or, you can use a Symbol if a method with that name is defined
212
+
213
+ class User < ActiveRecord::Base
214
+ validates :settings, verify_hash: { keys: :accepted_settings }
215
+
216
+ # The method can be private or a visible one
217
+ private
218
+
219
+ def accepted_settings
220
+ ['public_email', 'public_profile']
221
+ end
222
+ end
223
+
224
+ The keys or values are checked for existence and not for equality. In
225
+ other words even if the hash in the first example in Hash section was `{
226
+ one: 1, two: 2 }`, it would still validate to true. To add to that, the
227
+ ordering is not significant.
228
+
116
229
  ## Contributing
117
230
 
118
231
  1. Fork it
@@ -0,0 +1,88 @@
1
+ module RailsLegit
2
+ class VerifyArrayValidator < ActiveModel::EachValidator
3
+
4
+ VALID_COMPARISIONS = [
5
+ :in, :not_in, :eq
6
+ ].freeze
7
+
8
+ attr_accessor :comparisions
9
+
10
+ def initialize(options)
11
+ super
12
+ @comparisions = {}
13
+ process_options!
14
+ end
15
+
16
+ def validate_each(record, attribute, value)
17
+ unless value.is_a?(Array)
18
+ record.errors.add(attribute, "Not an Array")
19
+ return
20
+ end
21
+
22
+ comparisions.each do |k, v|
23
+ if v.nil?
24
+ raise ArgumentError, "Cannot compare an array with nil"
25
+ elsif v.is_a?(Symbol)
26
+ if record.respond_to?(v, true)
27
+ array_to_be_compared_with = record.send(v)
28
+ if array_to_be_compared_with && array_to_be_compared_with.is_a?(Array)
29
+ compare_both_arrays(value, array_to_be_compared_with, attribute, k, record)
30
+ else
31
+ raise ArgumentError, "The comparision value was not an Array or didn't evaluate to an Array. It was #{array_to_be_compared_with}"
32
+ end
33
+ else
34
+ raise NoMethodError, "No method named #{v} on the record supplied"
35
+ end
36
+ elsif v.is_a?(Array)
37
+ compare_both_arrays(value, v, attribute, k, record)
38
+ else
39
+ raise ArgumentError, "The comparision can be done against an array or a Proc that evaluates to an array"
40
+ end
41
+ end
42
+ end
43
+
44
+ def check_validity!
45
+ options.keys.each do |key|
46
+ unless VALID_COMPARISIONS.member?(key)
47
+ raise ArgumentError, "Valid keys for options are #{VALID_COMPARISIONS.join(', ')}"
48
+ end
49
+ end
50
+ end
51
+
52
+ private
53
+
54
+ def compare_both_arrays(record_array, verification_array, attribute, type_of_comparision, record)
55
+ record_array_set = SortedSet.new(record_array)
56
+ verification_array_set = SortedSet.new(verification_array)
57
+
58
+ case type_of_comparision
59
+ when :in
60
+ unless record_array_set.subset?(verification_array_set)
61
+ record.errors.add(attribute, "The given array is not a subset of #{verification_array}. Expected it to not be one")
62
+ end
63
+ when :not_in
64
+ if record_array_set.subset?(verification_array_set)
65
+ record.errors.add(attribute, "The given array is a subset of #{verification_array}. Expected it to not be one")
66
+ end
67
+ when :eq
68
+ unless record_array_set == verification_array_set
69
+ record.errors.add(attribute, "The given array is not equal to #{verification_array}. Expected it to be equal")
70
+ end
71
+ end
72
+ end
73
+
74
+ def process_options!
75
+ options.each do |k, v|
76
+ unless v.is_a?(Proc) || v.is_a?(Symbol) || v.is_a?(Array)
77
+ raise ArgumentError, "Valid values for options are a Proc or Symbol or an Array"
78
+ end
79
+
80
+ if v.is_a?(Proc)
81
+ comparisions[k] = v.call
82
+ elsif v.is_a?(Array) || v.is_a?(Symbol)
83
+ comparisions[k] = v
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,85 @@
1
+ module RailsLegit
2
+ class VerifyHashValidator < ActiveModel::EachValidator
3
+
4
+ VALID_COMPARISIONS = [
5
+ :keys,
6
+ :values
7
+ ].freeze
8
+
9
+ attr_accessor :comparisions
10
+
11
+ def initialize(options)
12
+ super
13
+ @comparisions = {}
14
+ process_options!
15
+ end
16
+
17
+ def validate_each(record, attribute, value)
18
+ unless value.is_a?(Hash)
19
+ record.errors.add(attribute, "Not a Hash")
20
+ return
21
+ end
22
+
23
+
24
+ comparisions.each do |k, v|
25
+ if v.nil?
26
+ raise ArgumentError, "Cannot compare with nil"
27
+ elsif v.is_a?(Symbol)
28
+ if record.respond_to?(v, true)
29
+ array_to_be_compared_with = record.send(v)
30
+ if array_to_be_compared_with && array_to_be_compared_with.is_a?(Array)
31
+ compare_both_arrays(value, array_to_be_compared_with, attribute, k, record)
32
+ else
33
+ raise ArgumentError, "The comparision value was not an Array or didn't evaluate to an Array. It was #{array_to_be_compared_with}"
34
+ end
35
+ else
36
+ raise NoMethodError, "No method named #{v} on the record supplied"
37
+ end
38
+ elsif v.is_a?(Array)
39
+ compare_both_arrays(value, v, attribute, k, record)
40
+ else
41
+ raise ArgumentError, "The comparision can be done against an array or a Proc that evaluates to an array"
42
+ end
43
+ end
44
+ end
45
+
46
+ def check_validity!
47
+ options.keys.each do |key|
48
+ unless VALID_COMPARISIONS.member?(key)
49
+ raise ArgumentError, "Valid keys for options are #{VALID_COMPARISIONS.join(', ')}"
50
+ end
51
+ end
52
+ end
53
+
54
+ private
55
+
56
+ def compare_both_arrays(record_hash, verification_array, attribute, type_of_comparision, record)
57
+ case type_of_comparision
58
+ when :keys
59
+ record_array_set = SortedSet.new(record_hash.keys)
60
+ verification_array_set = SortedSet.new(verification_array)
61
+ when :values
62
+ record_array_set = SortedSet.new(record_hash.values)
63
+ verification_array_set = SortedSet.new(verification_array)
64
+ end
65
+
66
+ unless record_array_set.subset?(verification_array_set)
67
+ record.errors.add(attribute, "The given array is not a subset of #{verification_array}. Expected it to not be one")
68
+ end
69
+ end
70
+
71
+ def process_options!
72
+ options.each do |k, v|
73
+ unless v.is_a?(Proc) || v.is_a?(Symbol) || v.is_a?(Array)
74
+ raise ArgumentError, "You need to specify either an Array or a Symbol or a Proc that returns an Array as an option value"
75
+ end
76
+
77
+ if v.is_a?(Proc)
78
+ comparisions[k] = v.call
79
+ elsif v.is_a?(Array) || v.is_a?(Symbol)
80
+ comparisions[k] = v
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
@@ -1,3 +1,3 @@
1
1
  module RailsLegit
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/rails_legit.rb CHANGED
@@ -5,4 +5,6 @@ module RailsLegit
5
5
  extend ActiveSupport::Autoload
6
6
 
7
7
  autoload :VerifyDateValidator
8
+ autoload :VerifyArrayValidator
9
+ autoload :VerifyHashValidator
8
10
  end
data/rails_legit.gemspec CHANGED
@@ -20,6 +20,6 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_development_dependency "bundler", "~> 1.3"
22
22
  spec.add_development_dependency "rake"
23
- spec.add_development_dependency "rails"
24
23
  spec.add_development_dependency "rspec"
24
+ spec.add_dependency "activemodel", "~> 4.0.0"
25
25
  end
data/spec/spec_helper.rb CHANGED
@@ -6,7 +6,7 @@
6
6
  # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
7
 
8
8
  require File.expand_path("../../lib/rails_legit.rb", __FILE__)
9
- Dir["spec/support/**/*.rb"].each { |f| require File.expand_path(f) }
9
+ #Dir["spec/support/**/*.rb"].each { |f| require File.expand_path(f) }
10
10
 
11
11
  RSpec.configure do |config|
12
12
  config.treat_symbols_as_metadata_keys_with_true_values = true
@@ -0,0 +1,89 @@
1
+ require "active_model"
2
+
3
+ # Setup Classes
4
+
5
+ class NoOptionsSingleAttribute
6
+ include ActiveModel::Validations
7
+ include RailsLegit
8
+
9
+ validates :array, verify_array: true
10
+
11
+ attr_accessor :array
12
+ def initialize(array)
13
+ @array = array
14
+ end
15
+ end
16
+
17
+ class BaseRecord
18
+ include ActiveModel::Validations
19
+ include RailsLegit
20
+
21
+ attr_accessor :array, :anotherarray
22
+
23
+ def initialize(array, anotherarray)
24
+ @array = array
25
+ @anotherarray = anotherarray
26
+ end
27
+ end
28
+
29
+ class NoOptionsMultipleAttributes < BaseRecord
30
+ validates :array, :anotherarray, verify_array: true
31
+ end
32
+
33
+ class MultipleAttributesInOptionArray < BaseRecord
34
+ validates :array, :anotherarray, verify_array: { in: [1, 2, 3, 4] }
35
+ end
36
+
37
+ class MultipleAttributesNotInOptionArray < BaseRecord
38
+ validates :array, :anotherarray, verify_array: { not_in: [1, 2, 3, 4] }
39
+ end
40
+
41
+ class MultipleAttributesInOptionProc < BaseRecord
42
+ validates :array, :anotherarray, verify_array: { in: ->{ [1, 2, 3, 4] } }
43
+ end
44
+
45
+ class MultipleAttributesNotInOptionProc < BaseRecord
46
+ validates :array, :anotherarray, verify_array: { not_in: ->{ [1, 2, 3, 4] } }
47
+ end
48
+
49
+ class MultipleAttributesInOptionSymbol < BaseRecord
50
+ validates :array, :anotherarray, verify_array: { in: :some_other_array }
51
+
52
+ private
53
+
54
+ def some_other_array
55
+ [1, 2, 3, 4]
56
+ end
57
+ end
58
+
59
+ class MultipleAttributesNotInOptionSymbol < BaseRecord
60
+ validates :array, :anotherarray, verify_array: { not_in: :some_other_array }
61
+
62
+ private
63
+
64
+ def some_other_array
65
+ [1, 2, 3, 4]
66
+ end
67
+ end
68
+
69
+ class MultipleAttributesBothOptionsSymbol < BaseRecord
70
+ validates :array, :anotherarray, verify_array: { in: :some_array, not_in: :some_other_array }
71
+
72
+ private
73
+
74
+ def some_array
75
+ [1, 2, 3, 4]
76
+ end
77
+
78
+ def some_other_array
79
+ [5, 6, 7, 8]
80
+ end
81
+ end
82
+
83
+ class MultipleAttributesBothOptionsProc < BaseRecord
84
+ validates :array, :anotherarray, verify_array: { in: ->{ [1, 2, 3, 4] }, not_in: ->{ [5, 6, 7, 8] } }
85
+ end
86
+
87
+ class MultipleAttributesBothOptionsArray < BaseRecord
88
+ validates :array, :anotherarray, verify_array: { in: [1, 2, 3, 4], not_in: [5, 6, 7, 8] }
89
+ end
data/spec/support/date.rb CHANGED
@@ -1,3 +1,5 @@
1
+ require "active_model"
2
+
1
3
  shared_examples "basic date validations" do
2
4
 
3
5
  context "Invalid Date" do
@@ -25,3 +27,71 @@ shared_examples "basic date validations" do
25
27
  it { should be_valid }
26
28
  end
27
29
  end
30
+
31
+ # Setup Classes
32
+
33
+ class TestRecordWithNoExtraOptions
34
+ include ActiveModel::Validations
35
+ include RailsLegit
36
+
37
+ validates :date, verify_date: true
38
+
39
+ attr_accessor :date
40
+ def initialize(date)
41
+ @date = date
42
+ end
43
+ end
44
+
45
+ class TestRecordWithNoExtraOptionsMultipleAttributes
46
+ include ActiveModel::Validations
47
+ include RailsLegit
48
+
49
+ validates :date, :anotherdate, verify_date: true
50
+
51
+ attr_accessor :date, :anotherdate
52
+ def initialize(date, anotherdate)
53
+ @date = date
54
+ @anotherdate = anotherdate
55
+ end
56
+ end
57
+
58
+ class TestRecordWithMultipleAttributesBeforeOption
59
+ include ActiveModel::Validations
60
+ include RailsLegit
61
+
62
+ validates :date, :anotherdate, verify_date: { before: :before_date }
63
+
64
+ attr_accessor :date, :anotherdate, :before_date
65
+ def initialize(date, anotherdate, before_date)
66
+ @date = date
67
+ @anotherdate = anotherdate
68
+ @before_date = before_date
69
+ end
70
+ end
71
+
72
+ class TestRecordWithMultipleAttributesBeforeOptionCurrentSymbol
73
+ include ActiveModel::Validations
74
+ include RailsLegit
75
+
76
+ validates :date, :anotherdate, verify_date: { before: :today }
77
+
78
+ attr_accessor :date, :anotherdate, :before_date
79
+ def initialize(date, anotherdate, before_date)
80
+ @date = date
81
+ @anotherdate = anotherdate
82
+ @before_date = before_date
83
+ end
84
+ end
85
+
86
+ class TestRecordWithMultipleAttributesBeforeOptionProc
87
+ include ActiveModel::Validations
88
+ include RailsLegit
89
+
90
+ validates :date, :anotherdate, verify_date: { before: ->{ Date.today + 1 } }
91
+
92
+ attr_accessor :date, :anotherdate
93
+ def initialize(date, anotherdate)
94
+ @date = date
95
+ @anotherdate = anotherdate
96
+ end
97
+ end
@@ -0,0 +1,90 @@
1
+ require "active_model"
2
+
3
+ # Setup Classes
4
+
5
+ class NoExtraOptionsHash
6
+ include ActiveModel::Validations
7
+ include RailsLegit
8
+
9
+ validates :hash, verify_hash: true
10
+
11
+ attr_accessor :hash
12
+ def initialize(hash)
13
+ @hash = hash
14
+ end
15
+ end
16
+
17
+ class BaseRecordHashValidator
18
+ include ActiveModel::Validations
19
+ include RailsLegit
20
+
21
+
22
+ attr_accessor :hash, :anotherhash
23
+ def initialize(hash, anotherhash)
24
+ @hash = hash
25
+ @anotherhash = anotherhash
26
+ end
27
+
28
+ end
29
+
30
+ class NoOptionsMultipleAttributesHash < BaseRecordHashValidator
31
+ validates :hash, :anotherhash, verify_hash: true
32
+ end
33
+
34
+ class MultipleAttributesKeysOptionSymbol < BaseRecordHashValidator
35
+ validates :hash, :anotherhash, verify_hash: { keys: :some_other_array }
36
+
37
+ private
38
+
39
+ def some_other_array
40
+ [:one, :two, :three, :four, :five]
41
+ end
42
+ end
43
+
44
+ class MultipleAttributesValuesOptionSymbol < BaseRecordHashValidator
45
+ validates :hash, :anotherhash, verify_hash: { values: :some_other_array }
46
+
47
+ private
48
+
49
+ def some_other_array
50
+ [1, 2, 3, 4, 5]
51
+ end
52
+ end
53
+
54
+ class MultipleAttributesKeysOptionArray < BaseRecordHashValidator
55
+ validates :hash, :anotherhash, verify_hash: { keys: [:one, :two, :three, :four] }
56
+ end
57
+
58
+ class MultipleAttributesValuesOptionArray < BaseRecordHashValidator
59
+ validates :hash, :anotherhash, verify_hash: { values: [1, 2, 3, 4] }
60
+ end
61
+
62
+ class MultipleAttributesKeysOptionProc < BaseRecordHashValidator
63
+ validates :hash, :anotherhash, verify_hash: { keys: ->{ [:one, :two, :three, :four] } }
64
+ end
65
+
66
+ class MultipleAttributesValuesOptionProc < BaseRecordHashValidator
67
+ validates :hash, :anotherhash, verify_hash: { values: ->{ [1, 2, 3, 4] } }
68
+ end
69
+
70
+ class MultipleAttributesKeysValuesOptionProc < BaseRecordHashValidator
71
+ validates :hash, :anotherhash, verify_hash: { keys: ->{ [:one, :two, :three, :four] }, values: ->{ [1, 2, 3, 4] } }
72
+ end
73
+
74
+ class MultipleAttributesKeysValuesOptionArray < BaseRecordHashValidator
75
+ validates :hash, :anotherhash, verify_hash: { keys: [:one, :two, :three, :four], values: [1, 2, 3, 4] }
76
+ end
77
+
78
+ class MultipleAttributesKeysValuesOptionSymbol < BaseRecordHashValidator
79
+ validates :hash, :anotherhash, verify_hash: { keys: :some_array, values: :some_other_array }
80
+
81
+ def some_array
82
+ [:one, :two, :three, :four]
83
+ end
84
+
85
+ private
86
+
87
+ def some_other_array
88
+ [1, 2, 3, 4]
89
+ end
90
+ end
@@ -0,0 +1,186 @@
1
+ require "spec_helper"
2
+ require "support/array"
3
+
4
+ describe RailsLegit::VerifyArrayValidator do
5
+ subject { record }
6
+
7
+ describe "No Extra Options provided" do
8
+ let(:record) { NoOptionsSingleAttribute.new(array) }
9
+
10
+ context "Valid Array" do
11
+ let(:array) { [1, 2, 3] }
12
+ it { should be_valid }
13
+ end
14
+
15
+ context "Invalid Array" do
16
+ let(:array) { "Invalid Array" }
17
+
18
+ it { should_not be_valid }
19
+ it "should attach error on appropriate method" do
20
+ record.valid?
21
+ expect(record.errors[:array]).to include("Not an Array")
22
+ end
23
+ end
24
+ end
25
+
26
+ describe "No Extra Options provided and multiple attributes" do
27
+ let(:record) { NoOptionsMultipleAttributes.new(array, anotherarray) }
28
+ let(:anotherarray) { [1, 2, 3] }
29
+
30
+ context "Valid Array" do
31
+ let(:array) { [1, 2, 3] }
32
+ it { should be_valid }
33
+ end
34
+
35
+ context "Invalid Array on comparision method" do
36
+ let(:anotherarray) { "Invalid Array" }
37
+ let(:array) { [1, 2, 3] }
38
+
39
+ it { should_not be_valid }
40
+ it "should attach error on appropriate method" do
41
+ record.valid?
42
+ expect(record.errors[:anotherarray]).to include("Not an Array")
43
+ end
44
+ end
45
+ end
46
+
47
+ describe "Multiple attributes, Valid case" do
48
+ let(:array) { [1, 2, 3] }
49
+ let(:anotherarray) { [4, 3] }
50
+
51
+ context "In validator" do
52
+ context "comparision object is a Proc" do
53
+ let(:record) { MultipleAttributesInOptionProc.new(array, anotherarray) }
54
+
55
+ it { should be_valid }
56
+ end
57
+
58
+ context "comparision object is a Symbol" do
59
+ let(:record) { MultipleAttributesInOptionSymbol.new(array, anotherarray) }
60
+
61
+ it { should be_valid }
62
+ end
63
+
64
+ context "comparision object is Array" do
65
+ let(:record) { MultipleAttributesInOptionArray.new(array, anotherarray) }
66
+
67
+ it { should be_valid }
68
+ end
69
+ end
70
+
71
+ context "NotIn validator" do
72
+ context "comparision object is a Proc" do
73
+ let(:record) { MultipleAttributesNotInOptionProc.new(array, anotherarray) }
74
+
75
+ it { should_not be_valid }
76
+ end
77
+
78
+ context "comparision object is a Symbol" do
79
+ let(:record) { MultipleAttributesNotInOptionSymbol.new(array, anotherarray) }
80
+
81
+ it { should_not be_valid }
82
+ end
83
+
84
+ context "comparision object is Array" do
85
+ let(:record) { MultipleAttributesNotInOptionArray.new(array, anotherarray) }
86
+
87
+ it { should_not be_valid }
88
+ end
89
+ end
90
+ end
91
+
92
+ describe "Multiple attributes, Invalid case" do
93
+ let(:array) { [4, 1, 5] }
94
+ let(:anotherarray) { [8, 9] }
95
+
96
+ context "In validator" do
97
+ context "comparision object is a Proc" do
98
+ let(:record) { MultipleAttributesInOptionProc.new(array, anotherarray) }
99
+
100
+ it { should_not be_valid }
101
+ end
102
+
103
+ context "comparision object is a Symbol" do
104
+ let(:record) { MultipleAttributesInOptionSymbol.new(array, anotherarray) }
105
+
106
+ it { should_not be_valid }
107
+ end
108
+
109
+ context "comparision object is Array" do
110
+ let(:record) { MultipleAttributesInOptionArray.new(array, anotherarray) }
111
+
112
+ it { should_not be_valid }
113
+ end
114
+ end
115
+
116
+ context "NotIn validator" do
117
+ context "comparision object is a Proc" do
118
+ let(:record) { MultipleAttributesNotInOptionProc.new(array, anotherarray) }
119
+
120
+ it { should be_valid }
121
+ end
122
+
123
+ context "comparision object is a Symbol" do
124
+ let(:record) { MultipleAttributesNotInOptionSymbol.new(array, anotherarray) }
125
+
126
+ it { should be_valid }
127
+ end
128
+
129
+ context "comparision object is Array" do
130
+ let(:record) { MultipleAttributesNotInOptionArray.new(array, anotherarray) }
131
+
132
+ it { should be_valid }
133
+ end
134
+ end
135
+ end
136
+
137
+ describe "Both Options, Valid case" do
138
+ let(:array) { [1, 2] }
139
+ let(:anotherarray) { [1, 2, 3, 4] }
140
+
141
+ context "In validator" do
142
+ context "comparision against Proc" do
143
+ let(:record) { MultipleAttributesBothOptionsProc.new(array, anotherarray) }
144
+
145
+ it { should be_valid }
146
+ end
147
+
148
+ context "comparision against Symbol" do
149
+ let(:record) { MultipleAttributesBothOptionsSymbol.new(array, anotherarray) }
150
+
151
+ it { should be_valid }
152
+ end
153
+
154
+ context "comparision against Array" do
155
+ let(:record) { MultipleAttributesBothOptionsArray.new(array, anotherarray) }
156
+
157
+ it { should be_valid }
158
+ end
159
+ end
160
+ end
161
+
162
+ describe "Both Options, Invalid case" do
163
+ let(:array) { [5, 6] }
164
+ let(:anotherarray) { [3, 4] }
165
+
166
+ context "In validator" do
167
+ context "comparision against Proc" do
168
+ let(:record) { MultipleAttributesBothOptionsProc.new(array, anotherarray) }
169
+
170
+ it { should_not be_valid }
171
+ end
172
+
173
+ context "comparision against Symbol" do
174
+ let(:record) { MultipleAttributesBothOptionsSymbol.new(array, anotherarray) }
175
+
176
+ it { should_not be_valid }
177
+ end
178
+
179
+ context "comparision against Array" do
180
+ let(:record) { MultipleAttributesBothOptionsArray.new(array, anotherarray) }
181
+
182
+ it { should_not be_valid }
183
+ end
184
+ end
185
+ end
186
+ end
@@ -1,71 +1,5 @@
1
1
  require "spec_helper"
2
- require "active_model"
3
-
4
- class TestRecordWithNoExtraOptions
5
- include ActiveModel::Validations
6
- include RailsLegit
7
-
8
- validates :date, verify_date: true
9
-
10
- attr_accessor :date
11
- def initialize(date)
12
- @date = date
13
- end
14
- end
15
-
16
- class TestRecordWithNoExtraOptionsMultipleAttributes
17
- include ActiveModel::Validations
18
- include RailsLegit
19
-
20
- validates :date, :anotherdate, verify_date: true
21
-
22
- attr_accessor :date, :anotherdate
23
- def initialize(date, anotherdate)
24
- @date = date
25
- @anotherdate = anotherdate
26
- end
27
- end
28
-
29
- class TestRecordWithMultipleAttributesBeforeOption
30
- include ActiveModel::Validations
31
- include RailsLegit
32
-
33
- validates :date, :anotherdate, verify_date: { before: :before_date }
34
-
35
- attr_accessor :date, :anotherdate, :before_date
36
- def initialize(date, anotherdate, before_date)
37
- @date = date
38
- @anotherdate = anotherdate
39
- @before_date = before_date
40
- end
41
- end
42
-
43
- class TestRecordWithMultipleAttributesBeforeOptionCurrentSymbol
44
- include ActiveModel::Validations
45
- include RailsLegit
46
-
47
- validates :date, :anotherdate, verify_date: { before: :today }
48
-
49
- attr_accessor :date, :anotherdate, :before_date
50
- def initialize(date, anotherdate, before_date)
51
- @date = date
52
- @anotherdate = anotherdate
53
- @before_date = before_date
54
- end
55
- end
56
-
57
- class TestRecordWithMultipleAttributesBeforeOptionProc
58
- include ActiveModel::Validations
59
- include RailsLegit
60
-
61
- validates :date, :anotherdate, verify_date: { before: ->{ Date.today + 1 } }
62
-
63
- attr_accessor :date, :anotherdate
64
- def initialize(date, anotherdate)
65
- @date = date
66
- @anotherdate = anotherdate
67
- end
68
- end
2
+ require "support/date"
69
3
 
70
4
  describe RailsLegit::VerifyDateValidator do
71
5
  describe "No Extra Options provided" do
@@ -0,0 +1,186 @@
1
+ require "spec_helper"
2
+ require "support/hash"
3
+
4
+ describe RailsLegit::VerifyHashValidator do
5
+ describe "No Extra Options provided" do
6
+ let(:record) { NoExtraOptionsHash.new(hash) }
7
+ subject { record }
8
+
9
+ context "Valid Hash" do
10
+ let(:hash) { { one: 1, two: 2, three: 3, four: 4 } }
11
+ it { should be_valid }
12
+ end
13
+
14
+ context "Invalid Hash" do
15
+ let(:hash) { "Invalid Hash" }
16
+
17
+ it { should_not be_valid }
18
+ it "should attach error on appropriate method" do
19
+ record.valid?
20
+ expect(record.errors[:hash]).to include("Not a Hash")
21
+ end
22
+ end
23
+ end
24
+
25
+ describe "No Extra Options provided and multiple attributes" do
26
+ let(:record) { NoOptionsMultipleAttributesHash.new(hash, anotherhash) }
27
+ let(:anotherhash) { {one: 1, two: 2} }
28
+ subject { record }
29
+
30
+ context "Valid Hash" do
31
+ let(:hash) { {one: 1, two: 2, three: 3} }
32
+ it { should be_valid }
33
+ end
34
+
35
+ context "Invalid Hash on comparision method" do
36
+ let(:anotherhash) { "Invalid Hash" }
37
+ let(:hash) { { one: 1, two: 2, three: 3 } }
38
+
39
+ it { should_not be_valid }
40
+ it "should attach error on appropriate method" do
41
+ record.valid?
42
+ expect(record.errors[:anotherhash]).to include("Not a Hash")
43
+ end
44
+ end
45
+ end
46
+
47
+ describe "Individual Keys and Values options, Multiple attributes, Valid case" do
48
+ let(:hash) { { one: 1, two: 2, three: 3 } }
49
+ let(:anotherhash) { { four: 4, three: 3 } }
50
+ subject { record }
51
+
52
+ context "Keys as the option" do
53
+ context "comparision object is a Proc" do
54
+ let(:record) { MultipleAttributesKeysOptionProc.new(hash, anotherhash) }
55
+
56
+ it { should be_valid }
57
+ end
58
+
59
+ context "comparision object is a Symbol" do
60
+ let(:record) { MultipleAttributesKeysOptionSymbol.new(hash, anotherhash) }
61
+
62
+ it { should be_valid }
63
+ end
64
+
65
+ context "comparision object is Array" do
66
+ let(:record) { MultipleAttributesKeysOptionArray.new(hash, anotherhash) }
67
+
68
+ it { should be_valid }
69
+ end
70
+ end
71
+
72
+ context "Values as the option" do
73
+ context "comparision object is a Proc" do
74
+ let(:record) { MultipleAttributesValuesOptionProc.new(hash, anotherhash) }
75
+
76
+ it { should be_valid }
77
+ end
78
+
79
+ context "comparision object is a Symbol" do
80
+ let(:record) { MultipleAttributesValuesOptionSymbol.new(hash, anotherhash) }
81
+
82
+ it { should be_valid }
83
+ end
84
+
85
+ context "comparision object is Array" do
86
+ let(:record) { MultipleAttributesValuesOptionArray.new(hash, anotherhash) }
87
+
88
+ it { should be_valid }
89
+ end
90
+ end
91
+ end
92
+
93
+ describe "Individual Keys and Values options, Multiple attributes, Invalid case" do
94
+ let(:hash) { { seven: 7, eight: 8, nine: 9 } }
95
+ let(:anotherhash) { { ten: 10, eleven: 11 } }
96
+ subject { record }
97
+
98
+ context "Keys as the option" do
99
+ context "comparision object is a Proc" do
100
+ let(:record) { MultipleAttributesKeysOptionProc.new(hash, anotherhash) }
101
+
102
+ it { should_not be_valid }
103
+ end
104
+
105
+ context "comparision object is a Symbol" do
106
+ let(:record) { MultipleAttributesKeysOptionSymbol.new(hash, anotherhash) }
107
+
108
+ it { should_not be_valid }
109
+ end
110
+
111
+ context "comparision object is Array" do
112
+ let(:record) { MultipleAttributesKeysOptionArray.new(hash, anotherhash) }
113
+
114
+ it { should_not be_valid }
115
+ end
116
+ end
117
+
118
+ context "Values as the option" do
119
+ context "comparision object is a Proc" do
120
+ let(:record) { MultipleAttributesValuesOptionProc.new(hash, anotherhash) }
121
+
122
+ it { should_not be_valid }
123
+ end
124
+
125
+ context "comparision object is a Symbol" do
126
+ let(:record) { MultipleAttributesValuesOptionSymbol.new(hash, anotherhash) }
127
+
128
+ it { should_not be_valid }
129
+ end
130
+
131
+ context "comparision object is Array" do
132
+ let(:record) { MultipleAttributesValuesOptionArray.new(hash, anotherhash) }
133
+
134
+ it { should_not be_valid }
135
+ end
136
+ end
137
+ end
138
+
139
+ describe "Both Keys and Values options, Multiple attributes, Valid case" do
140
+ let(:hash) { { one: 1, two: 2, three: 3 } }
141
+ let(:anotherhash) { { four: 4, three: 3 } }
142
+ subject { record }
143
+
144
+ context "comparision object is a Proc" do
145
+ let(:record) { MultipleAttributesKeysValuesOptionProc.new(hash, anotherhash) }
146
+
147
+ it { should be_valid }
148
+ end
149
+
150
+ context "comparision object is a Symbol" do
151
+ let(:record) { MultipleAttributesKeysValuesOptionSymbol.new(hash, anotherhash) }
152
+
153
+ it { should be_valid }
154
+ end
155
+
156
+ context "comparision object is Array" do
157
+ let(:record) { MultipleAttributesKeysValuesOptionArray.new(hash, anotherhash) }
158
+
159
+ it { should be_valid }
160
+ end
161
+ end
162
+
163
+ describe "Both Keys and Values options, Multiple attributes, Invalid case" do
164
+ let(:hash) { { five: 5, six: 6, seven: 7 } }
165
+ let(:anotherhash) { { nine: 9, ten: 10 } }
166
+ subject { record }
167
+
168
+ context "comparision object is a Proc" do
169
+ let(:record) { MultipleAttributesKeysValuesOptionProc.new(hash, anotherhash) }
170
+
171
+ it { should_not be_valid }
172
+ end
173
+
174
+ context "comparision object is a Symbol" do
175
+ let(:record) { MultipleAttributesKeysValuesOptionSymbol.new(hash, anotherhash) }
176
+
177
+ it { should_not be_valid }
178
+ end
179
+
180
+ context "comparision object is Array" do
181
+ let(:record) { MultipleAttributesKeysValuesOptionArray.new(hash, anotherhash) }
182
+
183
+ it { should_not be_valid }
184
+ end
185
+ end
186
+ end
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.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kashyap
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-07 00:00:00.000000000 Z
11
+ date: 2014-03-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -39,7 +39,7 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: rails
42
+ name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - '>='
@@ -53,19 +53,19 @@ dependencies:
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
- name: rspec
56
+ name: activemodel
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - '>='
59
+ - - ~>
60
60
  - !ruby/object:Gem::Version
61
- version: '0'
62
- type: :development
61
+ version: 4.0.0
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: '0'
68
+ version: 4.0.0
69
69
  description: Provides a DSL for common validation formats like Date, Array, DateTime
70
70
  etc.
71
71
  email:
@@ -80,12 +80,18 @@ files:
80
80
  - README.md
81
81
  - Rakefile
82
82
  - lib/rails_legit.rb
83
+ - lib/rails_legit/verify_array_validator.rb
83
84
  - lib/rails_legit/verify_date_validator.rb
85
+ - lib/rails_legit/verify_hash_validator.rb
84
86
  - lib/rails_legit/version.rb
85
87
  - rails_legit.gemspec
86
88
  - spec/spec_helper.rb
89
+ - spec/support/array.rb
87
90
  - spec/support/date.rb
91
+ - spec/support/hash.rb
92
+ - spec/verify_array_validator_spec.rb
88
93
  - spec/verify_date_validator_spec.rb
94
+ - spec/verify_hash_validator_spec.rb
89
95
  homepage: ''
90
96
  licenses:
91
97
  - MIT
@@ -112,5 +118,10 @@ specification_version: 4
112
118
  summary: Provides a DSL for common validation formats like Date, Array, DateTime etc.
113
119
  test_files:
114
120
  - spec/spec_helper.rb
121
+ - spec/support/array.rb
115
122
  - spec/support/date.rb
123
+ - spec/support/hash.rb
124
+ - spec/verify_array_validator_spec.rb
116
125
  - spec/verify_date_validator_spec.rb
126
+ - spec/verify_hash_validator_spec.rb
127
+ has_rdoc: