strip_attributes 1.6.0 → 1.7.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 +10 -1
- data/lib/strip_attributes.rb +9 -4
- data/lib/strip_attributes/version.rb +1 -1
- data/test/strip_attributes_test.rb +86 -37
- data/test/test_helper.rb +7 -1
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6618accc8ca40df61327ca55b7791a33044db8ea
|
4
|
+
data.tar.gz: 1640362c209a8d7cfd674f43e3d622dcd1ca7682
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f185c24787def356267ba2fd9ecef1d74384fd43e679d3a3c204bd6fd540bde5b91421a8e7521137161181fa70f54e2486f96c0775e76f228f5b9dd4d22f0269
|
7
|
+
data.tar.gz: 6427387dac7327755d5f3035ae99ff85f985e8cefeff155836c298954189686644bee586026aa9b9638a61e84b7ae830c9f42ddb9b5e60a336276eacdbd1cae3
|
data/README.md
CHANGED
@@ -85,6 +85,15 @@ class EloquentPokerPlayer < ActiveRecord::Base
|
|
85
85
|
end
|
86
86
|
```
|
87
87
|
|
88
|
+
### Using `replace_newlines`
|
89
|
+
|
90
|
+
```ruby
|
91
|
+
# Newlines in attributes will be replaced with a space
|
92
|
+
class EloquentPokerPlayer < ActiveRecord::Base
|
93
|
+
strip_attributes :replace_newlines => true
|
94
|
+
end
|
95
|
+
```
|
96
|
+
|
88
97
|
### Using `regex`
|
89
98
|
|
90
99
|
```ruby
|
@@ -129,7 +138,7 @@ end
|
|
129
138
|
### Using it directly
|
130
139
|
|
131
140
|
```ruby
|
132
|
-
# where record is an ActiveModel
|
141
|
+
# where record is an ActiveModel instance
|
133
142
|
StripAttributes.strip(record, :collapse_spaces => true)
|
134
143
|
|
135
144
|
# works directly on Strings too
|
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, :regex]
|
22
|
+
VALID_OPTIONS = [:only, :except, :allow_empty, :collapse_spaces, :replace_newlines, :regex]
|
23
23
|
MULTIBYTE_SUPPORTED = "\u0020" == " "
|
24
24
|
|
25
25
|
def self.strip(record_or_string, options = nil)
|
@@ -44,9 +44,10 @@ module StripAttributes
|
|
44
44
|
|
45
45
|
def self.strip_string(value, options = nil)
|
46
46
|
if options
|
47
|
-
allow_empty
|
48
|
-
collapse_spaces
|
49
|
-
|
47
|
+
allow_empty = options[:allow_empty]
|
48
|
+
collapse_spaces = options[:collapse_spaces]
|
49
|
+
replace_newlines = options[:replace_newlines]
|
50
|
+
regex = options[:regex]
|
50
51
|
end
|
51
52
|
|
52
53
|
if value.respond_to?(:strip)
|
@@ -76,6 +77,10 @@ module StripAttributes
|
|
76
77
|
value.strip!
|
77
78
|
end
|
78
79
|
|
80
|
+
if replace_newlines && value.respond_to?(:gsub!)
|
81
|
+
value.gsub!(/[\r\n]+/, ' ')
|
82
|
+
end
|
83
|
+
|
79
84
|
if collapse_spaces && value.respond_to?(:squeeze!)
|
80
85
|
value.squeeze!(' ')
|
81
86
|
end
|
@@ -8,6 +8,7 @@ module MockAttributes
|
|
8
8
|
base.attribute :baz
|
9
9
|
base.attribute :bang
|
10
10
|
base.attribute :foz
|
11
|
+
base.attribute :fiz
|
11
12
|
end
|
12
13
|
end
|
13
14
|
|
@@ -46,6 +47,16 @@ class CollapseDuplicateSpaces < Tableless
|
|
46
47
|
strip_attributes :collapse_spaces => true
|
47
48
|
end
|
48
49
|
|
50
|
+
class ReplaceNewLines < Tableless
|
51
|
+
include MockAttributes
|
52
|
+
strip_attributes :replace_newlines => true
|
53
|
+
end
|
54
|
+
|
55
|
+
class ReplaceNewLinesAndDuplicateSpaces < Tableless
|
56
|
+
include MockAttributes
|
57
|
+
strip_attributes :replace_newlines => true, :collapse_spaces => true
|
58
|
+
end
|
59
|
+
|
49
60
|
class CoexistWithOtherValidations < Tableless
|
50
61
|
attribute :number, :type => Integer
|
51
62
|
|
@@ -63,7 +74,7 @@ end
|
|
63
74
|
|
64
75
|
class StripAttributesTest < Minitest::Test
|
65
76
|
def setup
|
66
|
-
@init_params = { :foo => "\tfoo", :bar => "bar \t ", :biz => "\tbiz ", :baz => "", :bang => " ", :foz => " foz foz" }
|
77
|
+
@init_params = { :foo => "\tfoo", :bar => "bar \t ", :biz => "\tbiz ", :baz => "", :bang => " ", :foz => " foz foz", :fiz => "fiz \n fiz" }
|
67
78
|
end
|
68
79
|
|
69
80
|
def test_should_exist
|
@@ -73,10 +84,11 @@ class StripAttributesTest < Minitest::Test
|
|
73
84
|
def test_should_strip_all_fields
|
74
85
|
record = StripAllMockRecord.new(@init_params)
|
75
86
|
record.valid?
|
76
|
-
assert_equal "foo",
|
77
|
-
assert_equal "bar",
|
78
|
-
assert_equal "biz",
|
79
|
-
assert_equal "foz foz",
|
87
|
+
assert_equal "foo", record.foo
|
88
|
+
assert_equal "bar", record.bar
|
89
|
+
assert_equal "biz", record.biz
|
90
|
+
assert_equal "foz foz", record.foz
|
91
|
+
assert_equal "fiz \n fiz", record.fiz
|
80
92
|
assert_nil record.baz
|
81
93
|
assert_nil record.bang
|
82
94
|
end
|
@@ -84,32 +96,35 @@ class StripAttributesTest < Minitest::Test
|
|
84
96
|
def test_should_strip_only_one_field
|
85
97
|
record = StripOnlyOneMockRecord.new(@init_params)
|
86
98
|
record.valid?
|
87
|
-
assert_equal "foo",
|
88
|
-
assert_equal "bar \t ",
|
89
|
-
assert_equal "\tbiz ",
|
90
|
-
assert_equal " foz foz",
|
91
|
-
assert_equal "",
|
92
|
-
assert_equal "
|
99
|
+
assert_equal "foo", record.foo
|
100
|
+
assert_equal "bar \t ", record.bar
|
101
|
+
assert_equal "\tbiz ", record.biz
|
102
|
+
assert_equal " foz foz", record.foz
|
103
|
+
assert_equal "fiz \n fiz", record.fiz
|
104
|
+
assert_equal "", record.baz
|
105
|
+
assert_equal " ", record.bang
|
93
106
|
end
|
94
107
|
|
95
108
|
def test_should_strip_only_three_fields
|
96
109
|
record = StripOnlyThreeMockRecord.new(@init_params)
|
97
110
|
record.valid?
|
98
|
-
assert_equal "foo",
|
99
|
-
assert_equal "bar",
|
100
|
-
assert_equal "biz",
|
101
|
-
assert_equal " foz foz",
|
102
|
-
assert_equal "",
|
103
|
-
assert_equal "
|
111
|
+
assert_equal "foo", record.foo
|
112
|
+
assert_equal "bar", record.bar
|
113
|
+
assert_equal "biz", record.biz
|
114
|
+
assert_equal " foz foz", record.foz
|
115
|
+
assert_equal "fiz \n fiz", record.fiz
|
116
|
+
assert_equal "", record.baz
|
117
|
+
assert_equal " ", record.bang
|
104
118
|
end
|
105
119
|
|
106
120
|
def test_should_strip_all_except_one_field
|
107
121
|
record = StripExceptOneMockRecord.new(@init_params)
|
108
122
|
record.valid?
|
109
|
-
assert_equal "\tfoo",
|
110
|
-
assert_equal "bar",
|
111
|
-
assert_equal "biz",
|
112
|
-
assert_equal "foz foz",
|
123
|
+
assert_equal "\tfoo", record.foo
|
124
|
+
assert_equal "bar", record.bar
|
125
|
+
assert_equal "biz", record.biz
|
126
|
+
assert_equal "foz foz", record.foz
|
127
|
+
assert_equal "fiz \n fiz", record.fiz
|
113
128
|
assert_nil record.baz
|
114
129
|
assert_nil record.bang
|
115
130
|
end
|
@@ -117,10 +132,11 @@ class StripAttributesTest < Minitest::Test
|
|
117
132
|
def test_should_strip_all_except_three_fields
|
118
133
|
record = StripExceptThreeMockRecord.new(@init_params)
|
119
134
|
record.valid?
|
120
|
-
assert_equal "\tfoo",
|
121
|
-
assert_equal "bar \t ",
|
122
|
-
assert_equal "\tbiz ",
|
123
|
-
assert_equal "foz foz",
|
135
|
+
assert_equal "\tfoo", record.foo
|
136
|
+
assert_equal "bar \t ", record.bar
|
137
|
+
assert_equal "\tbiz ", record.biz
|
138
|
+
assert_equal "foz foz", record.foz
|
139
|
+
assert_equal "fiz \n fiz", record.fiz
|
124
140
|
assert_nil record.baz
|
125
141
|
assert_nil record.bang
|
126
142
|
end
|
@@ -128,21 +144,47 @@ class StripAttributesTest < Minitest::Test
|
|
128
144
|
def test_should_strip_and_allow_empty
|
129
145
|
record = StripAllowEmpty.new(@init_params)
|
130
146
|
record.valid?
|
131
|
-
assert_equal "foo",
|
132
|
-
assert_equal "bar",
|
133
|
-
assert_equal "biz",
|
134
|
-
assert_equal "foz foz",
|
135
|
-
assert_equal "",
|
136
|
-
assert_equal "",
|
147
|
+
assert_equal "foo", record.foo
|
148
|
+
assert_equal "bar", record.bar
|
149
|
+
assert_equal "biz", record.biz
|
150
|
+
assert_equal "foz foz", record.foz
|
151
|
+
assert_equal "fiz \n fiz", record.fiz
|
152
|
+
assert_equal "", record.baz
|
153
|
+
assert_equal "", record.bang
|
137
154
|
end
|
138
155
|
|
139
156
|
def test_should_collapse_duplicate_spaces
|
140
157
|
record = CollapseDuplicateSpaces.new(@init_params)
|
141
158
|
record.valid?
|
159
|
+
assert_equal "foo", record.foo
|
160
|
+
assert_equal "bar", record.bar
|
161
|
+
assert_equal "biz", record.biz
|
162
|
+
assert_equal "foz foz", record.foz
|
163
|
+
assert_equal "fiz \n fiz", record.fiz
|
164
|
+
assert_equal nil, record.baz
|
165
|
+
assert_equal nil, record.bang
|
166
|
+
end
|
167
|
+
|
168
|
+
def test_should_replace_newlines
|
169
|
+
record = ReplaceNewLines.new(@init_params)
|
170
|
+
record.valid?
|
171
|
+
assert_equal "foo", record.foo
|
172
|
+
assert_equal "bar", record.bar
|
173
|
+
assert_equal "biz", record.biz
|
174
|
+
assert_equal "foz foz", record.foz
|
175
|
+
assert_equal "fiz fiz", record.fiz
|
176
|
+
assert_equal nil, record.baz
|
177
|
+
assert_equal nil, record.bang
|
178
|
+
end
|
179
|
+
|
180
|
+
def test_should_replace_newlines_and_duplicate_spaces
|
181
|
+
record = ReplaceNewLinesAndDuplicateSpaces.new(@init_params)
|
182
|
+
record.valid?
|
142
183
|
assert_equal "foo", record.foo
|
143
184
|
assert_equal "bar", record.bar
|
144
185
|
assert_equal "biz", record.biz
|
145
186
|
assert_equal "foz foz", record.foz
|
187
|
+
assert_equal "fiz fiz", record.fiz
|
146
188
|
assert_equal nil, record.baz
|
147
189
|
assert_equal nil, record.bang
|
148
190
|
end
|
@@ -152,12 +194,13 @@ class StripAttributesTest < Minitest::Test
|
|
152
194
|
record.valid?
|
153
195
|
record.assign_attributes(@init_params)
|
154
196
|
record.valid?
|
155
|
-
assert_equal "foo",
|
156
|
-
assert_equal "bar",
|
157
|
-
assert_equal "biz",
|
158
|
-
assert_equal "foz foz",
|
159
|
-
assert_equal "",
|
160
|
-
assert_equal "",
|
197
|
+
assert_equal "foo", record.foo
|
198
|
+
assert_equal "bar", record.bar
|
199
|
+
assert_equal "biz", record.biz
|
200
|
+
assert_equal "foz foz", record.foz
|
201
|
+
assert_equal "fiz \n fiz", record.fiz
|
202
|
+
assert_equal "", record.baz
|
203
|
+
assert_equal "", record.bang
|
161
204
|
end
|
162
205
|
|
163
206
|
def test_should_coexist_with_other_validations
|
@@ -209,6 +252,12 @@ class StripAttributesTest < Minitest::Test
|
|
209
252
|
assert_equal "1 2 3", StripAttributes.strip(" 1 2 3\t ", :collapse_spaces => true)
|
210
253
|
end
|
211
254
|
|
255
|
+
def test_should_replace_newlines
|
256
|
+
assert_equal "1 2", StripAttributes.strip("1\n2", :replace_newlines => true)
|
257
|
+
assert_equal "1 2", StripAttributes.strip("1\r\n2", :replace_newlines => true)
|
258
|
+
assert_equal "1 2", StripAttributes.strip("1\r2", :replace_newlines => true)
|
259
|
+
end
|
260
|
+
|
212
261
|
def test_should_strip_regex
|
213
262
|
assert_equal "abc", StripAttributes.strip("^%&*abc ^ ", :regex => /[\^\%&\*]/)
|
214
263
|
end
|
data/test/test_helper.rb
CHANGED
@@ -1,5 +1,11 @@
|
|
1
1
|
require "minitest/autorun"
|
2
|
-
require "minitest/
|
2
|
+
require "minitest/reporters"
|
3
|
+
if ENV["CI"] == "true"
|
4
|
+
Minitest::Reporters.use! Minitest::Reporters::DefaultReporter.new
|
5
|
+
else
|
6
|
+
Minitest::Reporters.use! Minitest::Reporters::ProgressReporter.new
|
7
|
+
end
|
8
|
+
|
3
9
|
require "active_attr"
|
4
10
|
require "strip_attributes"
|
5
11
|
|
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.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryan McGeary
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-02-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -78,6 +78,20 @@ dependencies:
|
|
78
78
|
- - "<"
|
79
79
|
- !ruby/object:Gem::Version
|
80
80
|
version: '6.0'
|
81
|
+
- !ruby/object:Gem::Dependency
|
82
|
+
name: minitest-reporters
|
83
|
+
requirement: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - ">="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: 0.14.24
|
88
|
+
type: :development
|
89
|
+
prerelease: false
|
90
|
+
version_requirements: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: 0.14.24
|
81
95
|
- !ruby/object:Gem::Dependency
|
82
96
|
name: rake
|
83
97
|
requirement: !ruby/object:Gem::Requirement
|