objective 0.2.2 → 0.3.0

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: c7705d3450e744e1f7e7a887a4d834c0be973c61
4
- data.tar.gz: 46ecee8f11eb7783768e9e396d968e67172ea6d4
3
+ metadata.gz: 36542ad6a5572ba8c8832cf1b1df0383516bb855
4
+ data.tar.gz: 1ccb77a65a1d6feec34011ba5509862df2b3b430
5
5
  SHA512:
6
- metadata.gz: a1756e5db7deb2324ee0d175379c3346f2b32fcf166330e3972d3f06d1a746ea3ec12596948d440e3ee9a0bca3acb9285c56ac8b07bf1952d5d2ec42b9123e31
7
- data.tar.gz: 0c9a485bfdb080d4117b53dcc26799f728d9a869cca0c675844f612e712285028d1cf480f48cbae1de137867d08f54d67a1ab2cd037098787174eb7fec8eea01
6
+ metadata.gz: 6af1516855837c90b9bd954e8724ad7d9b8e9d4b0cd470df6e99f135208c8058f40c1334fa2743bf4fa7cd97d410049e7952564ded26128c1184979163ac244c
7
+ data.tar.gz: 65f94c72d491fe8d1447b243422c9aaa3bc49a918435b02e84bdc8db0a7f4ea74a57e2ef33b124f193d1986550633a3423e2af554e21328da43ad7f726e3762a
@@ -19,8 +19,6 @@ require 'active_support/core_ext/integer/inflections'
19
19
 
20
20
  require 'objective/allow'
21
21
  require 'objective/deny'
22
- require 'objective/discard'
23
- require 'objective/none'
24
22
 
25
23
  require 'objective/errors/error_atom'
26
24
  require 'objective/errors/error_hash'
@@ -6,7 +6,6 @@ module Objective
6
6
  MESSAGES = Hash.new('is invalid').tap do |h|
