strip_attributes 1.8.0 → 1.8.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/LICENSE.txt +1 -1
- data/README.md +20 -18
- data/Rakefile +1 -1
- data/lib/strip_attributes/version.rb +1 -1
- data/test/matchers_test.rb +2 -2
- data/test/strip_attributes_test.rb +62 -39
- metadata +8 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 287d7a12033039a5d2f06d55433a9f613ad584886a4add18b78ff6f432455073
|
4
|
+
data.tar.gz: d30d2f78245bfce1534c0509366fa12cbb0bdea7945f2dd97547bb257a926053
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1e6fbbb056f4ad22e8d4d13c767f3ae2124ede7a7d39e5cffcb9a406be4f590cfbb0f495cf2c6ea0fa8efc1e750e2e333b8eb4467d1ffee582c36deabb2b1417
|
7
|
+
data.tar.gz: ec6311504c2dd730e4810cce0cd72aaf9a2ce5cdbe383214c7e8e46c98cacc82732f8304c90128646c26cc7f99c91227f2cb2f51fcc844d8265e04040db5a688
|
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -2,7 +2,6 @@
|
|
2
2
|
|
3
3
|
[![Gem Version](http://img.shields.io/gem/v/strip_attributes.svg)](https://rubygems.org/gems/strip_attributes)
|
4
4
|
[![Build Status](https://secure.travis-ci.org/rmm5t/strip_attributes.svg)](http://travis-ci.org/rmm5t/strip_attributes)
|
5
|
-
[![Code Climate](http://img.shields.io/codeclimate/github/rmm5t/strip_attributes.svg)](https://codeclimate.com/github/rmm5t/strip_attributes)
|
6
5
|
[![Gem Downloads](https://img.shields.io/gem/dt/strip_attributes.svg)](https://rubygems.org/gems/strip_attributes)
|
7
6
|
|
8
7
|
StripAttributes is an ActiveModel extension that automatically strips all
|
@@ -12,8 +11,7 @@ attribute is blank, it strips the value to `nil` by default.
|
|
12
11
|
It works by adding a before_validation hook to the record. By default, all
|
13
12
|
attributes are stripped of whitespace, but `:only` and `:except`
|
14
13
|
options can be used to limit which attributes are stripped. Both options accept
|
15
|
-
a single attribute (
|
16
|
-
[:field1, :field2, :field3]`).
|
14
|
+
a single attribute (`only: :field`) or arrays of attributes (`except: [:field1, :field2, :field3]`).
|
17
15
|
|
18
16
|
---
|
19
17
|
|
@@ -56,7 +54,7 @@ end
|
|
56
54
|
```ruby
|
57
55
|
# all attributes will be stripped except :boxers
|
58
56
|
class SoberPokerPlayer < ActiveRecord::Base
|
59
|
-
strip_attributes :
|
57
|
+
strip_attributes except: :boxers
|
60
58
|
end
|
61
59
|
```
|
62
60
|
|
@@ -65,7 +63,7 @@ end
|
|
65
63
|
```ruby
|
66
64
|
# only :shoe, :sock, and :glove attributes will be stripped
|
67
65
|
class ConservativePokerPlayer < ActiveRecord::Base
|
68
|
-
strip_attributes :
|
66
|
+
strip_attributes only: [:shoe, :sock, :glove]
|
69
67
|
end
|
70
68
|
```
|
71
69
|
|
@@ -74,7 +72,7 @@ end
|
|
74
72
|
```ruby
|
75
73
|
# Empty attributes will not be converted to nil
|
76
74
|
class BrokePokerPlayer < ActiveRecord::Base
|
77
|
-
strip_attributes :
|
75
|
+
strip_attributes allow_empty: true
|
78
76
|
end
|
79
77
|
```
|
80
78
|
|
@@ -83,7 +81,7 @@ end
|
|
83
81
|
```ruby
|
84
82
|
# Sequential spaces in attributes will be collapsed to one space
|
85
83
|
class EloquentPokerPlayer < ActiveRecord::Base
|
86
|
-
strip_attributes :
|
84
|
+
strip_attributes collapse_spaces: true
|
87
85
|
end
|
88
86
|
```
|
89
87
|
|
@@ -92,7 +90,7 @@ end
|
|
92
90
|
```ruby
|
93
91
|
# Newlines in attributes will be replaced with a space
|
94
92
|
class EloquentPokerPlayer < ActiveRecord::Base
|
95
|
-
strip_attributes :
|
93
|
+
strip_attributes replace_newlines: true
|
96
94
|
end
|
97
95
|
```
|
98
96
|
|
@@ -101,11 +99,16 @@ end
|
|
101
99
|
```ruby
|
102
100
|
class User < ActiveRecord::Base
|
103
101
|
# Strip off characters defined by RegEx
|
104
|
-
strip_attributes :
|
102
|
+
strip_attributes only: [:first_name, :last_name], regex: /[^[:alpha:]\s]/
|
103
|
+
|
105
104
|
# Strip off non-integers
|
106
|
-
strip_attributes :
|
105
|
+
strip_attributes only: :phone, regex: /[^0-9]/
|
106
|
+
|
107
107
|
# Strip off all spaces and keep only alphabetic and numeric characters
|
108
|
-
strip_attributes :
|
108
|
+
strip_attributes only: :nickname, regex: /[^[:alnum:]_-]/
|
109
|
+
|
110
|
+
# Remove trailing whitespace from a multi-line string
|
111
|
+
strip_attributes only: :code, regex: /[[:blank:]]+$/)
|
109
112
|
end
|
110
113
|
```
|
111
114
|
|
@@ -118,7 +121,7 @@ It also works on other ActiveModel classes, such as [Mongoid](http://mongoid.org
|
|
118
121
|
```ruby
|
119
122
|
class User
|
120
123
|
include Mongoid::Document
|
121
|
-
strip_attributes :
|
124
|
+
strip_attributes only: :email
|
122
125
|
end
|
123
126
|
```
|
124
127
|
|
@@ -141,11 +144,11 @@ end
|
|
141
144
|
|
142
145
|
```ruby
|
143
146
|
# where record is an ActiveModel instance
|
144
|
-
StripAttributes.strip(record, :
|
147
|
+
StripAttributes.strip(record, collapse_spaces: true)
|
145
148
|
|
146
149
|
# works directly on Strings too
|
147
150
|
StripAttributes.strip(" foo \t") #=> "foo"
|
148
|
-
StripAttributes.strip(" foo bar", :
|
151
|
+
StripAttributes.strip(" foo bar", collapse_spaces: true) #=> "foo bar"
|
149
152
|
```
|
150
153
|
|
151
154
|
## Testing
|
@@ -189,7 +192,7 @@ end
|
|
189
192
|
#### To initialize **Minitest-MatchersVaccine**, add this to your `test_helper.rb`:
|
190
193
|
|
191
194
|
```ruby
|
192
|
-
require "strip_attributes/
|
195
|
+
require "strip_attributes/matchers"
|
193
196
|
class MiniTest::Spec
|
194
197
|
include StripAttributes::Matchers
|
195
198
|
end
|
@@ -198,7 +201,7 @@ end
|
|
198
201
|
OR if in a Rails environment, you might prefer this:
|
199
202
|
|
200
203
|
``` ruby
|
201
|
-
require "strip_attributes/
|
204
|
+
require "strip_attributes/matchers"
|
202
205
|
class ActiveSupport::TestCase
|
203
206
|
include StripAttributes::Matchers
|
204
207
|
end
|
@@ -280,5 +283,4 @@ Semantic Versioning 2.0 as defined at <http://semver.org>.
|
|
280
283
|
|
281
284
|
## License
|
282
285
|
|
283
|
-
|
284
|
-
license](http://en.wikipedia.org/wiki/MIT_License)
|
286
|
+
[MIT License](https://rmm5t.mit-license.org/)
|
data/Rakefile
CHANGED
data/test/matchers_test.rb
CHANGED
@@ -13,11 +13,11 @@ class SampleMockRecord < Tableless
|
|
13
13
|
attribute :unstripped1
|
14
14
|
attribute :unstripped2
|
15
15
|
attribute :unstripped3
|
16
|
-
strip_attributes :
|
16
|
+
strip_attributes only: [:stripped1, :stripped2, :stripped3]
|
17
17
|
|
18
18
|
attribute :collapsed
|
19
19
|
attribute :uncollapsed
|
20
|
-
strip_attributes :
|
20
|
+
strip_attributes only: [:collapsed], collapse_spaces: true
|
21
21
|
end
|
22
22
|
|
23
23
|
describe SampleMockRecord do
|
@@ -19,69 +19,69 @@ end
|
|
19
19
|
|
20
20
|
class StripOnlyOneMockRecord < Tableless
|
21
21
|
include MockAttributes
|
22
|
-
strip_attributes :
|
22
|
+
strip_attributes only: :foo
|
23
23
|
end
|
24
24
|
|
25
25
|
class StripOnlyThreeMockRecord < Tableless
|
26
26
|
include MockAttributes
|
27
|
-
strip_attributes :
|
27
|
+
strip_attributes only: [:foo, :bar, :biz]
|
28
28
|
end
|
29
29
|
|
30
30
|
class StripExceptOneMockRecord < Tableless
|
31
31
|
include MockAttributes
|
32
|
-
strip_attributes :
|
32
|
+
strip_attributes except: :foo
|
33
33
|
end
|
34
34
|
|
35
35
|
class StripExceptThreeMockRecord < Tableless
|
36
36
|
include MockAttributes
|
37
|
-
strip_attributes :
|
37
|
+
strip_attributes except: [:foo, :bar, :biz]
|
38
38
|
end
|
39
39
|
|
40
40
|
class StripAllowEmpty < Tableless
|
41
41
|
include MockAttributes
|
42
|
-
strip_attributes :
|
42
|
+
strip_attributes allow_empty: true
|
43
43
|
end
|
44
44
|
|
45
45
|
class CollapseDuplicateSpaces < Tableless
|
46
46
|
include MockAttributes
|
47
|
-
strip_attributes :
|
47
|
+
strip_attributes collapse_spaces: true
|
48
48
|
end
|
49
49
|
|
50
50
|
class ReplaceNewLines < Tableless
|
51
51
|
include MockAttributes
|
52
|
-
strip_attributes :
|
52
|
+
strip_attributes replace_newlines: true
|
53
53
|
end
|
54
54
|
|
55
55
|
class ReplaceNewLinesAndDuplicateSpaces < Tableless
|
56
56
|
include MockAttributes
|
57
|
-
strip_attributes :
|
57
|
+
strip_attributes replace_newlines: true, collapse_spaces: true
|
58
58
|
end
|
59
59
|
|
60
60
|
class CoexistWithOtherValidations < Tableless
|
61
|
-
attribute :number, :
|
61
|
+
attribute :number, type: Integer
|
62
62
|
|
63
63
|
strip_attributes
|
64
64
|
validates :number, {
|
65
|
-
:
|
66
|
-
:
|
65
|
+
numericality: { only_integer: true, greater_than_or_equal_to: 1000 },
|
66
|
+
allow_blank: true
|
67
67
|
}
|
68
68
|
end
|
69
69
|
|
70
70
|
class StripRegexMockRecord < Tableless
|
71
71
|
include MockAttributes
|
72
|
-
strip_attributes :
|
72
|
+
strip_attributes regex: /[\^\%&\*]/
|
73
73
|
end
|
74
74
|
|
75
75
|
class StripAttributesTest < Minitest::Test
|
76
76
|
def setup
|
77
77
|
@init_params = {
|
78
|
-
:
|
79
|
-
:
|
80
|
-
:
|
81
|
-
:
|
82
|
-
:
|
83
|
-
:
|
84
|
-
:
|
78
|
+
foo: "\tfoo",
|
79
|
+
bar: "bar \t ",
|
80
|
+
biz: "\tbiz ",
|
81
|
+
baz: "",
|
82
|
+
bang: " ",
|
83
|
+
foz: " foz foz",
|
84
|
+
fiz: "fiz \n fiz"
|
85
85
|
}
|
86
86
|
end
|
87
87
|
|
@@ -169,8 +169,8 @@ class StripAttributesTest < Minitest::Test
|
|
169
169
|
assert_equal "biz", record.biz
|
170
170
|
assert_equal "foz foz", record.foz
|
171
171
|
assert_equal "fiz \n fiz", record.fiz
|
172
|
-
|
173
|
-
|
172
|
+
assert_nil record.baz
|
173
|
+
assert_nil record.bang
|
174
174
|
end
|
175
175
|
|
176
176
|
def test_should_replace_newlines
|
@@ -181,8 +181,8 @@ class StripAttributesTest < Minitest::Test
|
|
181
181
|
assert_equal "biz", record.biz
|
182
182
|
assert_equal "foz foz", record.foz
|
183
183
|
assert_equal "fiz fiz", record.fiz
|
184
|
-
|
185
|
-
|
184
|
+
assert_nil record.baz
|
185
|
+
assert_nil record.bang
|
186
186
|
end
|
187
187
|
|
188
188
|
def test_should_replace_newlines_and_duplicate_spaces
|
@@ -193,8 +193,8 @@ class StripAttributesTest < Minitest::Test
|
|
193
193
|
assert_equal "biz", record.biz
|
194
194
|
assert_equal "foz foz", record.foz
|
195
195
|
assert_equal "fiz fiz", record.fiz
|
196
|
-
|
197
|
-
|
196
|
+
assert_nil record.baz
|
197
|
+
assert_nil record.bang
|
198
198
|
end
|
199
199
|
|
200
200
|
def test_should_strip_and_allow_empty_always
|
@@ -217,18 +217,18 @@ class StripAttributesTest < Minitest::Test
|
|
217
217
|
assert !record.valid?, "Expected record to be invalid"
|
218
218
|
assert record.errors.include?(:number), "Expected record to have an error on :number"
|
219
219
|
|
220
|
-
record = CoexistWithOtherValidations.new(:
|
220
|
+
record = CoexistWithOtherValidations.new(number: " 1000.2 ")
|
221
221
|
assert !record.valid?, "Expected record to be invalid"
|
222
222
|
assert record.errors.include?(:number), "Expected record to have an error on :number"
|
223
223
|
|
224
|
-
# record = CoexistWithOtherValidations.new(:
|
224
|
+
# record = CoexistWithOtherValidations.new(number: " 1000 ")
|
225
225
|
# assert record.valid?, "Expected record to be valid, but got #{record.errors.full_messages}"
|
226
226
|
# assert !record.errors.include?(:number), "Expected record to have no errors on :number"
|
227
227
|
end
|
228
228
|
|
229
229
|
def test_should_strip_regex
|
230
230
|
record = StripRegexMockRecord.new
|
231
|
-
record.assign_attributes(@init_params.merge(:
|
231
|
+
record.assign_attributes(@init_params.merge(foo: "^%&*abc "))
|
232
232
|
record.valid?
|
233
233
|
assert_equal "abc", record.foo
|
234
234
|
assert_equal "bar", record.bar
|
@@ -237,39 +237,62 @@ class StripAttributesTest < Minitest::Test
|
|
237
237
|
def test_should_strip_unicode
|
238
238
|
skip "multi-byte characters not supported by this version of Ruby" unless StripAttributes::MULTIBYTE_SUPPORTED
|
239
239
|
|
240
|
-
record = StripOnlyOneMockRecord.new({:
|
240
|
+
record = StripOnlyOneMockRecord.new({foo: "\u200A\u200B foo\u200A\u200B\u00A0 "})
|
241
241
|
record.valid?
|
242
242
|
assert_equal "foo", record.foo
|
243
243
|
end
|
244
244
|
|
245
245
|
class ClassMethodsTest < Minitest::Test
|
246
246
|
def test_should_strip_whitespace
|
247
|
-
|
248
|
-
|
247
|
+
assert_nil StripAttributes.strip("")
|
248
|
+
assert_nil StripAttributes.strip(" \t ")
|
249
249
|
assert_equal "thirty six", StripAttributes.strip(" thirty six \t \n")
|
250
250
|
end
|
251
251
|
|
252
252
|
def test_should_allow_empty
|
253
|
-
assert_equal "", StripAttributes.strip("", :
|
254
|
-
assert_equal "", StripAttributes.strip(" \t ", :
|
253
|
+
assert_equal "", StripAttributes.strip("", allow_empty: true)
|
254
|
+
assert_equal "", StripAttributes.strip(" \t ", allow_empty: true)
|
255
255
|
end
|
256
256
|
|
257
257
|
def test_should_collapse_spaces
|
258
|
-
assert_equal "1 2 3", StripAttributes.strip(" 1 2 3\t ", :
|
258
|
+
assert_equal "1 2 3", StripAttributes.strip(" 1 2 3\t ", collapse_spaces: true)
|
259
259
|
end
|
260
260
|
|
261
261
|
def test_should_collapse_multibyte_spaces
|
262
|
-
assert_equal "1 2 3", StripAttributes.strip(" 1 \u00A0 2\u00A03\t ", :
|
262
|
+
assert_equal "1 2 3", StripAttributes.strip(" 1 \u00A0 2\u00A03\t ", collapse_spaces: true)
|
263
263
|
end
|
264
264
|
|
265
265
|
def test_should_replace_newlines
|
266
|
-
assert_equal "1 2", StripAttributes.strip("1\n2", :
|
267
|
-
assert_equal "1 2", StripAttributes.strip("1\r\n2", :
|
268
|
-
assert_equal "1 2", StripAttributes.strip("1\r2", :
|
266
|
+
assert_equal "1 2", StripAttributes.strip("1\n2", replace_newlines: true)
|
267
|
+
assert_equal "1 2", StripAttributes.strip("1\r\n2", replace_newlines: true)
|
268
|
+
assert_equal "1 2", StripAttributes.strip("1\r2", replace_newlines: true)
|
269
269
|
end
|
270
270
|
|
271
271
|
def test_should_strip_regex
|
272
|
-
assert_equal "abc", StripAttributes.strip("^%&*abc ^ ", :
|
272
|
+
assert_equal "abc", StripAttributes.strip("^%&*abc ^ ", regex: /[\^\%&\*]/)
|
273
|
+
end
|
274
|
+
|
275
|
+
def test_should_keep_only_alphanumerics
|
276
|
+
nickname = " funky BAT-2009"
|
277
|
+
assert_equal "funkyBAT-2009", StripAttributes.strip(nickname, regex: /[^[:alnum:]_-]/)
|
278
|
+
end
|
279
|
+
|
280
|
+
def test_should_strip_trailing_whitespace
|
281
|
+
messy_code =
|
282
|
+
"const hello = (name) => { \n" +
|
283
|
+
" if (name === 'voldemort') return; \n" +
|
284
|
+
" \n" +
|
285
|
+
" console.log(`Hello ${name}!`); \t \t \n" +
|
286
|
+
"}; \n"
|
287
|
+
expected = <<~EOF.strip
|
288
|
+
const hello = (name) => {
|
289
|
+
if (name === 'voldemort') return;
|
290
|
+
|
291
|
+
console.log(`Hello ${name}!`);
|
292
|
+
};
|
293
|
+
EOF
|
294
|
+
actual = StripAttributes.strip(messy_code, regex: /[[:blank:]]+$/)
|
295
|
+
assert_equal expected, actual
|
273
296
|
end
|
274
297
|
|
275
298
|
def test_should_strip_unicode
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: strip_attributes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.8.
|
4
|
+
version: 1.8.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryan McGeary
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-01-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -19,7 +19,7 @@ dependencies:
|
|
19
19
|
version: '3.0'
|
20
20
|
- - "<"
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version: '
|
22
|
+
version: '7.0'
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -29,21 +29,21 @@ dependencies:
|
|
29
29
|
version: '3.0'
|
30
30
|
- - "<"
|
31
31
|
- !ruby/object:Gem::Version
|
32
|
-
version: '
|
32
|
+
version: '7.0'
|
33
33
|
- !ruby/object:Gem::Dependency
|
34
34
|
name: active_attr
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|
36
36
|
requirements:
|
37
37
|
- - "~>"
|
38
38
|
- !ruby/object:Gem::Version
|
39
|
-
version: '0.
|
39
|
+
version: '0.10'
|
40
40
|
type: :development
|
41
41
|
prerelease: false
|
42
42
|
version_requirements: !ruby/object:Gem::Requirement
|
43
43
|
requirements:
|
44
44
|
- - "~>"
|
45
45
|
- !ruby/object:Gem::Version
|
46
|
-
version: '0.
|
46
|
+
version: '0.10'
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: minitest
|
49
49
|
requirement: !ruby/object:Gem::Requirement
|
@@ -138,15 +138,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
138
138
|
requirements:
|
139
139
|
- - ">="
|
140
140
|
- !ruby/object:Gem::Version
|
141
|
-
version:
|
141
|
+
version: 1.9.3
|
142
142
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
143
143
|
requirements:
|
144
144
|
- - ">="
|
145
145
|
- !ruby/object:Gem::Version
|
146
146
|
version: '0'
|
147
147
|
requirements: []
|
148
|
-
|
149
|
-
rubygems_version: 2.5.1
|
148
|
+
rubygems_version: 3.0.1
|
150
149
|
signing_key:
|
151
150
|
specification_version: 4
|
152
151
|
summary: Whitespace cleanup for ActiveModel attributes
|