strip_attributes 1.9.2 → 1.12.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +9 -1
- data/lib/strip_attributes/matchers.rb +17 -8
- data/lib/strip_attributes/version.rb +1 -1
- data/lib/strip_attributes.rb +8 -16
- data/test/matchers_test.rb +10 -0
- data/test/strip_attributes_test.rb +7 -0
- metadata +10 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b8e78767cc587a9037bf167a66d1d90d65f0d4cf0b38e67f160d43c04e66d34c
|
4
|
+
data.tar.gz: '0348b7b1d3f81c068ce84277fa1da7213560af9f5a927aab0e23c4bdeca2b52f'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ef6d0002e6b59692efc5f40df76d8af73ec1da8ba1c2ff6887135fdfa4c10628fc635d8adf209872459d2c8bebda3820761a006d086984ef472323ded424e8a4
|
7
|
+
data.tar.gz: d21ad0b0d749ad52ebb5c03b88476d922b9c2034189f01a92b892ddca33e821d7dea02c5d34a33078ef1c4892e3df55791a7f41d0e0f24a6bbc2a1bed66fddec
|
data/README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# StripAttributes
|
2
2
|
|
3
3
|
[![Gem Version](http://img.shields.io/gem/v/strip_attributes.svg)](https://rubygems.org/gems/strip_attributes)
|
4
|
-
[![Build Status](https://
|
4
|
+
[![Build Status](https://github.com/rmm5t/strip_attributes/workflows/CI/badge.svg?branch=master)](https://github.com/rmm5t/strip_attributes/actions?query=workflow%3ACI)
|
5
5
|
[![Gem Downloads](https://img.shields.io/gem/dt/strip_attributes.svg)](https://rubygems.org/gems/strip_attributes)
|
6
6
|
|
7
7
|
StripAttributes is an ActiveModel extension that automatically strips all
|
@@ -234,7 +234,9 @@ end
|
|
234
234
|
describe User do
|
235
235
|
it { is_expected.to strip_attribute(:name).collapse_spaces }
|
236
236
|
it { is_expected.to strip_attribute :email }
|
237
|
+
it { is_expected.to strip_attributes(:name, :email) }
|
237
238
|
it { is_expected.not_to strip_attribute :password }
|
239
|
+
it { is_expected.not_to strip_attributes(:password, :encrypted_password) }
|
238
240
|
end
|
239
241
|
```
|
240
242
|
|
@@ -244,7 +246,9 @@ end
|
|
244
246
|
class UserTest < ActiveSupport::TestCase
|
245
247
|
should strip_attribute(:name).collapse_spaces
|
246
248
|
should strip_attribute :email
|
249
|
+
should strip_attributes(:name, :email)
|
247
250
|
should_not strip_attribute :password
|
251
|
+
should_not strip_attributes(:password, :encrypted_password)
|
248
252
|
end
|
249
253
|
```
|
250
254
|
|
@@ -257,7 +261,9 @@ describe User do
|
|
257
261
|
it "should strip attributes" do
|
258
262
|
must strip_attribute(:name).collapse_spaces
|
259
263
|
must strip_attribute :email
|
264
|
+
must strip_attributes(:name, :email)
|
260
265
|
wont strip_attribute :password
|
266
|
+
wont strip_attributes(:password, :encrypted_password)
|
261
267
|
end
|
262
268
|
end
|
263
269
|
```
|
@@ -270,7 +276,9 @@ describe User do
|
|
270
276
|
|
271
277
|
must { strip_attribute(:name).collapse_spaces }
|
272
278
|
must { strip_attribute :email }
|
279
|
+
must { strip_attributes(:name, :email) }
|
273
280
|
wont { strip_attribute :password }
|
281
|
+
wont { strip_attributes(:password, :encrypted_password) }
|
274
282
|
end
|
275
283
|
```
|
276
284
|
|
@@ -6,26 +6,35 @@ module StripAttributes
|
|
6
6
|
# RSpec Examples:
|
7
7
|
#
|
8
8
|
# it { is_expected.to strip_attribute(:first_name) }
|
9
|
+
# it { is_expected.to strip_attributes(:first_name, :last_name) }
|
9
10
|
# it { is_expected.not_to strip_attribute(:password) }
|
11
|
+
# it { is_expected.not_to strip_attributes(:password, :encrypted_password) }
|
10
12
|
#
|
11
13
|
# Minitest Matchers Examples:
|
12
14
|
#
|
13
15
|
# must { strip_attribute :first_name }
|
16
|
+
# must { strip_attributes(:first_name, :last_name) }
|
14
17
|
# wont { strip_attribute :password }
|
15
|
-
|
16
|
-
|
18
|
+
# wont { strip_attributes(:password, :encrypted_password) }
|
19
|
+
def strip_attribute(*attributes)
|
20
|
+
StripAttributeMatcher.new(attributes)
|
17
21
|
end
|
18
22
|
|
23
|
+
alias_method :strip_attributes, :strip_attribute
|
24
|
+
|
19
25
|
class StripAttributeMatcher
|
20
|
-
def initialize(
|
21
|
-
@
|
26
|
+
def initialize(attributes)
|
27
|
+
@attributes = attributes
|
22
28
|
@options = {}
|
23
29
|
end
|
24
30
|
|
25
31
|
def matches?(subject)
|
26
|
-
|
27
|
-
|
28
|
-
|
32
|
+
@attributes.all? do |attribute|
|
33
|
+
@attribute = attribute
|
34
|
+
subject.send("#{@attribute}=", " string ")
|
35
|
+
subject.valid?
|
36
|
+
subject.send(@attribute) == "string" and collapse_spaces?(subject)
|
37
|
+
end
|
29
38
|
end
|
30
39
|
|
31
40
|
def collapse_spaces
|
@@ -45,7 +54,7 @@ module StripAttributes
|
|
45
54
|
alias_method :negative_failure_message, :failure_message_when_negated # RSpec 1.1
|
46
55
|
|
47
56
|
def description
|
48
|
-
"#{expectation(false)} whitespace from ##{
|
57
|
+
"#{expectation(false)} whitespace from #{@attributes.map {|el| "##{el}" }.to_sentence}"
|
49
58
|
end
|
50
59
|
|
51
60
|
private
|
data/lib/strip_attributes.rb
CHANGED
@@ -9,19 +9,12 @@ module ActiveModel::Validations::HelperMethods
|
|
9
9
|
StripAttributes.strip(record, options)
|
10
10
|
end
|
11
11
|
end
|
12
|
-
|
13
|
-
# <b>DEPRECATED:</b> Please use <tt>strip_attributes</tt> (non-bang method)
|
14
|
-
# instead.
|
15
|
-
def strip_attributes!(options = {})
|
16
|
-
warn "[DEPRECATION] `strip_attributes!` is deprecated. Please use `strip_attributes` (non-bang method) instead."
|
17
|
-
strip_attributes(options)
|
18
|
-
end
|
19
12
|
end
|
20
13
|
|
21
14
|
module StripAttributes
|
22
15
|
VALID_OPTIONS = [:only, :except, :allow_empty, :collapse_spaces, :replace_newlines, :regex, :if, :unless].freeze
|
23
16
|
|
24
|
-
# Unicode invisible and whitespace characters.
|
17
|
+
# Unicode invisible and whitespace characters. The POSIX character class
|
25
18
|
# [:space:] corresponds to the Unicode class Z ("separator"). We also
|
26
19
|
# include the following characters from Unicode class C ("control"), which
|
27
20
|
# are spaces or invisible characters that make no sense at the start or end
|
@@ -59,28 +52,27 @@ module StripAttributes
|
|
59
52
|
|
60
53
|
def self.strip_string(value, options = {})
|
61
54
|
return value unless value.is_a?(String)
|
55
|
+
return value if value.frozen?
|
62
56
|
|
63
57
|
allow_empty = options[:allow_empty]
|
64
58
|
collapse_spaces = options[:collapse_spaces]
|
65
59
|
replace_newlines = options[:replace_newlines]
|
66
60
|
regex = options[:regex]
|
67
61
|
|
68
|
-
value
|
62
|
+
value.gsub!(regex, "") if regex
|
69
63
|
|
70
|
-
|
71
|
-
|
72
|
-
if MULTIBYTE_SUPPORTED && value.respond_to?(:gsub!) && Encoding.compatible?(value, MULTIBYTE_SPACE)
|
64
|
+
if MULTIBYTE_SUPPORTED && Encoding.compatible?(value, MULTIBYTE_SPACE)
|
73
65
|
value.gsub!(/\A#{MULTIBYTE_SPACE}+|#{MULTIBYTE_SPACE}+\z/, "")
|
74
|
-
|
66
|
+
else
|
75
67
|
value.strip!
|
76
68
|
end
|
77
69
|
|
78
|
-
value.gsub!(/[\r\n]+/, " ") if replace_newlines
|
70
|
+
value.gsub!(/[\r\n]+/, " ") if replace_newlines
|
79
71
|
|
80
72
|
if collapse_spaces
|
81
|
-
if MULTIBYTE_SUPPORTED &&
|
73
|
+
if MULTIBYTE_SUPPORTED && Encoding.compatible?(value, MULTIBYTE_BLANK)
|
82
74
|
value.gsub!(/#{MULTIBYTE_BLANK}+/, " ")
|
83
|
-
|
75
|
+
else
|
84
76
|
value.squeeze!(" ")
|
85
77
|
end
|
86
78
|
end
|
data/test/matchers_test.rb
CHANGED
@@ -92,4 +92,14 @@ describe SampleMockRecord do
|
|
92
92
|
assert true
|
93
93
|
end
|
94
94
|
end
|
95
|
+
|
96
|
+
it "should take a list of arguments" do
|
97
|
+
must strip_attribute(:stripped1, :stripped2, :stripped3)
|
98
|
+
wont strip_attribute(:unstripped1, :unstripped2, :unstripped3)
|
99
|
+
end
|
100
|
+
|
101
|
+
it "should alias strip_attribute to strip_attributes" do
|
102
|
+
must strip_attributes(:stripped1, :stripped2, :stripped3)
|
103
|
+
wont strip_attributes(:unstripped1, :unstripped2, :unstripped3)
|
104
|
+
end
|
95
105
|
end
|
@@ -13,6 +13,7 @@ module MockAttributes
|
|
13
13
|
base.attribute :qux
|
14
14
|
base.attribute :strip_me
|
15
15
|
base.attribute :skip_me
|
16
|
+
base.attribute :frozen
|
16
17
|
end
|
17
18
|
end
|
18
19
|
|
@@ -203,6 +204,12 @@ class StripAttributesTest < Minitest::Test
|
|
203
204
|
assert_equal "", record.bang
|
204
205
|
end
|
205
206
|
|
207
|
+
def test_should_skip_frozen_values
|
208
|
+
record = StripAllMockRecord.new(frozen: " ice ".freeze)
|
209
|
+
record.valid?
|
210
|
+
assert_equal " ice ", record.frozen
|
211
|
+
end
|
212
|
+
|
206
213
|
def test_should_collapse_duplicate_spaces
|
207
214
|
record = CollapseDuplicateSpaces.new(@init_params)
|
208
215
|
record.valid?
|
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.
|
4
|
+
version: 1.12.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryan McGeary
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-12-20 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: '8.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: '8.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.15'
|
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.15'
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: minitest
|
49
49
|
requirement: !ruby/object:Gem::Requirement
|
@@ -130,7 +130,7 @@ homepage: https://github.com/rmm5t/strip_attributes
|
|
130
130
|
licenses:
|
131
131
|
- MIT
|
132
132
|
metadata: {}
|
133
|
-
post_install_message:
|
133
|
+
post_install_message:
|
134
134
|
rdoc_options: []
|
135
135
|
require_paths:
|
136
136
|
- lib
|
@@ -145,8 +145,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
145
145
|
- !ruby/object:Gem::Version
|
146
146
|
version: '0'
|
147
147
|
requirements: []
|
148
|
-
rubygems_version: 3.
|
149
|
-
signing_key:
|
148
|
+
rubygems_version: 3.1.6
|
149
|
+
signing_key:
|
150
150
|
specification_version: 4
|
151
151
|
summary: Whitespace cleanup for ActiveModel attributes
|
152
152
|
test_files:
|