mutations 0.7.0 → 0.7.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,8 @@
1
+ 0.7.1
2
+ -----------
3
+
4
+ - Bug fix: If your optional filter discards_empty and strips, then discard whitespace.
5
+
1
6
  0.7.0
2
7
  -----------
3
8
 
@@ -101,29 +101,29 @@ module Mutations
101
101
 
102
102
  data_element = data[key]
103
103
 
104
- # First, discard optional nils/empty params
105
- data.delete(key) if !is_required && data.has_key?(key) && filterer.discard_nils? && data_element.nil?
106
- data.delete(key) if !is_required && data.has_key?(key) && filterer.discard_empty? && data_element == "" # BUG: this doesn't account for data_elem being " "
107
-
108
- default_used = false
109
- if !data.has_key?(key) && filterer.has_default?
110
- data_element = filterer.default
111
- default_used = true
112
- end
113
-
114
- if data.has_key?(key) || default_used
104
+ if data.has_key?(key)
115
105
  sub_data, sub_error = filterer.filter(data_element)
116
106
 
117
107
  if sub_error.nil?
118
108
  filtered_data[key] = sub_data
119
109
  elsif !is_required && filterer.discard_invalid?
120
110
  data.delete(key)
111
+ elsif !is_required && sub_error == :empty && filterer.discard_empty?
112
+ data.delete(key)
113
+ elsif !is_required && sub_error == :nils && filterer.discard_nils?
114
+ data.delete(key)
121
115
  else
122
116
  sub_error = ErrorAtom.new(key, sub_error) if sub_error.is_a?(Symbol)
123
117
  errors[key] = sub_error
124
118
  end
125
- elsif is_required
126
- errors[key] = ErrorAtom.new(key, :required)
119
+ end
120
+
121
+ if !data.has_key?(key)
122
+ if filterer.has_default?
123
+ filtered_data[key] = filterer.default
124
+ elsif is_required
125
+ errors[key] = ErrorAtom.new(key, :required)
126
+ end
127
127
  end
128
128
  end
129
129
  end
@@ -134,15 +134,15 @@ module Mutations
134
134
  filtered_keys.each do |key|
135
135
  data_element = data[key]
136
136
 
137
- # First, discard optional nils/empty params
138
- next if data.has_key?(key) && wildcard_filterer.discard_nils? && data_element.nil?
139
- next if data.has_key?(key) && wildcard_filterer.discard_empty? && data_element == ""
140
-
141
137
  sub_data, sub_error = wildcard_filterer.filter(data_element)
142
138
  if sub_error.nil?
143
139
  filtered_data[key] = sub_data
144
140
  elsif wildcard_filterer.discard_invalid?
145
141
  data.delete(key)
142
+ elsif sub_error == :empty && wildcard_filterer.discard_empty?
143
+ data.delete(key)
144
+ elsif sub_error == :nils && wildcard_filterer.discard_nils?
145
+ data.delete(key)
146
146
  else
147
147
  sub_error = ErrorAtom.new(key, sub_error) if sub_error.is_a?(Symbol)
148
148
  errors[key] = sub_error
@@ -36,7 +36,7 @@ module Mutations
36
36
  end
37
37
 
38
38
  def discard_invalid?
39
- self.options[:discard_invalid]
39
+ options[:discard_invalid]
40
40
  end
41
41
  end
42
42
  end
@@ -1,3 +1,3 @@
1
1
  module Mutations
2
- VERSION = "0.7.0"
2
+ VERSION = "0.7.1"
3
3
  end
@@ -155,6 +155,36 @@ describe "Mutations::HashFilter" do
155
155
  assert_equal ({"foo" => "bar"}), filtered
156
156
  assert_equal nil, errors
157
157
  end
158
+
159
+ it "bar is optional -- discards empty if it needs to be stripped" do
160
+ hf = Mutations::HashFilter.new do
161
+ required do
162
+ string :foo
163
+ end
164
+ optional do
165
+ string :bar, :discard_empty => true
166
+ end
167
+ end
168
+
169
+ filtered, errors = hf.filter(:foo => "bar", :bar => " ")
170
+ assert_equal ({"foo" => "bar"}), filtered
171
+ assert_equal nil, errors
172
+ end
173
+
174
+ it "bar is optional -- don't discard empty if it's spaces but stripping is off" do
175
+ hf = Mutations::HashFilter.new do
176
+ required do
177
+ string :foo
178
+ end
179
+ optional do
180
+ string :bar, :discard_empty => true, :strip => false
181
+ end
182
+ end
183
+
184
+ filtered, errors = hf.filter(:foo => "bar", :bar => " ")
185
+ assert_equal ({"foo" => "bar", "bar" => " "}), filtered
186
+ assert_equal nil, errors
187
+ end
158
188
 
159
189
  it "bar is optional -- errors if discard_empty is false and value is blank" do
160
190
  hf = Mutations::HashFilter.new do
@@ -180,7 +210,7 @@ describe "Mutations::HashFilter" do
180
210
  end
181
211
  end
182
212
 
183
- filtered, errors = hf.filter(:foo => "bar", :bar => "")
213
+ filtered, errors = hf.filter(:foo => "bar", :bar => "", :baz => "\t")
184
214
  assert_equal ({"foo" => "bar"}), filtered
185
215
  assert_equal nil, errors
186
216
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mutations
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.7.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: