mutations 0.8.2 → 0.8.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/CHANGELOG.md +6 -0
- data/lib/mutations/array_filter.rb +23 -5
- data/lib/mutations/date_filter.rb +12 -7
- data/lib/mutations/version.rb +1 -1
- data/spec/array_filter_spec.rb +74 -0
- data/spec/date_filter_spec.rb +14 -1
- data/spec/model_filter_spec.rb +21 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3eda7e54196daf6a06659a669f0864921ab376c54f218f4463f7c8b06d1dc075
|
4
|
+
data.tar.gz: 0c6d22af4d18c4ae3041c600f2c61873cae3728514f49d48dfed41bfd6b57e63
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: eb82171717884e5ed61fa499c50fa9f5d3b4b1108c988619610b461e76f1a25c70078ad4aa3fff275e32e4b43f711cbe334d402b86e61a53d58778b4792c5986
|
7
|
+
data.tar.gz: 9538cc3fbe474561fe72ff096c072128a03bb13237541e1e21338d72557abd2beeb75e85d1ff2bb72b5c9d9015bb89046b44d4330e6cd3ddde8eebb6f1e3ebb7
|
data/.gitignore
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
+
0.8.3
|
2
|
+
-----------
|
3
|
+
- Add `min_length` and `max_length` options to array filter (#128, @jwoertink)
|
4
|
+
- Add `empty_is_nil` option to date filter (#122, @jamesacarr)
|
5
|
+
- Respect `Mutations.cache_constants` in array filters (#125, @eugeneius)
|
6
|
+
|
1
7
|
0.8.2
|
2
8
|
-----------
|
3
9
|
- Add `:error_key` filter option to allow error messages to be customised. (#115, @mwhatters)
|
@@ -10,7 +10,9 @@ module Mutations
|
|
10
10
|
@default_options = {
|
11
11
|
:nils => false, # true allows an explicit nil to be valid. Overrides any other options
|
12
12
|
:class => nil, # A constant or string indicates that each element of the array needs to be one of these classes
|
13
|
-
:arrayize => false
|
13
|
+
:arrayize => false, # true will convert "hi" to ["hi"]. "" converts to []
|
14
|
+
:min_length => nil, # Can be a number like 5, meaning 5 objects are required
|
15
|
+
:max_length => nil # Can be a number like 20, meaning no more than 20 objects
|
14
16
|
}
|
15
17
|
|
16
18
|
def initialize(name, opts = {}, &block)
|
@@ -38,7 +40,23 @@ module Mutations
|
|
38
40
|
@element_filter = ArrayFilter.new(nil, options, &block)
|
39
41
|
end
|
40
42
|
|
43
|
+
def initialize_constants!
|
44
|
+
@initialize_constants ||= begin
|
45
|
+
if options[:class]
|
46
|
+
options[:class] = options[:class].constantize if options[:class].is_a?(String)
|
47
|
+
end
|
48
|
+
|
49
|
+
true
|
50
|
+
end
|
51
|
+
|
52
|
+
unless Mutations.cache_constants?
|
53
|
+
options[:class] = options[:class].to_s.constantize if options[:class]
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
41
57
|
def filter(data)
|
58
|
+
initialize_constants!
|
59
|
+
|
42
60
|
# Handle nil case
|
43
61
|
if data.nil?
|
44
62
|
return [nil, nil] if options[:nils]
|
@@ -54,6 +72,9 @@ module Mutations
|
|
54
72
|
errors = ErrorArray.new
|
55
73
|
filtered_data = []
|
56
74
|
found_error = false
|
75
|
+
|
76
|
+
return [data, :min_length] if options[:min_length] && data.length < options[:min_length]
|
77
|
+
return [data, :max_length] if options[:max_length] && data.length > options[:max_length]
|
57
78
|
data.each_with_index do |el, i|
|
58
79
|
el_filtered, el_error = filter_element(el)
|
59
80
|
el_error = ErrorAtom.new(@name, el_error, :index => i) if el_error.is_a?(Symbol)
|
@@ -81,10 +102,7 @@ module Mutations
|
|
81
102
|
data, el_errors = @element_filter.filter(data)
|
82
103
|
return [data, el_errors] if el_errors
|
83
104
|
elsif options[:class]
|
84
|
-
|
85
|
-
class_const = class_const.constantize if class_const.is_a?(String)
|
86
|
-
|
87
|
-
if !data.is_a?(class_const)
|
105
|
+
if !data.is_a?(options[:class])
|
88
106
|
return [data, :class]
|
89
107
|
end
|
90
108
|
end
|
@@ -1,21 +1,26 @@
|
|
1
1
|
module Mutations
|
2
2
|
class DateFilter < AdditionalFilter
|
3
3
|
@default_options = {
|
4
|
-
:nils => false,
|
5
|
-
:
|
6
|
-
:
|
7
|
-
:
|
4
|
+
:nils => false, # true allows an explicit nil to be valid. Overrides any other options
|
5
|
+
:empty_is_nil => false, # If true, treat empty string as if it were nil
|
6
|
+
:format => nil, # If nil, Date.parse will be used for coercion. If something like "%Y-%m-%d", Date.strptime is used
|
7
|
+
:after => nil, # A date object, representing the minimum date allowed, inclusive
|
8
|
+
:before => nil # A date object, representing the maximum date allowed, inclusive
|
8
9
|
}
|
9
10
|
|
10
11
|
def filter(data)
|
12
|
+
if options[:empty_is_nil] && data == ""
|
13
|
+
data = nil
|
14
|
+
end
|
15
|
+
|
11
16
|
# Handle nil case
|
12
17
|
if data.nil?
|
13
18
|
return [nil, nil] if options[:nils]
|
14
19
|
return [nil, :nils]
|
15
20
|
end
|
16
|
-
|
21
|
+
|
17
22
|
# Now check if it's empty:
|
18
|
-
return [data, :empty] if "" == data
|
23
|
+
return [data, :empty] if "" == data
|
19
24
|
|
20
25
|
if data.is_a?(Date) # Date and DateTime
|
21
26
|
actual_date = data
|
@@ -34,7 +39,7 @@ module Mutations
|
|
34
39
|
else
|
35
40
|
return [nil, :date]
|
36
41
|
end
|
37
|
-
|
42
|
+
|
38
43
|
# Ok, its a valid date, check if it falls within the range
|
39
44
|
if options[:after]
|
40
45
|
if actual_date <= options[:after]
|
data/lib/mutations/version.rb
CHANGED
data/spec/array_filter_spec.rb
CHANGED
@@ -3,6 +3,8 @@ require 'stringio'
|
|
3
3
|
|
4
4
|
describe "Mutations::ArrayFilter" do
|
5
5
|
|
6
|
+
class SimpleModel; end
|
7
|
+
|
6
8
|
it "allows arrays" do
|
7
9
|
f = Mutations::ArrayFilter.new(:arr)
|
8
10
|
filtered, errors = f.filter([1])
|
@@ -189,4 +191,76 @@ describe "Mutations::ArrayFilter" do
|
|
189
191
|
assert_equal [1,2,4,5], filtered
|
190
192
|
assert_equal nil, errors
|
191
193
|
end
|
194
|
+
|
195
|
+
it "considers long arrays to be valid" do
|
196
|
+
f = Mutations::ArrayFilter.new(:arr, :min_length => 2)
|
197
|
+
filtered, errors = f.filter([1, 2])
|
198
|
+
assert_equal [1, 2], filtered
|
199
|
+
assert_equal nil, errors
|
200
|
+
end
|
201
|
+
|
202
|
+
it "considers short arrays to be invalid" do
|
203
|
+
f = Mutations::ArrayFilter.new(:arr, :min_length => 2)
|
204
|
+
filtered, errors = f.filter([1])
|
205
|
+
assert_equal [1], filtered
|
206
|
+
assert_equal :min_length, errors
|
207
|
+
end
|
208
|
+
|
209
|
+
it "sets a max_length for the array of 20 with one element as valid" do
|
210
|
+
f = Mutations::ArrayFilter.new(:arr, :max_length => 20)
|
211
|
+
filtered, errors = f.filter([1])
|
212
|
+
assert_equal [1], filtered
|
213
|
+
assert_equal nil, errors
|
214
|
+
end
|
215
|
+
|
216
|
+
it "sets a max_length for the array of 2 with 5 as invalid" do
|
217
|
+
f = Mutations::ArrayFilter.new(:arr, :max_length => 2)
|
218
|
+
filtered, errors = f.filter([1,2,3,4,5])
|
219
|
+
assert_equal [1,2,3,4,5], filtered
|
220
|
+
assert_equal :max_length, errors
|
221
|
+
end
|
222
|
+
|
223
|
+
it "will not re-constantize if cache_constants is true" do
|
224
|
+
was = Mutations.cache_constants?
|
225
|
+
Mutations.cache_constants = true
|
226
|
+
|
227
|
+
f = Mutations::ArrayFilter.new(:simple_models, class: "SimpleModel")
|
228
|
+
|
229
|
+
m = SimpleModel.new
|
230
|
+
filtered, errors = f.filter([m])
|
231
|
+
assert_equal [m], filtered
|
232
|
+
assert_equal nil, errors
|
233
|
+
|
234
|
+
Object.send(:remove_const, 'SimpleModel')
|
235
|
+
class SimpleModel; end
|
236
|
+
|
237
|
+
m = SimpleModel.new
|
238
|
+
filtered, errors = f.filter([m])
|
239
|
+
assert_equal [m], filtered
|
240
|
+
assert_equal [:class], errors.symbolic
|
241
|
+
|
242
|
+
Mutations.cache_constants = was
|
243
|
+
end
|
244
|
+
|
245
|
+
it "will re-constantize if cache_constants is false" do
|
246
|
+
was = Mutations.cache_constants?
|
247
|
+
Mutations.cache_constants = false
|
248
|
+
|
249
|
+
f = Mutations::ArrayFilter.new(:simple_models, class: "SimpleModel")
|
250
|
+
|
251
|
+
m = SimpleModel.new
|
252
|
+
filtered, errors = f.filter([m])
|
253
|
+
assert_equal [m], filtered
|
254
|
+
assert_equal nil, errors
|
255
|
+
|
256
|
+
Object.send(:remove_const, 'SimpleModel')
|
257
|
+
class SimpleModel; end
|
258
|
+
|
259
|
+
m = SimpleModel.new
|
260
|
+
filtered, errors = f.filter([m])
|
261
|
+
assert_equal [m], filtered
|
262
|
+
assert_equal nil, errors
|
263
|
+
|
264
|
+
Mutations.cache_constants = was
|
265
|
+
end
|
192
266
|
end
|
data/spec/date_filter_spec.rb
CHANGED
@@ -124,7 +124,7 @@ describe "Mutations::DateFilter" do
|
|
124
124
|
assert_equal nil, filtered
|
125
125
|
assert_equal :nils, errors
|
126
126
|
end
|
127
|
-
|
127
|
+
|
128
128
|
it "considers empty strings to be empty" do
|
129
129
|
f = Mutations::DateFilter.new
|
130
130
|
filtered, errors = f.filter("")
|
@@ -140,6 +140,19 @@ describe "Mutations::DateFilter" do
|
|
140
140
|
assert_equal nil, errors
|
141
141
|
end
|
142
142
|
|
143
|
+
it "considers empty strings to be nil if empty_is_nil option is used" do
|
144
|
+
f = Mutations::DateFilter.new(:empty_is_nil => true)
|
145
|
+
_filtered, errors = f.filter("")
|
146
|
+
assert_equal :nils, errors
|
147
|
+
end
|
148
|
+
|
149
|
+
it "returns empty strings as nil if empty_is_nil option is used" do
|
150
|
+
f = Mutations::DateFilter.new(:empty_is_nil => true, :nils => true)
|
151
|
+
filtered, errors = f.filter("")
|
152
|
+
assert_equal nil, filtered
|
153
|
+
assert_equal nil, errors
|
154
|
+
end
|
155
|
+
|
143
156
|
it "doesn't allow non-existing dates" do
|
144
157
|
date_string = "1, 20, 2013"
|
145
158
|
f = Mutations::DateFilter.new
|
data/spec/model_filter_spec.rb
CHANGED
@@ -60,6 +60,27 @@ describe "Mutations::ModelFilter" do
|
|
60
60
|
assert_equal nil, filtered
|
61
61
|
assert_equal nil, errors
|
62
62
|
end
|
63
|
+
|
64
|
+
it "will not re-constantize if cache_constants is true" do
|
65
|
+
was = Mutations.cache_constants?
|
66
|
+
Mutations.cache_constants = true
|
67
|
+
f = Mutations::ModelFilter.new(:simple_model)
|
68
|
+
m = SimpleModel.new
|
69
|
+
filtered, errors = f.filter(m)
|
70
|
+
assert_equal m, filtered
|
71
|
+
assert_equal nil, errors
|
72
|
+
|
73
|
+
Object.send(:remove_const, 'SimpleModel')
|
74
|
+
|
75
|
+
class SimpleModel; end
|
76
|
+
|
77
|
+
m = SimpleModel.new
|
78
|
+
filtered, errors = f.filter(m)
|
79
|
+
assert_equal m, filtered
|
80
|
+
assert_equal :model, errors
|
81
|
+
|
82
|
+
Mutations.cache_constants = was
|
83
|
+
end
|
63
84
|
|
64
85
|
it "will re-constantize if cache_constants is false" do
|
65
86
|
was = Mutations.cache_constants?
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mutations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.8.
|
4
|
+
version: 0.8.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jonathan Novak
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-07-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|