strip_attributes 1.9.0 → 1.9.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/README.md +0 -18
- data/lib/strip_attributes.rb +9 -15
- data/lib/strip_attributes/version.rb +1 -1
- data/test/strip_attributes_test.rb +7 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 45ea7e1316e058313b4aa362722d1b3eced15ed144b1c9576ef01834181d9733
|
4
|
+
data.tar.gz: f38bd5fb9ac513e5a72cedaee5caf43e72d2e08c89d1fa9e234f7f30b798b961
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ec101c79b0d25e128b848e233fbc614b87e6bf8ea6e1a8650d1863808cbb862f650ff516b89e569d0e491d3b49cdd995a24c8f37bae2875cc5ad581ef7f31aa3
|
7
|
+
data.tar.gz: 28ceffbb9193abd64abf82bf2e22ff5a492d88255209a627e7e3d03c701bc2a98b370de0d5b7c16ed2cc6c803f3fb3903bceca1b36784dd85f4d44b76c91960d
|
data/README.md
CHANGED
@@ -15,24 +15,6 @@ a single attribute (`only: :field`) or arrays of attributes (`except: [:field1,
|
|
15
15
|
|
16
16
|
It's also possible to skip stripping the attributes altogether per model using the `:if` and `:unless` options.
|
17
17
|
|
18
|
-
---
|
19
|
-
|
20
|
-
**How You Can Help**
|
21
|
-
|
22
|
-
[][square]
|
23
|
-
[][paypal]
|
24
|
-
[][codementor]
|
25
|
-
|
26
|
-
If you like this project, [buy me a coffee][paypal], or [book a session with me][codementor], or donate bitcoin: `1rmm5tv6f997JK5bLcGbRCZyVjZUPkQ2m`
|
27
|
-
|
28
|
-
[square]: https://cash.me/$rmm5t/5 "Donate to rmm5t for open source!"
|
29
|
-
[paypal]: https://www.paypal.me/rmm5t/5 "Donate to rmm5t for open source!"
|
30
|
-
[bitcoin]: bitcoin:1rmm5tv6f997JK5bLcGbRCZyVjZUPkQ2m?amount=0.01&label=Coffee%20to%20rmm5t%20for%20Open%20Source "Buy rmm5t a coffee for open source!"
|
31
|
-
[codementor]: https://www.codementor.io/rmm5t?utm_campaign=profile&utm_source=button-rmm5t&utm_medium=shields "Book a session with rmm5t on Codementor!"
|
32
|
-
|
33
|
-
[](https://twitter.com/rmm5t)
|
34
|
-
[](http://stackoverflow.com/users/8985/ryan-mcgeary)
|
35
|
-
|
36
18
|
## Installation
|
37
19
|
|
38
20
|
Include the gem in your Gemfile:
|
data/lib/strip_attributes.rb
CHANGED
@@ -19,7 +19,7 @@ module ActiveModel::Validations::HelperMethods
|
|
19
19
|
end
|
20
20
|
|
21
21
|
module StripAttributes
|
22
|
-
VALID_OPTIONS = [:only, :except, :allow_empty, :collapse_spaces, :replace_newlines, :regex, :if, :unless]
|
22
|
+
VALID_OPTIONS = [:only, :except, :allow_empty, :collapse_spaces, :replace_newlines, :regex, :if, :unless].freeze
|
23
23
|
|
24
24
|
# Unicode invisible and whitespace characters. The POSIX character class
|
25
25
|
# [:space:] corresponds to the Unicode class Z ("separator"). We also
|
@@ -32,10 +32,10 @@ module StripAttributes
|
|
32
32
|
# U+200D ZERO WIDTH JOINER
|
33
33
|
# U+2060 WORD JOINER
|
34
34
|
# U+FEFF ZERO WIDTH NO-BREAK SPACE
|
35
|
-
MULTIBYTE_WHITE = "\u180E\u200B\u200C\u200D\u2060\uFEFF"
|
36
|
-
MULTIBYTE_SPACE = /[[:space:]#{MULTIBYTE_WHITE}]
|
37
|
-
MULTIBYTE_BLANK = /[[:blank:]#{MULTIBYTE_WHITE}]
|
38
|
-
MULTIBYTE_SUPPORTED
|
35
|
+
MULTIBYTE_WHITE = "\u180E\u200B\u200C\u200D\u2060\uFEFF".freeze
|
36
|
+
MULTIBYTE_SPACE = /[[:space:]#{MULTIBYTE_WHITE}]/.freeze
|
37
|
+
MULTIBYTE_BLANK = /[[:blank:]#{MULTIBYTE_WHITE}]/.freeze
|
38
|
+
MULTIBYTE_SUPPORTED = "\u0020" == " "
|
39
39
|
|
40
40
|
def self.strip(record_or_string, options = {})
|
41
41
|
if record_or_string.respond_to?(:attributes)
|
@@ -63,13 +63,9 @@ module StripAttributes
|
|
63
63
|
replace_newlines = options[:replace_newlines]
|
64
64
|
regex = options[:regex]
|
65
65
|
|
66
|
-
if value.respond_to?(:strip)
|
67
|
-
value = (value.blank? && !allow_empty) ? nil : value.strip
|
68
|
-
end
|
66
|
+
value = value.strip if value.respond_to?(:strip)
|
69
67
|
|
70
|
-
if regex && value.respond_to?(:gsub!)
|
71
|
-
value.gsub!(regex, "")
|
72
|
-
end
|
68
|
+
value.gsub!(regex, "") if regex && value.respond_to?(:gsub!)
|
73
69
|
|
74
70
|
if MULTIBYTE_SUPPORTED && value.respond_to?(:gsub!) && Encoding.compatible?(value, MULTIBYTE_SPACE)
|
75
71
|
value.gsub!(/\A#{MULTIBYTE_SPACE}+|#{MULTIBYTE_SPACE}+\z/, "")
|
@@ -77,9 +73,7 @@ module StripAttributes
|
|
77
73
|
value.strip!
|
78
74
|
end
|
79
75
|
|
80
|
-
if replace_newlines && value.respond_to?(:gsub!)
|
81
|
-
value.gsub!(/[\r\n]+/, " ")
|
82
|
-
end
|
76
|
+
value.gsub!(/[\r\n]+/, " ") if replace_newlines && value.respond_to?(:gsub!)
|
83
77
|
|
84
78
|
if collapse_spaces
|
85
79
|
if MULTIBYTE_SUPPORTED && value.respond_to?(:gsub!) && Encoding.compatible?(value, MULTIBYTE_BLANK)
|
@@ -89,7 +83,7 @@ module StripAttributes
|
|
89
83
|
end
|
90
84
|
end
|
91
85
|
|
92
|
-
value
|
86
|
+
(value.blank? && !allow_empty) ? nil : value
|
93
87
|
end
|
94
88
|
|
95
89
|
# Necessary because Rails has removed the narrowing of attributes using :only
|
@@ -9,6 +9,8 @@ module MockAttributes
|
|
9
9
|
base.attribute :bang
|
10
10
|
base.attribute :foz
|
11
11
|
base.attribute :fiz
|
12
|
+
base.attribute :qax
|
13
|
+
base.attribute :qux
|
12
14
|
base.attribute :strip_me
|
13
15
|
base.attribute :skip_me
|
14
16
|
end
|
@@ -106,7 +108,9 @@ class StripAttributesTest < Minitest::Test
|
|
106
108
|
baz: "",
|
107
109
|
bang: " ",
|
108
110
|
foz: " foz foz",
|
109
|
-
fiz: "fiz \n fiz"
|
111
|
+
fiz: "fiz \n fiz",
|
112
|
+
qax: "\n\t ",
|
113
|
+
qux: "\u200B"
|
110
114
|
}
|
111
115
|
end
|
112
116
|
|
@@ -124,6 +128,8 @@ class StripAttributesTest < Minitest::Test
|
|
124
128
|
assert_equal "fiz \n fiz", record.fiz
|
125
129
|
assert_nil record.baz
|
126
130
|
assert_nil record.bang
|
131
|
+
assert_nil record.qax
|
132
|
+
assert_nil record.qux
|
127
133
|
end
|
128
134
|
|
129
135
|
def test_should_strip_only_one_field
|
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.9.
|
4
|
+
version: 1.9.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: 2020-02-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -145,7 +145,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
145
145
|
- !ruby/object:Gem::Version
|
146
146
|
version: '0'
|
147
147
|
requirements: []
|
148
|
-
rubygems_version: 3.0.
|
148
|
+
rubygems_version: 3.0.3
|
149
149
|
signing_key:
|
150
150
|
specification_version: 4
|
151
151
|
summary: Whitespace cleanup for ActiveModel attributes
|