objective 0.1.0 → 0.2.1
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/lib/objective/errors/error_message_creator.rb +0 -3
- data/lib/objective/filter.rb +1 -5
- data/lib/objective/filters/any_filter.rb +6 -0
- data/lib/objective/filters/array_filter.rb +8 -0
- data/lib/objective/filters/boolean_filter.rb +13 -0
- data/lib/objective/filters/date_filter.rb +12 -2
- data/lib/objective/filters/decimal_filter.rb +13 -0
- data/lib/objective/filters/duck_filter.rb +8 -0
- data/lib/objective/filters/file_filter.rb +9 -0
- data/lib/objective/filters/float_filter.rb +13 -0
- data/lib/objective/filters/hash_filter.rb +7 -0
- data/lib/objective/filters/integer_filter.rb +13 -6
- data/lib/objective/filters/model_filter.rb +9 -0
- data/lib/objective/filters/root_filter.rb +2 -0
- data/lib/objective/filters/string_filter.rb +16 -2
- data/lib/objective/filters/time_filter.rb +12 -2
- data/lib/objective.rb +1 -2
- data/spec/filters/date_filter_spec.rb +17 -17
- data/spec/filters/decimal_filter_spec.rb +9 -1
- data/spec/filters/float_filter_spec.rb +17 -0
- data/spec/filters/integer_filter_spec.rb +17 -0
- data/spec/filters/root_filter_spec.rb +3 -3
- data/spec/filters/string_filter_spec.rb +9 -30
- data/spec/filters/time_filter_spec.rb +11 -11
- metadata +3 -4
- data/lib/objective/filters.rb +0 -140
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d1987e4e2d7c0e97589f5ee5d1071c1bd3d37c41
|
4
|
+
data.tar.gz: 75cf1c8c8a8fb1f90ef4408912115d69efaa03b6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 043aea1f02ba613f35f5edeed6194f21a63dd6e9a2a50cebbf2ae4c1adcbce162d6652a9958afcfabe993885c36964ed9c7aba5148104b42ce6e315b63c2d330
|
7
|
+
data.tar.gz: 941e5fe7e25266dfb18817af02d4ae6ec0e799617c97a4c8d76705e0d75a13598428235e74bab36d7ee9a7b3f6552990d343bafd07013284aaece642ee1fcaa7
|
@@ -16,14 +16,11 @@ module Objective
|
|
16
16
|
model: 'must be the right class',
|
17
17
|
date: 'date does non exist',
|
18
18
|
|
19
|
-
before: 'must be before given date',
|
20
|
-
after: 'must be after given date',
|
21
19
|
empty: 'cannot be empty',
|
22
20
|
matches: 'has an incorrect format',
|
23
21
|
in: 'is not an available option',
|
24
22
|
min: 'is too small',
|
25
23
|
max: 'is too big',
|
26
|
-
|
27
24
|
new_records: 'model must be saved'
|
28
25
|
)
|
29
26
|
end
|
data/lib/objective/filter.rb
CHANGED
@@ -29,11 +29,7 @@ module Objective
|
|
29
29
|
end
|
30
30
|
|
31
31
|
def options
|
32
|
-
@options ||= OpenStruct.new(
|
33
|
-
end
|
34
|
-
|
35
|
-
def type_specific_options_hash
|
36
|
-
Objective::Filters::Config[self.class.filter_name].to_h
|
32
|
+
@options ||= OpenStruct.new(self.class.const_get('Options').to_h.merge(@given_options))
|
37
33
|
end
|
38
34
|
|
39
35
|
def sub_filters_hash
|
@@ -2,6 +2,14 @@
|
|
2
2
|
module Objective
|
3
3
|
module Filters
|
4
4
|
class ArrayFilter < Objective::Filter
|
5
|
+
Options = OpenStruct.new(
|
6
|
+
none: Objective::DENY,
|
7
|
+
nils: Objective::DENY,
|
8
|
+
invalid: Objective::DENY,
|
9
|
+
strict: false,
|
10
|
+
wrap: false
|
11
|
+
)
|
12
|
+
|
5
13
|
def feed(raw)
|
6
14
|
result = super(raw)
|
7
15
|
return result if result == Objective::DISCARD || result.errors || result.inputs.nil?
|
@@ -2,6 +2,19 @@
|
|
2
2
|
module Objective
|
3
3
|
module Filters
|
4
4
|
class BooleanFilter < Objective::Filter
|
5
|
+
Options = OpenStruct.new(
|
6
|
+
none: Objective::DENY,
|
7
|
+
nils: Objective::DENY,
|
8
|
+
invalid: Objective::DENY,
|
9
|
+
strict: false,
|
10
|
+
coercion_map: {
|
11
|
+
'true' => true,
|
12
|
+
'false' => false,
|
13
|
+
'1' => true,
|
14
|
+
'0' => false
|
15
|
+
}.freeze
|
16
|
+
)
|
17
|
+
|
5
18
|
private
|
6
19
|
|
7
20
|
def coerce(raw)
|
@@ -2,6 +2,16 @@
|
|
2
2
|
module Objective
|
3
3
|
module Filters
|
4
4
|
class DateFilter < Objective::Filter
|
5
|
+
Options = OpenStruct.new(
|
6
|
+
none: Objective::DENY,
|
7
|
+
nils: Objective::DENY,
|
8
|
+
invalid: Objective::DENY,
|
9
|
+
strict: false,
|
10
|
+
format: nil,
|
11
|
+
min: nil,
|
12
|
+
max: nil
|
13
|
+
)
|
14
|
+
|
5
15
|
private
|
6
16
|
|
7
17
|
def coerce(raw)
|
@@ -21,8 +31,8 @@ module Objective
|
|
21
31
|
end
|
22
32
|
|
23
33
|
def validate(coerced)
|
24
|
-
return :
|
25
|
-
return :
|
34
|
+
return :min if options.min && coerced <= options.min
|
35
|
+
return :max if options.max && coerced >= options.max
|
26
36
|
end
|
27
37
|
end
|
28
38
|
end
|
@@ -2,6 +2,18 @@
|
|
2
2
|
module Objective
|
3
3
|
module Filters
|
4
4
|
class DecimalFilter < Objective::Filter
|
5
|
+
Options = OpenStruct.new(
|
6
|
+
none: Objective::DENY,
|
7
|
+
nils: Objective::DENY,
|
8
|
+
invalid: Objective::DENY,
|
9
|
+
strict: false,
|
10
|
+
delimiter: ', ',
|
11
|
+
decimal_mark: '.',
|
12
|
+
min: nil,
|
13
|
+
max: nil,
|
14
|
+
scale: nil
|
15
|
+
)
|
16
|
+
|
5
17
|
private
|
6
18
|
|
7
19
|
# TODO: the Rational class should be coerced - it requires a precision argument
|
@@ -11,6 +23,7 @@ module Objective
|
|
11
23
|
return datum.to_d if datum.is_a?(Integer) || datum.is_a?(Float)
|
12
24
|
|
13
25
|
return datum unless datum.is_a?(String)
|
26
|
+
return datum if options.decimal_mark != '.' && !options.delimiter.include?('.') && datum.include?('.')
|
14
27
|
|
15
28
|
clean_str = datum.tr(options.delimiter, '').tr(options.decimal_mark, '.')
|
16
29
|
return datum unless clean_str =~ /\A[-+]?\d*\.?\d*\z/
|
@@ -2,6 +2,14 @@
|
|
2
2
|
module Objective
|
3
3
|
module Filters
|
4
4
|
class DuckFilter < Objective::Filter
|
5
|
+
Options = OpenStruct.new(
|
6
|
+
none: Objective::DENY,
|
7
|
+
nils: Objective::DENY,
|
8
|
+
invalid: Objective::DENY,
|
9
|
+
strict: false,
|
10
|
+
methods: nil
|
11
|
+
)
|
12
|
+
|
5
13
|
private
|
6
14
|
|
7
15
|
def coerce_error(coerced)
|
@@ -2,6 +2,15 @@
|
|
2
2
|
module Objective
|
3
3
|
module Filters
|
4
4
|
class FileFilter < Objective::Filter
|
5
|
+
Options = OpenStruct.new(
|
6
|
+
none: Objective::DENY,
|
7
|
+
nils: Objective::DENY,
|
8
|
+
invalid: Objective::DENY,
|
9
|
+
strict: false,
|
10
|
+
upload: false,
|
11
|
+
size: nil
|
12
|
+
)
|
13
|
+
|
5
14
|
private
|
6
15
|
|
7
16
|
def coerce_error(coerced)
|
@@ -2,6 +2,18 @@
|
|
2
2
|
module Objective
|
3
3
|
module Filters
|
4
4
|
class FloatFilter < Objective::Filter
|
5
|
+
Options = OpenStruct.new(
|
6
|
+
none: Objective::DENY,
|
7
|
+
nils: Objective::DENY,
|
8
|
+
invalid: Objective::DENY,
|
9
|
+
strict: false,
|
10
|
+
delimiter: ', ',
|
11
|
+
decimal_mark: '.',
|
12
|
+
min: nil,
|
13
|
+
max: nil,
|
14
|
+
scale: nil
|
15
|
+
)
|
16
|
+
|
5
17
|
private
|
6
18
|
|
7
19
|
def coerce(datum)
|
@@ -10,6 +22,7 @@ module Objective
|
|
10
22
|
return datum.to_f if datum.is_a?(Integer) || datum.is_a?(BigDecimal)
|
11
23
|
|
12
24
|
return datum unless datum.is_a?(String)
|
25
|
+
return datum if options.decimal_mark != '.' && !options.delimiter.include?('.') && datum.include?('.')
|
13
26
|
|
14
27
|
clean_str = datum.tr(options.delimiter, '').tr(options.decimal_mark, '.')
|
15
28
|
return datum unless clean_str =~ /\A[-+]?\d*\.?\d*\z/
|
@@ -2,6 +2,13 @@
|
|
2
2
|
module Objective
|
3
3
|
module Filters
|
4
4
|
class HashFilter < Objective::Filter
|
5
|
+
Options = OpenStruct.new(
|
6
|
+
none: Objective::DENY,
|
7
|
+
nils: Objective::DENY,
|
8
|
+
invalid: Objective::DENY,
|
9
|
+
strict: false
|
10
|
+
)
|
11
|
+
|
5
12
|
def feed(raw)
|
6
13
|
result = super(raw)
|
7
14
|
return result if result == Objective::DISCARD || result.errors || result.inputs.nil?
|
@@ -2,6 +2,18 @@
|
|
2
2
|
module Objective
|
3
3
|
module Filters
|
4
4
|
class IntegerFilter < Objective::Filter
|
5
|
+
Options = OpenStruct.new(
|
6
|
+
none: Objective::DENY,
|
7
|
+
nils: Objective::DENY,
|
8
|
+
invalid: Objective::DENY,
|
9
|
+
strict: false,
|
10
|
+
delimiter: ', ',
|
11
|
+
decimal_mark: '.',
|
12
|
+
min: nil,
|
13
|
+
max: nil,
|
14
|
+
in: nil
|
15
|
+
)
|
16
|
+
|
5
17
|
private
|
6
18
|
|
7
19
|
def coerce(datum)
|
@@ -9,6 +21,7 @@ module Objective
|
|
9
21
|
|
10
22
|
datum_str = raw_to_string(datum)
|
11
23
|
return datum unless datum_str
|
24
|
+
return datum if datum_str.include?('.') && options.decimal_mark != '.' && !options.delimiter.include?('.')
|
12
25
|
|
13
26
|
clean_str = datum_str.tr(options.delimiter, '').tr(options.decimal_mark, '.')
|
14
27
|
return datum unless clean_str =~ /\A[-+]?\d*\.?0*\z/
|
@@ -33,7 +46,6 @@ module Objective
|
|
33
46
|
return :in unless included?(coerced)
|
34
47
|
return :min unless above_min?(coerced)
|
35
48
|
return :max unless below_max?(coerced)
|
36
|
-
return :scale unless within_scale?(coerced)
|
37
49
|
end
|
38
50
|
|
39
51
|
def included?(datum)
|
@@ -50,11 +62,6 @@ module Objective
|
|
50
62
|
return true if options.max.nil?
|
51
63
|
datum <= options.max
|
52
64
|
end
|
53
|
-
|
54
|
-
def within_scale?(datum)
|
55
|
-
return true if options.scale.nil?
|
56
|
-
(datum - datum.round(options.scale)).zero?
|
57
|
-
end
|
58
65
|
end
|
59
66
|
end
|
60
67
|
end
|
@@ -2,6 +2,15 @@
|
|
2
2
|
module Objective
|
3
3
|
module Filters
|
4
4
|
class ModelFilter < Objective::Filter
|
5
|
+
Options = OpenStruct.new(
|
6
|
+
none: Objective::DENY,
|
7
|
+
nils: Objective::DENY,
|
8
|
+
invalid: Objective::DENY,
|
9
|
+
strict: false,
|
10
|
+
class: nil,
|
11
|
+
new_records: false
|
12
|
+
)
|
13
|
+
|
5
14
|
private
|
6
15
|
|
7
16
|
def coerce_error(coerced)
|
@@ -2,11 +2,25 @@
|
|
2
2
|
module Objective
|
3
3
|
module Filters
|
4
4
|
class StringFilter < Objective::Filter
|
5
|
+
Options = OpenStruct.new(
|
6
|
+
none: Objective::DENY,
|
7
|
+
nils: Objective::DENY,
|
8
|
+
invalid: Objective::DENY,
|
9
|
+
strict: false,
|
10
|
+
empty: Objective::DENY,
|
11
|
+
squish: true,
|
12
|
+
min: nil,
|
13
|
+
max: nil,
|
14
|
+
in: nil,
|
15
|
+
matches: nil,
|
16
|
+
decimal_format: 'F',
|
17
|
+
coercable_classes: [Symbol, TrueClass, FalseClass, Integer, Float, BigDecimal].freeze
|
18
|
+
)
|
19
|
+
|
5
20
|
def coerce(raw)
|
6
21
|
return raw unless raw.is_a?(String) || coercable?(raw)
|
7
22
|
tmp = raw.is_a?(BigDecimal) ? raw.to_s(options.decimal_format) : raw.to_s
|
8
|
-
tmp = tmp.
|
9
|
-
tmp = tmp.strip if options.strip
|
23
|
+
tmp = tmp.squish if options.squish
|
10
24
|
tmp
|
11
25
|
end
|
12
26
|
|
@@ -2,6 +2,16 @@
|
|
2
2
|
module Objective
|
3
3
|
module Filters
|
4
4
|
class TimeFilter < Objective::Filter
|
5
|
+
Options = OpenStruct.new(
|
6
|
+
none: Objective::DENY,
|
7
|
+
nils: Objective::DENY,
|
8
|
+
invalid: Objective::DENY,
|
9
|
+
strict: false,
|
10
|
+
format: nil,
|
11
|
+
min: nil,
|
12
|
+
max: nil
|
13
|
+
)
|
14
|
+
|
5
15
|
private
|
6
16
|
|
7
17
|
def coerce(raw)
|
@@ -22,8 +32,8 @@ module Objective
|
|
22
32
|
end
|
23
33
|
|
24
34
|
def validate(coerced)
|
25
|
-
return :
|
26
|
-
return :
|
35
|
+
return :min if options.min && coerced <= options.min
|
36
|
+
return :max if options.max && coerced >= options.max
|
27
37
|
end
|
28
38
|
end
|
29
39
|
end
|
data/lib/objective.rb
CHANGED
@@ -12,13 +12,13 @@ require 'active_support/core_ext/object/blank'
|
|
12
12
|
require 'active_support/core_ext/hash/indifferent_access'
|
13
13
|
require 'active_support/core_ext/hash/deep_merge'
|
14
14
|
require 'active_support/core_ext/array/wrap'
|
15
|
+
require 'active_support/core_ext/string/filters'
|
15
16
|
require 'active_support/core_ext/string/inflections'
|
16
17
|
require 'active_support/core_ext/integer/inflections'
|
17
18
|
|
18
19
|
require 'objective/allow'
|
19
20
|
require 'objective/deny'
|
20
21
|
require 'objective/discard'
|
21
|
-
require 'objective/invalid'
|
22
22
|
require 'objective/none'
|
23
23
|
|
24
24
|
require 'objective/errors/error_atom'
|
@@ -28,7 +28,6 @@ require 'objective/errors/error_message_creator'
|
|
28
28
|
require 'objective/errors/validation_error'
|
29
29
|
|
30
30
|
require 'objective/filter'
|
31
|
-
require 'objective/filters'
|
32
31
|
require 'objective/filters/any_filter'
|
33
32
|
require 'objective/filters/array_filter'
|
34
33
|
require 'objective/filters/boolean_filter'
|
@@ -30,51 +30,51 @@ describe 'Objective::Filters::DateFilter' do
|
|
30
30
|
end
|
31
31
|
end
|
32
32
|
|
33
|
-
it 'checks if the given date is after
|
33
|
+
it 'checks if the given date is after the min date' do
|
34
34
|
date = Date.new(2005, 1, 1)
|
35
|
-
|
36
|
-
f = Objective::Filters::DateFilter.new(:d1,
|
35
|
+
min_date = Date.new(2000, 1, 1)
|
36
|
+
f = Objective::Filters::DateFilter.new(:d1, min: min_date)
|
37
37
|
result = f.feed(date)
|
38
38
|
|
39
39
|
assert_equal date, result.inputs
|
40
40
|
assert_nil result.errors
|
41
41
|
end
|
42
42
|
|
43
|
-
it 'gives errors when the given date is before the
|
43
|
+
it 'gives errors when the given date is before the min date' do
|
44
44
|
date = Date.new(1995, 1, 1)
|
45
|
-
|
46
|
-
f = Objective::Filters::DateFilter.new(:d1,
|
45
|
+
min_date = Date.new(2000, 1, 1)
|
46
|
+
f = Objective::Filters::DateFilter.new(:d1, min: min_date)
|
47
47
|
result = f.feed(date)
|
48
48
|
|
49
49
|
assert_equal date, result.inputs
|
50
|
-
assert_equal :
|
50
|
+
assert_equal :min, result.errors
|
51
51
|
end
|
52
52
|
|
53
|
-
it 'checks if the given date is before a
|
53
|
+
it 'checks if the given date is before a max date' do
|
54
54
|
date = Date.new(1995, 1, 1)
|
55
|
-
|
56
|
-
f = Objective::Filters::DateFilter.new(:d1,
|
55
|
+
min_date = Date.new(2000, 1, 1)
|
56
|
+
f = Objective::Filters::DateFilter.new(:d1, max: min_date)
|
57
57
|
result = f.feed(date)
|
58
58
|
|
59
59
|
assert_equal date, result.inputs
|
60
60
|
assert_nil result.errors
|
61
61
|
end
|
62
62
|
|
63
|
-
it 'gives errors when the given date is after the
|
63
|
+
it 'gives errors when the given date is after the max date' do
|
64
64
|
date = Date.new(2005, 1, 1)
|
65
|
-
|
66
|
-
f = Objective::Filters::DateFilter.new(:d1,
|
65
|
+
max_date = Date.new(2000, 1, 1)
|
66
|
+
f = Objective::Filters::DateFilter.new(:d1, max: max_date)
|
67
67
|
result = f.feed(date)
|
68
68
|
|
69
69
|
assert_equal date, result.inputs
|
70
|
-
assert_equal :
|
70
|
+
assert_equal :max, result.errors
|
71
71
|
end
|
72
72
|
|
73
73
|
it 'checks if the given date is in the given range' do
|
74
74
|
date = Date.new(2005, 1, 1)
|
75
|
-
|
76
|
-
|
77
|
-
f = Objective::Filters::DateFilter.new(:d1,
|
75
|
+
min_date = Date.new(2000, 1, 1)
|
76
|
+
max_date = Date.new(2010, 1, 1)
|
77
|
+
f = Objective::Filters::DateFilter.new(:d1, min: min_date, max: max_date)
|
78
78
|
result = f.feed(date)
|
79
79
|
|
80
80
|
assert_equal date, result.inputs
|
@@ -125,7 +125,7 @@ describe 'Objective::Filters::DecimalFilter' do
|
|
125
125
|
assert_equal :decimal, result.errors
|
126
126
|
end
|
127
127
|
|
128
|
-
it '
|
128
|
+
it 'allows alternative number formats' do
|
129
129
|
f = Objective::Filters::DecimalFilter.new(:x, delimiter: '.', decimal_mark: ',')
|
130
130
|
result = f.feed('123.456,789')
|
131
131
|
|
@@ -134,6 +134,14 @@ describe 'Objective::Filters::DecimalFilter' do
|
|
134
134
|
assert_nil result.errors
|
135
135
|
end
|
136
136
|
|
137
|
+
it 'considers periods invalid when provided an alternative number format without a period' do
|
138
|
+
f = Objective::Filters::DecimalFilter.new(:x, decimal_mark: '|')
|
139
|
+
result = f.feed('3.14')
|
140
|
+
|
141
|
+
assert_equal '3.14', result.inputs
|
142
|
+
assert_equal :decimal, result.errors
|
143
|
+
end
|
144
|
+
|
137
145
|
it 'considers numbers with less decimal points than the scale value to be valid' do
|
138
146
|
f = Objective::Filters::DecimalFilter.new(:x, scale: 2)
|
139
147
|
result = f.feed('1.2')
|
@@ -82,6 +82,23 @@ describe 'Objective::Filters::FloatFilter' do
|
|
82
82
|
end
|
83
83
|
end
|
84
84
|
|
85
|
+
it 'allows alternative number formats' do
|
86
|
+
f = Objective::Filters::FloatFilter.new(:x, delimiter: '.', decimal_mark: ',')
|
87
|
+
result = f.feed('123.456,789')
|
88
|
+
|
89
|
+
assert result.inputs.is_a?(Float)
|
90
|
+
assert_equal 123_456.789, result.inputs
|
91
|
+
assert_nil result.errors
|
92
|
+
end
|
93
|
+
|
94
|
+
it 'considers periods invalid when provided an alternative number format without a period' do
|
95
|
+
f = Objective::Filters::FloatFilter.new(:x, decimal_mark: '|')
|
96
|
+
result = f.feed('3.14')
|
97
|
+
|
98
|
+
assert_equal '3.14', result.inputs
|
99
|
+
assert_equal :float, result.errors
|
100
|
+
end
|
101
|
+
|
85
102
|
it 'considers nil to be invalid' do
|
86
103
|
f = Objective::Filters::FloatFilter.new(:x)
|
87
104
|
result = f.feed(nil)
|
@@ -71,6 +71,23 @@ describe 'Objective::Filters::IntegerFilter' do
|
|
71
71
|
end
|
72
72
|
end
|
73
73
|
|
74
|
+
it 'allows alternative number formats' do
|
75
|
+
f = Objective::Filters::IntegerFilter.new(:x, delimiter: '.', decimal_mark: ',')
|
76
|
+
result = f.feed('123.456,000')
|
77
|
+
|
78
|
+
assert result.inputs.is_a?(Integer)
|
79
|
+
assert_equal 123_456, result.inputs
|
80
|
+
assert_nil result.errors
|
81
|
+
end
|
82
|
+
|
83
|
+
it 'considers periods invalid when provided an alternative number format without a period' do
|
84
|
+
f = Objective::Filters::IntegerFilter.new(:x, decimal_mark: '|')
|
85
|
+
result = f.feed('3.00')
|
86
|
+
|
87
|
+
assert_equal '3.00', result.inputs
|
88
|
+
assert_equal :integer, result.errors
|
89
|
+
end
|
90
|
+
|
74
91
|
it 'considers nil to be invalid' do
|
75
92
|
f = Objective::Filters::IntegerFilter.new(:i)
|
76
93
|
result = f.feed(nil)
|
@@ -57,7 +57,7 @@ describe 'Objective::Filters::RootFilter' do
|
|
57
57
|
assert_nil result.errors
|
58
58
|
end
|
59
59
|
|
60
|
-
it 'bar is optional -- discards empty if it needs to be
|
60
|
+
it 'bar is optional -- discards empty if it needs to be squished' do
|
61
61
|
hf = Objective::Filters::RootFilter.new do
|
62
62
|
filter do
|
63
63
|
string :foo
|
@@ -70,11 +70,11 @@ describe 'Objective::Filters::RootFilter' do
|
|
70
70
|
assert_nil result.errors
|
71
71
|
end
|
72
72
|
|
73
|
-
it "bar is optional -- don't discard empty if it's spaces but
|
73
|
+
it "bar is optional -- don't discard empty if it's spaces but squishing is off" do
|
74
74
|
hf = Objective::Filters::RootFilter.new do
|
75
75
|
filter do
|
76
76
|
string :foo
|
77
|
-
string :bar, empty: Objective::DISCARD,
|
77
|
+
string :bar, empty: Objective::DISCARD, squish: false
|
78
78
|
end
|
79
79
|
end
|
80
80
|
|
@@ -38,17 +38,17 @@ describe 'Objective::Filters::StringFilter' do
|
|
38
38
|
end
|
39
39
|
end
|
40
40
|
|
41
|
-
it '
|
42
|
-
sf = Objective::Filters::StringFilter.new(:s
|
43
|
-
result = sf.feed(
|
44
|
-
assert_equal '
|
41
|
+
it 'squishes' do
|
42
|
+
sf = Objective::Filters::StringFilter.new(:s)
|
43
|
+
result = sf.feed(" Hello,\tWorld!\r\nNew Line ")
|
44
|
+
assert_equal 'Hello, World! New Line', result.inputs
|
45
45
|
assert_nil result.errors
|
46
46
|
end
|
47
47
|
|
48
|
-
it '
|
49
|
-
sf = Objective::Filters::StringFilter.new(:s,
|
50
|
-
result = sf.feed(
|
51
|
-
assert_equal
|
48
|
+
it 'does not squish' do
|
49
|
+
sf = Objective::Filters::StringFilter.new(:s, squish: false)
|
50
|
+
result = sf.feed(" hello \t")
|
51
|
+
assert_equal " hello \t", result.inputs
|
52
52
|
assert_nil result.errors
|
53
53
|
end
|
54
54
|
|
@@ -80,7 +80,7 @@ describe 'Objective::Filters::StringFilter' do
|
|
80
80
|
assert_nil result.errors
|
81
81
|
end
|
82
82
|
|
83
|
-
it 'considers
|
83
|
+
it 'considers squished strings that are empty to be invalid' do
|
84
84
|
sf = Objective::Filters::StringFilter.new(:s)
|
85
85
|
result = sf.feed(' ')
|
86
86
|
assert_equal '', result.inputs
|
@@ -227,25 +227,4 @@ describe 'Objective::Filters::StringFilter' do
|
|
227
227
|
assert_equal true, result.inputs
|
228
228
|
assert_equal :string, result.errors
|
229
229
|
end
|
230
|
-
|
231
|
-
it 'removes unprintable characters' do
|
232
|
-
sf = Objective::Filters::StringFilter.new(:s, allow_control_characters: false)
|
233
|
-
result = sf.feed("Hello\u0000\u0000World!")
|
234
|
-
assert_equal 'Hello World!', result.inputs
|
235
|
-
assert_nil result.errors
|
236
|
-
end
|
237
|
-
|
238
|
-
it "doesn't remove unprintable characters" do
|
239
|
-
sf = Objective::Filters::StringFilter.new(:s, allow_control_characters: true)
|
240
|
-
result = sf.feed("Hello\u0000\u0000World!")
|
241
|
-
assert_equal "Hello\u0000\u0000World!", result.inputs
|
242
|
-
assert_nil result.errors
|
243
|
-
end
|
244
|
-
|
245
|
-
it "doesn't remove tabs, spaces and line breaks" do
|
246
|
-
sf = Objective::Filters::StringFilter.new(:s, allow_control_characters: false)
|
247
|
-
result = sf.feed("Hello,\tWorld !\r\nNew Line")
|
248
|
-
assert_equal "Hello,\tWorld !\r\nNew Line", result.inputs
|
249
|
-
assert_nil result.errors
|
250
|
-
end
|
251
230
|
end
|
@@ -26,41 +26,41 @@ describe 'Objective::Filters::TimeFilter' do
|
|
26
26
|
assert_nil result.errors
|
27
27
|
end
|
28
28
|
|
29
|
-
it 'checks if the given time is after
|
29
|
+
it 'checks if the given time is after the min time' do
|
30
30
|
time = Time.now
|
31
|
-
f = Objective::Filters::TimeFilter.new(:t,
|
31
|
+
f = Objective::Filters::TimeFilter.new(:t, min: time - 1)
|
32
32
|
result = f.feed(time)
|
33
33
|
assert_equal time, result.inputs
|
34
34
|
assert_nil result.errors
|
35
35
|
end
|
36
36
|
|
37
|
-
it 'gives errors when the given time is before the
|
37
|
+
it 'gives errors when the given time is before the min time' do
|
38
38
|
time = Time.now
|
39
|
-
f = Objective::Filters::TimeFilter.new(:t,
|
39
|
+
f = Objective::Filters::TimeFilter.new(:t, min: time + 1)
|
40
40
|
result = f.feed(time)
|
41
41
|
assert_equal time, result.inputs
|
42
|
-
assert_equal :
|
42
|
+
assert_equal :min, result.errors
|
43
43
|
end
|
44
44
|
|
45
|
-
it 'checks if the given time is before
|
45
|
+
it 'checks if the given time is before the max time' do
|
46
46
|
time = Time.now
|
47
|
-
f = Objective::Filters::TimeFilter.new(:t,
|
47
|
+
f = Objective::Filters::TimeFilter.new(:t, max: time + 1)
|
48
48
|
result = f.feed(time)
|
49
49
|
assert_equal time, result.inputs
|
50
50
|
assert_nil result.errors
|
51
51
|
end
|
52
52
|
|
53
|
-
it 'gives errors when the given time is after the
|
53
|
+
it 'gives errors when the given time is after the max time' do
|
54
54
|
time = Time.now
|
55
|
-
f = Objective::Filters::TimeFilter.new(:t,
|
55
|
+
f = Objective::Filters::TimeFilter.new(:t, max: time - 1)
|
56
56
|
result = f.feed(time)
|
57
57
|
assert_equal time, result.inputs
|
58
|
-
assert_equal :
|
58
|
+
assert_equal :max, result.errors
|
59
59
|
end
|
60
60
|
|
61
61
|
it 'checks if the given time is in the given range' do
|
62
62
|
time = Time.now
|
63
|
-
f = Objective::Filters::TimeFilter.new(:t,
|
63
|
+
f = Objective::Filters::TimeFilter.new(:t, min: time - 1, max: time + 1)
|
64
64
|
result = f.feed(time)
|
65
65
|
assert_equal time, result.inputs
|
66
66
|
assert_nil result.errors
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: objective
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dallin Crane
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-05-
|
11
|
+
date: 2017-05-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -82,7 +82,6 @@ files:
|
|
82
82
|
- lib/objective/errors/error_message_creator.rb
|
83
83
|
- lib/objective/errors/validation_error.rb
|
84
84
|
- lib/objective/filter.rb
|
85
|
-
- lib/objective/filters.rb
|
86
85
|
- lib/objective/filters/any_filter.rb
|
87
86
|
- lib/objective/filters/array_filter.rb
|
88
87
|
- lib/objective/filters/boolean_filter.rb
|
@@ -140,7 +139,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
140
139
|
version: '0'
|
141
140
|
requirements: []
|
142
141
|
rubyforge_project:
|
143
|
-
rubygems_version: 2.6.
|
142
|
+
rubygems_version: 2.6.11
|
144
143
|
signing_key:
|
145
144
|
specification_version: 4
|
146
145
|
summary: Business Logic Units
|
data/lib/objective/filters.rb
DELETED
@@ -1,140 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
module Objective
|
3
|
-
module Filters
|
4
|
-
Config = OpenStruct.new
|
5
|
-
|
6
|
-
Config.any = OpenStruct.new(
|
7
|
-
none: Objective::DENY,
|
8
|
-
nils: Objective::ALLOW,
|
9
|
-
invalid: Objective::DENY
|
10
|
-
)
|
11
|
-
|
12
|
-
Config.array = OpenStruct.new(
|
13
|
-
none: Objective::DENY,
|
14
|
-
nils: Objective::DENY,
|
15
|
-
invalid: Objective::DENY,
|
16
|
-
wrap: false
|
17
|
-
)
|
18
|
-
|
19
|
-
Config.boolean = OpenStruct.new(
|
20
|
-
none: Objective::DENY,
|
21
|
-
nils: Objective::DENY,
|
22
|
-
invalid: Objective::DENY,
|
23
|
-
coercion_map: {
|
24
|
-
'true' => true,
|
25
|
-
'false' => false,
|
26
|
-
'1' => true,
|
27
|
-
'0' => false
|
28
|
-
}.freeze
|
29
|
-
)
|
30
|
-
|
31
|
-
Config.date = OpenStruct.new(
|
32
|
-
none: Objective::DENY,
|
33
|
-
nils: Objective::DENY,
|
34
|
-
invalid: Objective::DENY,
|
35
|
-
format: nil, # If nil, Date.parse will be used for coercion. If something like "%Y-%m-%d", Date.strptime is used
|
36
|
-
after: nil, # A date object, representing the minimum date allowed, inclusive
|
37
|
-
before: nil # A date object, representing the maximum date allowed, inclusive
|
38
|
-
)
|
39
|
-
|
40
|
-
Config.decimal = OpenStruct.new(
|
41
|
-
none: Objective::DENY,
|
42
|
-
nils: Objective::DENY,
|
43
|
-
invalid: Objective::DENY,
|
44
|
-
delimiter: ', ',
|
45
|
-
decimal_mark: '.',
|
46
|
-
min: nil,
|
47
|
-
max: nil,
|
48
|
-
scale: nil
|
49
|
-
)
|
50
|
-
|
51
|
-
Config.duck = OpenStruct.new(
|
52
|
-
none: Objective::DENY,
|
53
|
-
nils: Objective::DENY,
|
54
|
-
invalid: Objective::DENY,
|
55
|
-
methods: nil
|
56
|
-
)
|
57
|
-
|
58
|
-
Config.file = OpenStruct.new(
|
59
|
-
none: Objective::DENY,
|
60
|
-
nils: Objective::DENY,
|
61
|
-
invalid: Objective::DENY,
|
62
|
-
upload: false,
|
63
|
-
size: nil
|
64
|
-
)
|
65
|
-
|
66
|
-
Config.float = OpenStruct.new(
|
67
|
-
none: Objective::DENY,
|
68
|
-
nils: Objective::DENY,
|
69
|
-
invalid: Objective::DENY,
|
70
|
-
delimiter: ', ',
|
71
|
-
decimal_mark: '.',
|
72
|
-
min: nil,
|
73
|
-
max: nil,
|
74
|
-
scale: nil
|
75
|
-
)
|
76
|
-
|
77
|
-
Config.hash = OpenStruct.new(
|
78
|
-
none: Objective::DENY,
|
79
|
-
nils: Objective::DENY,
|
80
|
-
invalid: Objective::DENY
|
81
|
-
)
|
82
|
-
|
83
|
-
Config.integer = OpenStruct.new(
|
84
|
-
none: Objective::DENY,
|
85
|
-
nils: Objective::DENY,
|
86
|
-
invalid: Objective::DENY,
|
87
|
-
delimiter: ', ',
|
88
|
-
decimal_mark: '.',
|
89
|
-
min: nil,
|
90
|
-
max: nil,
|
91
|
-
scale: nil,
|
92
|
-
in: nil
|
93
|
-
)
|
94
|
-
|
95
|
-
Config.model = OpenStruct.new(
|
96
|
-
none: Objective::DENY,
|
97
|
-
nils: Objective::DENY,
|
98
|
-
invalid: Objective::DENY,
|
99
|
-
class: nil,
|
100
|
-
new_records: false
|
101
|
-
)
|
102
|
-
|
103
|
-
Config.root = OpenStruct.new
|
104
|
-
|
105
|
-
Config.string = OpenStruct.new(
|
106
|
-
none: Objective::DENY,
|
107
|
-
nils: Objective::DENY,
|
108
|
-
invalid: Objective::DENY,
|
109
|
-
allow_control_characters: false,
|
110
|
-
strip: true,
|
111
|
-
empty: Objective::DENY,
|
112
|
-
min: nil,
|
113
|
-
max: nil,
|
114
|
-
in: nil,
|
115
|
-
matches: nil,
|
116
|
-
decimal_format: 'F',
|
117
|
-
coercable_classes: [
|
118
|
-
Symbol,
|
119
|
-
TrueClass,
|
120
|
-
FalseClass,
|
121
|
-
Integer,
|
122
|
-
Float,
|
123
|
-
BigDecimal
|
124
|
-
].freeze
|
125
|
-
)
|
126
|
-
|
127
|
-
Config.time = OpenStruct.new(
|
128
|
-
none: Objective::DENY,
|
129
|
-
nils: Objective::DENY,
|
130
|
-
invalid: Objective::DENY,
|
131
|
-
format: nil,
|
132
|
-
after: nil,
|
133
|
-
before: nil
|
134
|
-
)
|
135
|
-
|
136
|
-
def self.config
|
137
|
-
yield Config
|
138
|
-
end
|
139
|
-
end
|
140
|
-
end
|