strip_attributes 1.4.4 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ff3a0ea7a0ad2399cfe77252be06860ed86f84fb
4
- data.tar.gz: eb2d6ea7b6375258381f914368385d0a6e015260
3
+ metadata.gz: e3e651d0d39ce72ba89f17e690a0dac548c37ff0
4
+ data.tar.gz: c8d3915d4cb62080de4ce629af76ef8a4974672a
5
5
  SHA512:
6
- metadata.gz: 449748943c98701d704edcf2333aced1a1d9850eaba9c97acf15948fb26f55ffa4979744080f487334f8de49304511a570ef8f5ee22c498c93868eb91a2e379b
7
- data.tar.gz: ca85da74e5400e8ed4e0397c43d827ccedff7e0ead9ba67c7af66eb1f096db50a50602ae5c88b233767e613da3712d76bf6d38897278d7371681ad163c7ac642
6
+ metadata.gz: 869369a4ed6c794974d87ce80d95f7835ce2714e9391e2a79b1af5dd8a3cca45c1b640f7e1e4ab632b999bf5b4fbec14698c8b2d232cdd0b4af8550c625f5923
7
+ data.tar.gz: 36ee4132a596ca23bc7ac451b0fa71176e1c9cf452121d8c7d48a0162bf0e9400c402b9571c4cdd7672e6237191dc2ab1b885fac93847543b9101d747efca4d0
data/README.md CHANGED
@@ -20,7 +20,7 @@ a single attribute (`:only => :field`) or arrays of attributes (`:except =>
20
20
  Include the gem in your Gemfile:
21
21
 
22
22
  ```ruby
23
- gem "strip_attributes", "~> 1.2"
23
+ gem "strip_attributes"
24
24
  ```
25
25
 
26
26
  ## Examples
@@ -69,6 +69,17 @@ class EloquentPokerPlayer < ActiveRecord::Base
69
69
  end
70
70
  ```
71
71
 
72
+ ### Using `regex`
73
+
74
+ ```ruby
75
+ class User < ActiveRecord::Base
76
+ # Strip off characters defined by RegEx
77
+ strip_attributes :only => [:first_name, :last_name], :regex => /[^[:alpha:]\s]/
78
+ # Strip off non-integers
79
+ strip_attributes :only => [:phone], :regex => /[^0-9]/
80
+ end
81
+ ```
82
+
72
83
  ## Usage Patterns
73
84
 
74
85
  ### Other ORMs implementing `ActiveModel`
@@ -19,7 +19,8 @@ module ActiveModel::Validations::HelperMethods
19
19
  end
20
20
 
21
21
  module StripAttributes
22
- VALID_OPTIONS = [:only, :except, :allow_empty, :collapse_spaces]
22
+ VALID_OPTIONS = [:only, :except, :allow_empty, :collapse_spaces, :regex]
23
+ MULTIBYTE_SUPPORTED = "\u0020" == " "
23
24
 
24
25
  # Necessary because Rails has removed the narrowing of attributes using :only
25
26
  # and :except on Base#attributes
@@ -41,6 +42,7 @@ module StripAttributes
41
42
  if options
42
43
  allow_empty = options[:allow_empty]
43
44
  collapse_spaces = options[:collapse_spaces]
45
+ regex = options[:regex]
44
46
  end
45
47
 
46
48
  attributes.each do |attr, value|
@@ -50,6 +52,27 @@ module StripAttributes
50
52
  value = (value.blank? && !allow_empty) ? nil : value.strip
51
53
  end
52
54
 
55
+ if regex && value.respond_to?(:gsub!)
56
+ value.gsub!(regex, '')
57
+ end
58
+
59
+ if MULTIBYTE_SUPPORTED
60
+ # Remove leading and trailing Unicode invisible and whitespace characters.
61
+ # The POSIX character class [:space:] corresponds to the Unicode class Z
62
+ # ("separator"). We also include the following characters from Unicode class
63
+ # C ("control"), which are spaces or invisible characters that make no
64
+ # sense at the start or end of a string:
65
+ # U+180E MONGOLIAN VOWEL SEPARATOR
66
+ # U+200B ZERO WIDTH SPACE
67
+ # U+200C ZERO WIDTH NON-JOINER
68
+ # U+200D ZERO WIDTH JOINER
69
+ # U+2060 WORD JOINER
70
+ # U+FEFF ZERO WIDTH NO-BREAK SPACE
71
+ if value.respond_to?(:gsub!)
72
+ value.gsub!(/\A[[:space:]\u180E\u200B\u200C\u200D\u2060\uFEFF]+|[[:space:]\u180E\u200B\u200C\u200D\u2060\uFEFF]+\z/, '')
73
+ end
74
+ end
75
+
53
76
  if collapse_spaces && value.respond_to?(:squeeze!)
54
77
  value.squeeze!(' ')
55
78
  end
@@ -1,3 +1,3 @@
1
1
  module StripAttributes
2
- VERSION = "1.4.4"
2
+ VERSION = "1.5.0"
3
3
  end
@@ -56,6 +56,11 @@ class CoexistWithOtherValidations < Tableless
56
56
  }
57
57
  end
58
58
 
59
+ class StripRegexMockRecord < Tableless
60
+ include MockAttributes
61
+ strip_attributes :regex => /[\^\%&\*]/
62
+ end
63
+
59
64
  class StripAttributesTest < MiniTest::Unit::TestCase