7
7
  h.merge!(
8
8
  nils: 'cannot be nil',
9
- required: 'is required',
10
9
 
11
10
  string: 'must be a string',
12
11
  integer: 'must be an integer',
@@ -53,7 +53,6 @@ module Objective
53
53
  end
54
54
 
55
55
  def feed(raw)
56
- return feed_none if raw == Objective::NONE
57
56
  return feed_nil if raw.nil?
58
57
 
59
58
  coerced = options.strict == true ? raw : coerce(raw)
@@ -66,33 +65,16 @@ module Objective
66
65
  feed_result(errors, raw, coerced)
67
66
  end
68
67
 
69
- def feed_none
70
- case options.none
71
- when Objective::ALLOW
72
- return Objective::DISCARD
73
- when Objective::DENY
74
- coerced = Objective::NONE
75
- errors = :required
76
- when Objective::DISCARD
77
- raise 'the none option cannot be discarded — did you mean to use allow instead?'
78
- else
79
- coerced = options.none
80
- end
81
-
82
- feed_result(errors, Objective::NONE, coerced)
83
- end
84
-
85
68
  def feed_nil
86
69
  case options.nils
87
70
  when Objective::ALLOW
88
- coerced = nil
89
71
  errors = nil
90
- when Objective::DENY
91
72
  coerced = nil
73
+ when Objective::DENY
92
74
  errors = :nils
93
- when Objective::DISCARD
94
- return Objective::DISCARD
75
+ coerced = nil
95
76
  else
77
+ errors = nil
96
78
  coerced = options.nils
97
79
  end
98
80
 
@@ -101,11 +83,12 @@ module Objective
101
83
 
102
84
  def feed_invalid(errors, raw, coerced)
103
85
  case options.invalid
86
+ when Objective::ALLOW
87
+ errors = nil
104
88
  when Objective::DENY
105
89
  nil
106
- when Objective::DISCARD
107
- return Objective::DISCARD
108
90
  else
91
+ errors = nil
109
92
  coerced = options.invalid
110
93
  end
111
94
 
@@ -118,8 +101,6 @@ module Objective
118
101
  errors = nil
119
102
  when Objective::DENY
120
103
  errors = :empty
121
- when Objective::DISCARD
122
- return Objective::DISCARD
123
104
  else
124
105
  coerced = options.empty
125
106
  end
@@ -4,7 +4,6 @@ module Objective
4
4
  module Filters
5
5
  class AnyFilter < Objective::Filter
6
6
  Options = OpenStruct.new(
7
- none: Objective::DENY,
8
7
  nils: Objective::ALLOW,
9
8
  invalid: Objective::DENY,
10
9
  strict: false
@@ -4,7 +4,6 @@ module Objective
4
4
  module Filters
5
5
  class ArrayFilter < Objective::Filter
6
6
  Options = OpenStruct.new(
7
- none: Objective::DENY,
8
7
  nils: Objective::DENY,
9
8
  invalid: Objective::DENY,
10
9
  strict: false,
@@ -13,7 +12,7 @@ module Objective
13
12
 
14
13
  def feed(raw)
15
14
  result = super(raw)
16
- return result if result == Objective::DISCARD || result.errors || result.inputs.nil?
15
+ return result if result.errors || result.inputs.nil?
17
16
 
18
17
  inputs = []
19
18
  errors = Objective::Errors::ErrorArray.new
@@ -25,11 +24,6 @@ module Objective
25
24
  data = result.coerced
26
25
  data.each_with_index do |sub_data, index|
27
26
  sub_result = sub_filter.feed(sub_data)
28
- if sub_result == Objective::DISCARD
29
- index_shift += 1
30
- next
31
- end
32
-
33
27
  sub_data = sub_result.inputs
34
28
  sub_error = sub_result.errors
35
29
 
@@ -4,7 +4,6 @@ module Objective
4
4
  module Filters
5
5
  class BooleanFilter < Objective::Filter
6
6
  Options = OpenStruct.new(
7
- none: Objective::DENY,
8
7
  nils: Objective::DENY,
9
8
  invalid: Objective::DENY,
10
9
  strict: false,
@@ -4,7 +4,6 @@ module Objective
4
4
  module Filters
5
5
  class DateFilter < Objective::Filter
6
6
  Options = OpenStruct.new(
7
- none: Objective::DENY,
8
7
  nils: Objective::DENY,
9
8
  invalid: Objective::DENY,
10
9
  strict: false,
@@ -4,7 +4,6 @@ module Objective
4
4
  module Filters
5
5
  class DecimalFilter < Objective::Filter
6
6
  Options = OpenStruct.new(
7
- none: Objective::DENY,
8
7
  nils: Objective::DENY,
9
8
  invalid: Objective::DENY,
10
9
  strict: false,
@@ -4,7 +4,6 @@ module Objective
4
4
  module Filters
5
5
  class DuckFilter < Objective::Filter
6
6
  Options = OpenStruct.new(
7
- none: Objective::DENY,
8
7
  nils: Objective::DENY,
9
8
  invalid: Objective::DENY,
10
9
  strict: false,
@@ -4,7 +4,6 @@ module Objective
4
4
  module Filters
5
5
  class FileFilter < Objective::Filter
6
6
  Options = OpenStruct.new(
7
- none: Objective::DENY,
8
7
  nils: Objective::DENY,
9
8
  invalid: Objective::DENY,
10
9
  strict: false,
@@ -4,7 +4,6 @@ module Objective
4
4
  module Filters
5
5
  class FloatFilter < Objective::Filter
6
6
  Options = OpenStruct.new(
7
- none: Objective::DENY,
8
7
  nils: Objective::DENY,
9
8
  invalid: Objective::DENY,
10
9
  strict: false,
@@ -4,7 +4,6 @@ module Objective
4
4
  module Filters
5
5
  class HashFilter < Objective::Filter
6
6
  Options = OpenStruct.new(
7
- none: Objective::DENY,
8
7
  nils: Objective::DENY,
9
8
  invalid: Objective::DENY,
10
9
  strict: false
@@ -12,16 +11,15 @@ module Objective
12
11
 
13
12
  def feed(raw)
14
13
  result = super(raw)
15
- return result if result == Objective::DISCARD || result.errors || result.inputs.nil?
14
+ return result if result.errors || result.inputs.nil?
16
15
 
17
16
  errors = Objective::Errors::ErrorHash.new
18
17
  inputs = HashWithIndifferentAccess.new
19
18
 
20
19
  data = result.coerced
21
20
  sub_filters_hash.each_pair do |key, key_filter|
22
- datum = data.key?(key) ? data[key] : Objective::NONE
21
+ datum = data[key]
23
22
  key_filter_result = key_filter.feed(datum)
24
- next if key_filter_result == Objective::DISCARD
25
23
 
26
24
  sub_data = key_filter_result.inputs
27
25
  sub_error = key_filter_result.errors
@@ -4,7 +4,6 @@ module Objective
4
4
  module Filters
5
5
  class IntegerFilter < Objective::Filter
6
6
  Options = OpenStruct.new(
7
- none: Objective::DENY,
8
7
  nils: Objective::DENY,
9
8
  invalid: Objective::DENY,
10
9
  strict: false,
@@ -4,7 +4,6 @@ module Objective
4
4
  module Filters
5
5
  class ModelFilter < Objective::Filter
6
6
  Options = OpenStruct.new(
7
- none: Objective::DENY,
8
7
  nils: Objective::DENY,
9
8
  invalid: Objective::DENY,
10
9
  strict: false,
@@ -18,14 +18,13 @@ module Objective
18
18
  result.raw = raw
19
19
  result.coerced = coerce(raw)
20
20
 
21
- inputs = OpenStruct.new
21
+ inputs = HashWithIndifferentAccess.new
22
22
  errors = Objective::Errors::ErrorHash.new
23
23
 
24
24
  data = result.coerced
25
25
  sub_filters_hash.each_pair do |key, key_filter|
26
- datum = data.to_h.key?(key) ? data[key] : Objective::NONE
26
+ datum = data.to_h[key]
27
27
  key_filter_result = key_filter.feed(datum)
28
- next if key_filter_result == Objective::DISCARD
29
28
 
30
29
  sub_data = key_filter_result.inputs
31
30
  sub_error = key_filter_result.errors
@@ -4,7 +4,6 @@ module Objective
4
4
  module Filters
5
5
  class StringFilter < Objective::Filter
6
6
  Options = OpenStruct.new(
7
- none: Objective::DENY,
8
7
  nils: Objective::DENY,
9
8
  invalid: Objective::DENY,
10
9
  strict: false,
@@ -4,7 +4,6 @@ module Objective
4
4
  module Filters
5
5
  class TimeFilter < Objective::Filter
6
6
  Options = OpenStruct.new(
7
- none: Objective::DENY,
8
7
  nils: Objective::DENY,
9
8
  invalid: Objective::DENY,
10
9
  strict: false,
@@ -8,7 +8,6 @@ module Objective
8
8
  attr_reader :inputs, :raw_inputs, :built
9
9
  const_set('ALLOW', Objective::ALLOW)
10
10
  const_set('DENY', Objective::DENY)
11
- const_set('DISCARD', Objective::DISCARD)
12
11
  end
13
12
 
14
13
  class_methods do
@@ -6,29 +6,43 @@ require 'simple_unit'
6
6
  describe 'Objective - defaults' do
7
7
  class DefaultUnit
8
8
  include Objective::Unit
9
+
9
10
  filter do
10
- string :name, none: 'Bob Jones'
11
+ string :name, nils: '-nils-', invalid: '-invalid-', empty: '-empty-'
11
12
  end
12
13
 
13
14
  def execute
14
- inputs.to_h
15
+ inputs
15
16
  end
16
17
  end
17
18
 
18
- it 'should have a default if no value is passed' do
19
- outcome = DefaultUnit.run
20
- assert_equal({ name: 'Bob Jones' }, outcome.result)
19
+ it 'should use a valid value passed to it' do
20
+ outcome = DefaultUnit.run(name: 'Fred')
21
21
  assert_equal true, outcome.success
22
+ assert_equal({ 'name' => 'Fred' }, outcome.result)
22
23
  end
23
24
 
24
- it 'should have the passed value if a value is passed' do
25
- outcome = DefaultUnit.run(name: 'Fred')
25
+ it 'should use a nils option default value if no value is passed' do
26
+ outcome = DefaultUnit.run
27
+ assert_equal({ 'name' => '-nils-' }, outcome.result)
26
28
  assert_equal true, outcome.success
27
- assert_equal({ name: 'Fred' }, outcome.result)
28
29
  end
29
30
 
30
- it 'should be an error if nil is passed on a required field with a default' do
31
+ it 'should use a nils option default value if nil is passed' do
31
32
  outcome = DefaultUnit.run(name: nil)
32
- assert_equal false, outcome.success
33
+ assert_equal({ 'name' => '-nils-' }, outcome.result)
34
+ assert_equal true, outcome.success
35
+ end
36
+
37
+ it 'should use the invalid option default if an invalid value is passed' do
38
+ outcome = DefaultUnit.run(name: /regex/)
39
+ assert_equal({ 'name' => '-invalid-' }, outcome.result)
40
+ assert_equal true, outcome.success
41
+ end
42
+
43
+ it 'should use the empty option default if an empty value is passed' do
44
+ outcome = DefaultUnit.run(name: ' ')
45
+ assert_equal({ 'name' => '-empty-' }, outcome.result)
46
+ assert_equal true, outcome.success
33
47
  end
34
48
  end
@@ -16,22 +16,22 @@ describe 'Objective - inheritance' do
16
16
  it 'should filter with inherited unit' do
17
17
  outcome = SimpleInherited.run(name: 'bob', email: 'jon@jones.com', age: 10, amount: 22)
18
18
  assert outcome.success
19
- assert_equal OpenStruct.new(name: 'bob', email: 'jon@jones.com', age: 10, amount: 22), outcome.inputs
19
+ assert_equal({ 'name' => 'bob', 'email' => 'jon@jones.com', 'age' => 10, 'amount' => 22 }, outcome.inputs)
20
20
  end
21
21
 
22
22
  it 'should filter with original unit' do
23
23
  outcome = SimpleUnit.run(name: 'bob', email: 'jon@jones.com', age: 10, amount: 22)
24
24
  assert outcome.success
25
- assert_equal OpenStruct.new(name: 'bob', email: 'jon@jones.com', amount: 22), outcome.inputs
25
+ assert_equal({ 'name' => 'bob', 'email' => 'jon@jones.com', 'amount' => 22 }, outcome.inputs)
26
26
  end
27
27
 
28
28
  it 'shouldnt collide' do
29
29
  outcome = SimpleInherited.run(name: 'bob', email: 'jon@jones.com', age: 10, amount: 22)
30
30
  assert outcome.success
31
- assert_equal OpenStruct.new(name: 'bob', email: 'jon@jones.com', age: 10, amount: 22), outcome.inputs
31
+ assert_equal({ 'name' => 'bob', 'email' => 'jon@jones.com', 'age' => 10, 'amount' => 22 }, outcome.inputs)
32
32
 
33
33
  outcome = SimpleUnit.run(name: 'bob', email: 'jon@jones.com', age: 10, amount: 22)
34
34
  assert outcome.success
35
- assert_equal OpenStruct.new(name: 'bob', email: 'jon@jones.com', amount: 22), outcome.inputs
35
+ assert_equal({ 'name' => 'bob', 'email' => 'jon@jones.com', 'amount' => 22 }, outcome.inputs)
36
36
  end
37
37
  end
@@ -8,14 +8,14 @@ describe 'Objective - errors' do
8
8
  filter do
9
9
  string :str1
10
10
  string :str2, in: %w[opt1 opt2 opt3]
11
- integer :int1, none: ALLOW
11
+ integer :int1, nils: ALLOW
12
12
 
13
- hash :hash1, none: ALLOW do
13
+ hash :hash1, nils: ALLOW do
14
14
  boolean :bool1
15
15
  boolean :bool2
16
16
  end
17
17
 
18
- array :arr1, none: ALLOW do
18
+ array :arr1, nils: ALLOW do
19
19
  integer
20
20
  end
21
21
  end
@@ -74,7 +74,7 @@ describe 'Objective - errors' do
74
74
  'str1' => :empty,
75
75
  'str2' => :in,
76
76
  'int1' => :integer,
77
- 'hash1' => { 'bool1' => :boolean, 'bool2' => :required },
77
+ 'hash1' => { 'bool1' => :boolean, 'bool2' => :nils },
78
78
  'arr1' => [:integer, nil, :integer]
79
79
  }
80
80
 
@@ -88,7 +88,7 @@ describe 'Objective - errors' do
88
88
  'int1' => 'Int1 must be an integer',
89
89
  'hash1' => {
90
90
  'bool1' => 'Bool1 must be a boolean',
91
- 'bool2' => 'Bool2 is required'
91
+ 'bool2' => 'Bool2 cannot be nil'
92
92
  },
93
93
  'arr1' => ['1st Arr1 must be an integer', nil, '3rd Arr1 must be an integer']
94
94
  }
@@ -102,7 +102,7 @@ describe 'Objective - errors' do
102
102
  'Str2 is not an available option',
103
103
  'Int1 must be an integer',
104
104
  'Bool1 must be a boolean',
105
- 'Bool2 is required',
105
+ 'Bool2 cannot be nil',
106
106
  '1st Arr1 must be an integer',
107
107
  '3rd Arr1 must be an integer'
108
108
  ]
@@ -114,23 +114,4 @@ describe 'Objective - errors' do
114
114
  expected.each { |e| assert @outcome.errors.message_list.include?(e) }
115
115
  end
116
116
  end
117
-
118
- class WithShrinkingArray
119
- include Objective::Unit
120
- filter do
121
- array :arr_one do
122
- integer nils: DISCARD
123
- end
124
- end
125
- end
126
-
127
- it 'returns an ErrorArray for errors in arrays' do
128
- o = WithShrinkingArray.run(arr_one: [nil, 1, 'sally'])
129
-
130
- assert !o.success
131
- assert o.errors.is_a?(Objective::Errors::ErrorHash)
132
- assert o.errors[:arr_one].is_a?(Objective::Errors::ErrorArray)
133
- assert_nil o.errors[:arr_one][0]
134
- assert o.errors[:arr_one][1].is_a?(Objective::Errors::ErrorAtom)
135
- end
136
117
  end
@@ -128,17 +128,27 @@ describe 'Objective::Filters::ArrayFilter' do
128
128
  hash do
129
129
  string :foo
130
130
  integer :bar
131
- boolean :baz, none: Objective::ALLOW
131
+ boolean :baz, nils: Objective::ALLOW
132
132
  end
133
133
  end
134
134
 
135
- result = f.feed([{ foo: 'f', bar: 3, baz: true }, { foo: 'f', bar: 3 }, { foo: 'f' }])
136
- expected_result = [{ 'foo' => 'f', 'bar' => 3, 'baz' => true }, { 'foo' => 'f', 'bar' => 3 }, { 'foo' => 'f' }]
135
+ result = f.feed(
136
+ [
137
+ { foo: 'f', bar: 3, baz: true },
138
+ { foo: 'f', bar: 3 },
139
+ { foo: 'f' }
140
+ ]
141
+ )
142
+ expected_result = [
143
+ { 'foo' => 'f', 'bar' => 3, 'baz' => true },
144
+ { 'foo' => 'f', 'bar' => 3, 'baz' => nil },
145
+ { 'foo' => 'f', 'baz' => nil }
146
+ ]
137
147
 
138
148
  assert_equal expected_result, result.inputs
139
149
  assert_nil result.errors[0]
140
150
  assert_nil result.errors[1]
141
- assert_equal ({ 'bar' => :required }), result.errors[2].codes
151
+ assert_equal ({ 'bar' => :nils }), result.errors[2].codes
142
152
  end
143
153
 
144
154
  it 'lets you pass arrays of arrays' do
@@ -169,33 +179,33 @@ describe 'Objective::Filters::ArrayFilter' do
169
179
  assert_equal [err_message_string, err_message_empty], result.errors.message_list
170
180
  end
171
181
 
172
- it 'can discard invalid elements' do
182
+ it 'can make invalid elements nil' do
173
183
  f = Objective::Filters::ArrayFilter.new(:arr) do
174
- integer invalid: Objective::DISCARD
184
+ integer invalid: nil
175
185
  end
176
186
 
177
187
  result = f.feed([1, '2', 'three', '4', 5, [6]])
178
188
  assert_nil result.errors
179
- assert_equal [1, 2, 4, 5], result.inputs
189
+ assert_equal [1, 2, nil, 4, 5, nil], result.inputs
180
190
  end
181
191
 
182
- it 'can discard nil elements' do
192
+ it 'can allow nil elements' do
183
193
  f = Objective::Filters::ArrayFilter.new(:arr) do
184
- integer nils: Objective::DISCARD
194
+ integer nils: Objective::ALLOW
185
195
  end
186
196
 
187
197
  result = f.feed([nil, 1, '2', nil, nil, '4', 5, nil])
188
198
  assert_nil result.errors
189
- assert_equal [1, 2, 4, 5], result.inputs
199
+ assert_equal [nil, 1, 2, nil, nil, 4, 5, nil], result.inputs
190
200
  end
191
201
 
192
- it 'can discard empty elements' do
202
+ it 'can allow empty elements' do
193
203
  f = Objective::Filters::ArrayFilter.new(:arr) do
194
- string empty: Objective::DISCARD
204
+ string empty: Objective::ALLOW
195
205
  end
196
206
 
197
- result = f.feed(['', 1, '2', '', '', '4', 5, ''])
207
+ result = f.feed(['', 'foo', '', '', 'bar', ''])
198
208
  assert_nil result.errors
199
- assert_equal %w[1 2 4 5], result.inputs
209
+ assert_equal ['', 'foo', '', '', 'bar', ''], result.inputs
200
210
  end
201
211
  end
@@ -3,111 +3,96 @@
3
3
  require 'test_helper'
4
4
 
5
5
  describe 'Objective::Filters::RootFilter' do
6
- describe 'optional filters and nils' do
7
- it 'bar is optional -- it works if not passed' do
8
- hf = Objective::Filters::RootFilter.new do
6
+ describe 'by default' do
7
+ it 'does not allow missing keys' do
8
+ f = Objective::Filters::RootFilter.new do
9
9
  filter do
10
10
  string :foo
11
- string :bar, none: Objective::ALLOW
11
+ string :bar
12
12
  end
13
13
  end
14
14
 
15
- result = hf.feed(foo: 'bar')
16
- assert_equal OpenStruct.new(foo: 'bar'), result.inputs
17
- assert_nil result.errors
15
+ result = f.feed(foo: 'oof')
16
+ assert_equal :nils, result.errors[:bar].codes
18
17
  end
19
18
 
20
- it 'bar is optional -- it works if nil is passed' do
21
- hf = Objective::Filters::RootFilter.new do
19
+ it 'does not allow nil values' do
20
+ f = Objective::Filters::RootFilter.new do
22
21
  filter do
23
22
  string :foo
24
- string :bar, nils: Objective::DISCARD
23
+ string :bar
25
24
  end
26
25
  end
27
26
 
28
- result = hf.feed(foo: 'bar', bar: nil)
29
- assert_equal OpenStruct.new(foo: 'bar'), result.inputs
30
- assert_nil result.errors
27
+ result = f.feed(foo: 'oof', bar: nil)
28
+ assert_equal :nils, result.errors[:bar].codes
31
29
  end
32
30
 
33
- it 'bar is optional -- it works if nil is passed and nils are allowed' do
34
- hf = Objective::Filters::RootFilter.new do
31
+ it 'does not allow empty values' do
32
+ f = Objective::Filters::RootFilter.new do
35
33
  filter do
36
34
  string :foo
37
- string :bar, nils: Objective::ALLOW
35
+ string :bar
38
36
  end
39
37
  end
40
38
 
41
- result = hf.feed(foo: 'bar', bar: nil)
42
- assert_equal OpenStruct.new(foo: 'bar', bar: nil), result.inputs
43
- assert_nil result.errors
39
+ result = f.feed(foo: 'oof', bar: ' ')
40
+ assert_equal :empty, result.errors[:bar].codes
44
41
  end
45
42
  end
46
43
 
47
- describe 'optional filters and empty values' do
48
- it 'bar is optional -- discards empty' do
49
- hf = Objective::Filters::RootFilter.new do
44
+ describe 'when nils are allowed' do
45
+ it 'can accept a nil value' do
46
+ f = Objective::Filters::RootFilter.new do
50
47
  filter do
51
48
  string :foo
52
- string :bar, empty: Objective::DISCARD
49
+ string :bar, nils: Objective::ALLOW
53
50
  end
54
51
  end
55
52
 
56
- result = hf.feed(foo: 'bar', bar: '')
57
- assert_equal OpenStruct.new(foo: 'bar'), result.inputs
53
+ result = f.feed(foo: 'oof', bar: nil)
54
+ assert_equal({ 'foo' => 'oof', 'bar' => nil }, result.inputs)
58
55
  assert_nil result.errors
59
56
  end
60
57
 
61
- it 'bar is optional -- discards empty if it needs to be squished' do
62
- hf = Objective::Filters::RootFilter.new do
58
+ it 'uses nil when a value is not passed' do
59
+ f = Objective::Filters::RootFilter.new do
63
60
  filter do
64
61
  string :foo
65
- string :bar, empty: Objective::DISCARD
62
+ string :bar, nils: Objective::ALLOW
66
63
  end
67
64
  end
68
65
 
69
- result = hf.feed(foo: 'bar', bar: ' ')
70
- assert_equal OpenStruct.new(foo: 'bar'), result.inputs
66
+ result = f.feed(foo: 'oof')
67
+ assert_equal({ 'foo' => 'oof', 'bar' => nil }, result.inputs)
71
68
  assert_nil result.errors
72
69
  end
70
+ end
73
71
 
74
- it "bar is optional -- don't discard empty if it's spaces but squishing is off" do
75
- hf = Objective::Filters::RootFilter.new do
72
+ describe 'when empty values are allowed' do
73
+ it 'can accept an empty value' do
74
+ f = Objective::Filters::RootFilter.new do
76
75
  filter do
77
76
  string :foo
78
- string :bar, empty: Objective::DISCARD, squish: false
77
+ string :bar, empty: Objective::ALLOW
79
78
  end
80
79
  end
81
80
 
82
- result = hf.feed(foo: 'bar', bar: ' ')
83
- assert_equal OpenStruct.new(foo: 'bar', bar: ' '), result.inputs
81
+ result = f.feed(foo: 'bar', bar: '')
82
+ assert_equal({ 'foo' => 'bar', 'bar' => '' }, result.inputs)
84
83
  assert_nil result.errors
85
84
  end
86
85
 
87
- it 'bar is optional -- errors if value is blank' do
88
- hf = Objective::Filters::RootFilter.new do
89
- filter do
90
- string :foo
91
- string :bar, none: Objective::ALLOW
92
- end
93
- end
94
-
95
- result = hf.feed(foo: 'bar', bar: '')
96
- assert_equal({ 'bar' => :empty }, result.errors.codes)
97
- end
98
- end
99
-
100
- describe 'discarding invalid values' do
101
- it 'should discard invalid optional values' do
102
- hf = Objective::Filters::RootFilter.new do
86
+ it 'can default a nil value to being empty' do
87
+ f = Objective::Filters::RootFilter.new do
103
88
  filter do
104
89
  string :foo
105
- integer :bar, invalid: Objective::DISCARD
90
+ string :bar, empty: Objective::ALLOW, nils: ''
106
91
  end
107
92
  end
108
93
 
109
- result = hf.feed(foo: 'bar', bar: 'baz')
110
- assert_equal OpenStruct.new(foo: 'bar'), result.inputs
94
+ result = f.feed(foo: 'bar', bar: '')
95
+ assert_equal({ 'foo' => 'bar', 'bar' => '' }, result.inputs)
111
96
  assert_nil result.errors
112
97
  end
113
98
  end
@@ -6,7 +6,7 @@ class SimpleUnit
6
6
  filter do
7
7
  string :name, max: 10
8
8
  string :email
9
- integer :amount, none: ALLOW
9
+ integer :amount, nils: ALLOW
10
10
  end
11
11
 
12
12
  def validate
@@ -9,7 +9,7 @@ describe 'Unit' do
9
9
  outcome = SimpleUnit.run(name: 'John', email: 'john@gmail.com', amount: 5)
10
10
 
11
11
  assert outcome.success
12
- assert_equal OpenStruct.new(name: 'John', email: 'john@gmail.com', amount: 5), outcome.inputs
12
+ assert_equal({ 'name' => 'John', 'email' => 'john@gmail.com', 'amount' => 5 }, outcome.inputs)
13
13
  assert_nil outcome.errors
14
14
  end
15
15
 
@@ -17,7 +17,7 @@ describe 'Unit' do
17
17
  outcome = SimpleUnit.run(name: 'John', email: 'john@gmail.com', amount: 5, buggers: true)
18
18
 
19
19
  assert outcome.success
20
- assert_equal OpenStruct.new(name: 'John', email: 'john@gmail.com', amount: 5), outcome.inputs
20
+ assert_equal({ 'name' => 'John', 'email' => 'john@gmail.com', 'amount' => 5 }, outcome.inputs)
21
21
  assert_nil outcome.errors
22
22
  end
23
23
 
@@ -77,14 +77,14 @@ describe 'Unit' do
77
77
  outcome = SimpleUnit.run({ name: 'John', email: 'john@gmail.com' }, email: 'bob@jones.com', amount: 5)
78
78
 
79
79
  assert outcome.success
80
- assert_equal OpenStruct.new(name: 'John', email: 'bob@jones.com', amount: 5), outcome.inputs
80
+ assert_equal({ 'name' => 'John', 'email' => 'bob@jones.com', 'amount' => 5 }, outcome.inputs)
81
81
  end
82
82
 
83
83
  it 'should merge hashes indifferently' do
84
84
  outcome = SimpleUnit.run({ name: 'John', email: 'john@gmail.com' }, 'email' => 'bob@jones.com', 'amount' => 5)
85
85
 
86
86
  assert outcome.success
87
- assert_equal OpenStruct.new(name: 'John', email: 'bob@jones.com', amount: 5), outcome.inputs
87
+ assert_equal({ 'name' => 'John', 'email' => 'bob@jones.com', 'amount' => 5 }, outcome.inputs)
88
88
  end
89
89
 
90
90
  it 'shouldn\'t accept non-hashes' do
@@ -107,7 +107,7 @@ describe 'Unit' do
107
107
 
108
108
  it 'should return the filtered inputs in the outcome' do
109
109
  outcome = SimpleUnit.run(name: ' John ', email: 'john@gmail.com', amount: '5')
110
- assert_equal(OpenStruct.new(name: 'John', email: 'john@gmail.com', amount: 5), outcome.inputs)
110
+ assert_equal({ 'name' => 'John', 'email' => 'john@gmail.com', 'amount' => 5 }, outcome.inputs)
111
111
  end
112
112
  end
113
113
 
@@ -116,7 +116,7 @@ describe 'Unit' do
116
116
  include Objective::Unit
117
117
  filter do
118
118
  string :name
119
- string :email, none: ALLOW
119
+ string :email, nils: ALLOW
120
120
  end
121
121
 
122
122
  def execute
@@ -135,12 +135,12 @@ describe 'Unit' do
135
135
  include Objective::Unit
136
136
  filter do
137
137
  string :name
138
- string :email, none: ALLOW
138
+ string :email, nils: ALLOW
139
139
  end
140
140
 
141
141
  def execute
142
- inputs.name = 'bob'
143
- inputs.email = 'bob@jones.com'
142
+ inputs[:name] = 'bob'
143
+ inputs[:email] = 'bob@jones.com'
144
144
  { name: inputs[:name], email: inputs[:email] }
145
145
  end
146
146
  end
@@ -156,7 +156,7 @@ describe 'Unit' do
156
156
  include Objective::Unit
157
157
  filter do
158
158
  string :name
159
- string :email, none: ALLOW
159
+ string :email, nils: ALLOW
160
160
  end
161
161
 
162
162
  def execute
@@ -179,7 +179,7 @@ describe 'Unit' do
179
179
  include Objective::Unit
180
180
  filter do
181
181
  string :name
182
- string :email, none: ALLOW
182
+ string :email, nils: ALLOW
183
183
  end
184
184
 
185
185
  def execute
@@ -202,7 +202,7 @@ describe 'Unit' do
202
202
  include Objective::Unit
203
203
  filter do
204
204
  string :name
205
- string :email, none: ALLOW
205
+ string :email, nils: ALLOW
206
206
  end
207
207
 
208
208
  def execute
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.2.2
4
+ version: 0.3.0
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-31 00:00:00.000000000 Z
11
+ date: 2017-06-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -81,7 +81,6 @@ files:
81
81
  - lib/objective.rb
82
82
  - lib/objective/allow.rb
83
83
  - lib/objective/deny.rb
84
- - lib/objective/discard.rb
85
84
  - lib/objective/errors/error_array.rb
86
85
  - lib/objective/errors/error_atom.rb
87
86
  - lib/objective/errors/error_hash.rb
@@ -102,7 +101,6 @@ files:
102
101
  - lib/objective/filters/root_filter.rb
103
102
  - lib/objective/filters/string_filter.rb
104
103
  - lib/objective/filters/time_filter.rb
105
- - lib/objective/none.rb
106
104
  - lib/objective/outcome.rb
107
105
  - lib/objective/unit.rb
108
106
  - test/default_test.rb
@@ -1,11 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'singleton'
4
-
5
- module Objective
6
- class Discard
7
- include Singleton
8
- end
9
- end
10
-
11
- Objective::DISCARD = Objective::Discard.instance.freeze
@@ -1,11 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'singleton'
4
-
5
- module Objective
6
- class None
7
- include Singleton
8
- end
9
- end
10
-
11
- Objective::NONE = Objective::None.instance.freeze