60
65
  def setup
61
66
  @init_params = { :foo => "\tfoo", :bar => "bar \t ", :biz => "\tbiz ", :baz => "", :bang => " ", :foz => " foz foz" }
@@ -169,4 +174,22 @@ class StripAttributesTest < MiniTest::Unit::TestCase
169
174
  # assert record.valid?, "Expected record to be valid, but got #{record.errors.full_messages}"
170
175
  # assert !record.errors.include?(:number), "Expected record to have no errors on :number"
171
176
  end
177
+
178
+ def test_should_strip_regex
179
+ record = StripRegexMockRecord.new
180
+ record.assign_attributes(@init_params.merge(:foo => "^%&*abc "))
181
+ record.valid?
182
+ assert_equal "abc", record.foo
183
+ assert_equal "bar", record.bar
184
+ end
185
+
186
+ def test_strip_unicode
187
+ # This feature only works if multi-byte characters are supported by Ruby
188
+ return if "\u0020" != " "
189
+ # U200A - HAIR SPACE
190
+ # U200B - ZERO WIDTH SPACE
191
+ record = StripOnlyOneMockRecord.new({:foo => "\u200A\u200B foo\u200A\u200B "})
192
+ record.valid?
193
+ assert_equal "foo", record.foo
194
+ end
172
195
  end
metadata CHANGED
@@ -1,89 +1,89 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: strip_attributes
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.4
4
+ version: 1.5.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: 2013-07-18 00:00:00.000000000 Z
11
+ date: 2014-01-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '3.0'
20
- - - <
20
+ - - "<"
21
21
  - !ruby/object:Gem::Version
22
22
  version: '5.0'
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
26
26
  requirements:
27
- - - '>='
27
+ - - ">="
28
28
  - !ruby/object:Gem::Version
29
29
  version: '3.0'
30
- - - <
30
+ - - "<"
31
31
  - !ruby/object:Gem::Version
32
32
  version: '5.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
39
  version: '0.7'
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
46
  version: '0.7'
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: minitest-matchers
49
49
  requirement: !ruby/object:Gem::Requirement
50
50
  requirements:
51
- - - ~>
51
+ - - "~>"
52
52
  - !ruby/object:Gem::Version
53
53
  version: '1.2'
54
54
  type: :development
55
55
  prerelease: false
56
56
  version_requirements: !ruby/object:Gem::Requirement
57
57
  requirements:
58
- - - ~>
58
+ - - "~>"
59
59
  - !ruby/object:Gem::Version
60
60
  version: '1.2'
61
61
  - !ruby/object:Gem::Dependency
62
62
  name: minitest
63
63
  requirement: !ruby/object:Gem::Requirement
64
64
  requirements:
65
- - - ~>
65
+ - - "~>"
66
66
  - !ruby/object:Gem::Version
67
67
  version: '4.7'
68
68
  type: :development
69
69
  prerelease: false
70
70
  version_requirements: !ruby/object:Gem::Requirement
71
71
  requirements:
72
- - - ~>
72
+ - - "~>"
73
73
  - !ruby/object:Gem::Version
74
74
  version: '4.7'
75
75
  - !ruby/object:Gem::Dependency
76
76
  name: rake
77
77
  requirement: !ruby/object:Gem::Requirement
78
78
  requirements:
79
- - - ~>
79
+ - - "~>"
80
80
  - !ruby/object:Gem::Version
81
81
  version: '10.0'
82
82
  type: :development
83
83
  prerelease: false
84
84
  version_requirements: !ruby/object:Gem::Requirement
85
85
  requirements:
86
- - - ~>
86
+ - - "~>"
87
87
  - !ruby/object:Gem::Version
88
88
  version: '10.0'
89
89
  description: StripAttributes automatically strips all ActiveRecord model attributes
@@ -95,15 +95,15 @@ executables: []
95
95
  extensions: []
96
96
  extra_rdoc_files: []
97
97
  files:
98
+ - README.md
99
+ - lib/strip_attributes.rb
98
100
  - lib/strip_attributes/matchers.rb
99
- - lib/strip_attributes/shoulda/macros.rb
100
101
  - lib/strip_attributes/shoulda.rb
102
+ - lib/strip_attributes/shoulda/macros.rb
101
103
  - lib/strip_attributes/version.rb
102
- - lib/strip_attributes.rb
103
104
  - test/matchers_test.rb
104
105
  - test/strip_attributes_test.rb
105
106
  - test/test_helper.rb
106
- - README.md
107
107
  homepage: https://github.com/rmm5t/strip_attributes
108
108
  licenses:
109
109
  - MIT
@@ -114,17 +114,17 @@ require_paths:
114
114
  - lib
115
115
  required_ruby_version: !ruby/object:Gem::Requirement
116
116
  requirements:
117
- - - '>='
117
+ - - ">="
118
118
  - !ruby/object:Gem::Version
119
119
  version: '0'
120
120
  required_rubygems_version: !ruby/object:Gem::Requirement
121
121
  requirements:
122
- - - '>='
122
+ - - ">="
123
123
  - !ruby/object:Gem::Version
124
124
  version: '0'
125
125
  requirements: []
126
126
  rubyforge_project:
127
- rubygems_version: 2.0.3
127
+ rubygems_version: 2.2.0
128
128
  signing_key:
129
129
  specification_version: 4
130
130
  summary: Whitespace cleanup for ActiveModel attributes