strip_attributes 1.14.1 → 2.0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9d901257abbca144b30becb86cd248e512f8f8b140a6f822d3965f1896356d7c
4
- data.tar.gz: 798c083261f25f554767c216cfb399961e48e723a5d06355a12f82cb591eed45
3
+ metadata.gz: 6edf6d84e674dcc78436bec4732122630b57b187744c3e8b57b6eb2ffed615cb
4
+ data.tar.gz: ef1ef5a59f057bda5c4f6df19f8b867d225e80f8db646c4fdb9339e6eff56ff0
5
5
  SHA512:
6
- metadata.gz: 01a7956efa14fa00820e59171bbc1324d015a457db4224f1d5903d1c9ecce31b39d2cc9fb34c26a7e9ae2dcf868fc49034682d5d2f6a0bb532ab89c16898a938
7
- data.tar.gz: 41ac5050c3810cd7de390efff4564a6cec3b025fc90455ce3b6c084444821a3deeb4873155b4ee00cc86f9dfaa426a31f88d1aeaca8195ee0965c606ea56d04b
6
+ metadata.gz: 915da1ef26816ff9d32886d44310ac77b927de8f0a49f67a76605d945779ec32a44ac30e0489e4ac9999757aea15dca2a738eeb69e03430a00fb1725cde2cc8c
7
+ data.tar.gz: f63d3b8dbc5555c787f0f4d5520f10f31f069ac5361dc67dacb4a237061370fc1e0d3e68bed868a3a261c14c4f2e5c340c6c91a2d3bc7a65c2b182e2b56300c5
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://github.com/rmm5t/strip_attributes/workflows/CI/badge.svg?branch=master)](https://github.com/rmm5t/strip_attributes/actions?query=workflow%3ACI)
4
+ [![Build Status](https://github.com/rmm5t/strip_attributes/workflows/CI/badge.svg)](https://github.com/rmm5t/strip_attributes/actions/workflows/ci.yml)
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
@@ -236,6 +236,7 @@ describe User do
236
236
  it { is_expected.to strip_attribute(:name).replace_newlines }
237
237
  it { is_expected.to strip_attribute :email }
238
238
  it { is_expected.to strip_attributes(:name, :email) }
239
+ it { is_expected.to strip_attributes(:ticker).using("AAPL") }
239
240
  it { is_expected.not_to strip_attribute :password }
240
241
  it { is_expected.not_to strip_attributes(:password, :encrypted_password) }
241
242
  end
@@ -249,6 +250,7 @@ class UserTest < ActiveSupport::TestCase
249
250
  should strip_attribute(:name).replace_newlines
250
251
  should strip_attribute :email
251
252
  should strip_attributes(:name, :email)
253
+ should strip_attributes(:ticker).using("AAPL")
252
254
  should_not strip_attribute :password
253
255
  should_not strip_attributes(:password, :encrypted_password)
254
256
  end
@@ -265,6 +267,7 @@ describe User do
265
267
  must strip_attribute(:name).replace_newlines
266
268
  must strip_attribute :email
267
269
  must strip_attributes(:name, :email)
270
+ must strip_attributes(:ticker).using("AAPL")
268
271
  wont strip_attribute :password
269
272
  wont strip_attributes(:password, :encrypted_password)
270
273
  end
@@ -281,6 +284,7 @@ describe User do
281
284
  must { strip_attribute(:name).replace_newlines }
282
285
  must { strip_attribute :email }
283
286
  must { strip_attributes(:name, :email) }
287
+ must { strip_attributes(:ticker).using("AAPL") }
284
288
  wont { strip_attribute :password }
285
289
  wont { strip_attributes(:password, :encrypted_password) }
286
290
  end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "strip_attributes"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/rake ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'rake' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__)
12
+
13
+ require "rubygems"
14
+ require "bundler/setup"
15
+
16
+ load Gem.bin_path("rake", "rake")
@@ -1,6 +1,5 @@
1
1
  module StripAttributes
2
2
  module Matchers
3
-
4
3
  # Whitespace is stripped from the beginning and end of the attribute
5
4
  #
6
5
  # RSpec Examples:
@@ -20,7 +19,7 @@ module StripAttributes
20
19
  StripAttributeMatcher.new(attributes)
21
20
  end
22
21
 
23
- alias_method :strip_attributes, :strip_attribute
22
+ alias strip_attributes strip_attribute
24
23
 
25
24
  class StripAttributeMatcher
26
25
  def initialize(attributes)
@@ -31,12 +30,17 @@ module StripAttributes
31
30
  def matches?(subject)
32
31
  @attributes.all? do |attribute|
33
32
  @attribute = attribute
34
- subject.send("#{@attribute}=", " string ")
33
+ subject.send("#{@attribute}=", " #{value} ")
35
34
  subject.valid?
36
- subject.send(@attribute) == "string" and collapse_spaces?(subject) and replace_newlines?(subject)
35
+ subject.send(@attribute) == value and collapse_spaces?(subject) and replace_newlines?(subject)
37
36
  end
38
37
  end
39
38
 
39
+ def using(value)
40
+ @options[:value] = value
41
+ self
42
+ end
43
+
40
44
  def collapse_spaces
41
45
  @options[:collapse_spaces] = true
42
46
  self
@@ -47,44 +51,51 @@ module StripAttributes
47
51
  self
48
52
  end
49
53
 
50
- def failure_message # RSpec 3.x
54
+ # RSpec 3.x
55
+ def failure_message
51
56
  "Expected whitespace to be #{expectation} from ##{@attribute}, but it was not"
52
57
  end
53
- alias_method :failure_message_for_should, :failure_message # RSpec 1.2, 2.x, and minitest-matchers
58
+ alias failure_message_for_should failure_message # RSpec 1.2, 2.x, and minitest-matchers
54
59
 
55
- def failure_message_when_negated # RSpec 3.x
60
+ # RSpec 3.x
61
+ def failure_message_when_negated
56
62
  "Expected whitespace to remain on ##{@attribute}, but it was #{expectation}"
57
63
  end
58
- alias_method :failure_message_for_should_not, :failure_message_when_negated # RSpec 1.2, 2.x, and minitest-matchers
59
- alias_method :negative_failure_message, :failure_message_when_negated # RSpec 1.1
64
+ alias failure_message_for_should_not failure_message_when_negated # RSpec 1.2, 2.x, and minitest-matchers
65
+ alias negative_failure_message failure_message_when_negated # RSpec 1.1
60
66
 
61
67
  def description
62
- "#{expectation(false)} whitespace from #{@attributes.map {|el| "##{el}" }.to_sentence}"
68
+ attrs = @attributes.map { |attr| "##{attr}" }.to_sentence
69
+ "#{expectation(past: false)} whitespace from #{attrs}"
63
70
  end
64
71
 
65
72
  private
66
73
 
74
+ def value
75
+ @options[:value] || "string"
76
+ end
77
+
67
78
  def collapse_spaces?(subject)
68
- return true if !@options[:collapse_spaces]
79
+ return true unless @options[:collapse_spaces]
69
80
 
70
- subject.send("#{@attribute}=", " string string ")
81
+ subject.send("#{@attribute}=", " #{value} #{value} ")
71
82
  subject.valid?
72
- subject.send(@attribute) == "string string"
83
+ subject.send(@attribute) == "#{value} #{value}"
73
84
  end
74
85
 
75
- def expectation(past = true)
86
+ def expectation(past: true)
76
87
  expectation = past ? "stripped" : "strip"
77
88
  expectation += past ? " and collapsed" : " and collapse" if @options[:collapse_spaces]
78
- expectation += past ? " and replaced" : " and replace" if !@options[:replace_newlines]
89
+ expectation += past ? " and replaced" : " and replace" unless @options[:replace_newlines]
79
90
  expectation
80
91
  end
81
92
 
82
93
  def replace_newlines?(subject)
83
- return true if !@options[:replace_newlines]
94
+ return true unless @options[:replace_newlines]
84
95
 
85
- subject.send("#{@attribute}=", "string\nstring")
96
+ subject.send("#{@attribute}=", "#{value}\n#{value}")
86
97
  subject.valid?
87
- subject.send(@attribute) == "string string"
98
+ subject.send(@attribute) == "#{value} #{value}"
88
99
  end
89
100
  end
90
101
  end
@@ -1,3 +1,3 @@
1
1
  module StripAttributes
2
- VERSION = "1.14.1"
2
+ VERSION = "2.0.1".freeze
3
3
  end
@@ -27,8 +27,11 @@ module StripAttributes
27
27
  # U+FEFF ZERO WIDTH NO-BREAK SPACE
28
28
  MULTIBYTE_WHITE = "\u180E\u200B\u200C\u200D\u2060\uFEFF".freeze
29
29
  MULTIBYTE_SPACE = /[[:space:]#{MULTIBYTE_WHITE}]/.freeze
30
+ MULTIBYTE_SPACE_AT_ENDS = /\A#{MULTIBYTE_SPACE}+|#{MULTIBYTE_SPACE}+\z/.freeze
30
31
  MULTIBYTE_BLANK = /[[:blank:]#{MULTIBYTE_WHITE}]/.freeze
32
+ MULTIBYTE_BLANK_REPEATED = /#{MULTIBYTE_BLANK}+/.freeze
31
33
  MULTIBYTE_SUPPORTED = "\u0020" == " "
34
+ NEWLINES = /[\r\n]+/.freeze
32
35
 
33
36
  def self.strip(record_or_string, options = {})
34
37
  if record_or_string.respond_to?(:attributes)
@@ -52,26 +55,26 @@ module StripAttributes
52
55
 
53
56
  def self.strip_string(value, options = {})
54
57
  return value unless value.is_a?(String)
55
- return value if value.frozen?
56
58
 
57
59
  allow_empty = options[:allow_empty]
58
60
  collapse_spaces = options[:collapse_spaces]
59
61
  replace_newlines = options[:replace_newlines]
60
62
  regex = options[:regex]
61
63
 
64
+ value = value.dup
62
65
  value.gsub!(regex, "") if regex
63
66
 
64
67
  if MULTIBYTE_SUPPORTED && Encoding.compatible?(value, MULTIBYTE_SPACE)
65
- value.gsub!(/\A#{MULTIBYTE_SPACE}+|#{MULTIBYTE_SPACE}+\z/, "")
68
+ value.gsub!(MULTIBYTE_SPACE_AT_ENDS, "")
66
69
  else
67
70
  value.strip!
68
71
  end
69
72
 
70
- value.gsub!(/[\r\n]+/, " ") if replace_newlines
73
+ value.gsub!(NEWLINES, " ") if replace_newlines
71
74
 
72
75
  if collapse_spaces
73
76
  if MULTIBYTE_SUPPORTED && Encoding.compatible?(value, MULTIBYTE_BLANK)
74
- value.gsub!(/#{MULTIBYTE_BLANK}+/, " ")
77
+ value.gsub!(MULTIBYTE_BLANK_REPEATED, " ")
75
78
  else
76
79
  value.squeeze!(" ")
77
80
  end
@@ -22,6 +22,13 @@ class SampleMockRecord < Tableless
22
22
  attribute :replaceable
23
23
  attribute :unreplaceable
24
24
  strip_attributes only: [:replaceable], replace_newlines: true
25
+
26
+ attribute :upcased
27
+ strip_attributes only: [:upcased]
28
+
29
+ def upcased=(value)
30
+ super((value.upcase if value))
31
+ end
25
32
  end
26
33
 
27
34
  describe SampleMockRecord do
@@ -57,19 +64,32 @@ describe SampleMockRecord do
57
64
  it "should not replace on other attributes" do
58
65
  wont strip_attribute(:unreplaceable).replace_newlines
59
66
  end
67
+
68
+ it "should not strip normalized attributes missing a custom value" do
69
+ wont strip_attribute(:upcased)
70
+ wont strip_attribute(:upcased).using("fOO")
71
+ end
72
+
73
+ it "should strip normalized attributes using a custom value" do
74
+ must strip_attribute(:upcased).using("BIG")
75
+ end
60
76
  else
61
- must { strip_attribute :stripped1 }
62
- must { strip_attribute :stripped2 }
63
- must { strip_attribute :stripped3 }
64
- wont { strip_attribute :unstripped1 }
65
- wont { strip_attribute :unstripped2 }
66
- wont { strip_attribute :unstripped3 }
67
-
68
- must { strip_attribute(:collapsed).collapse_spaces }
69
- wont { strip_attribute(:uncollapsed).collapse_spaces }
70
-
71
- must { strip_attribute(:replaceable).replace_newlines }
72
- wont { strip_attribute(:unreplaceable).replace_newlines }
77
+ must { strip_attribute :stripped1 }
78
+ must { strip_attribute :stripped2 }
79
+ must { strip_attribute :stripped3 }
80
+ wont { strip_attribute :unstripped1 }
81
+ wont { strip_attribute :unstripped2 }
82
+ wont { strip_attribute :unstripped3 }
83
+
84
+ must { strip_attribute(:collapsed).collapse_spaces }
85
+ wont { strip_attribute(:uncollapsed).collapse_spaces }
86
+
87
+ must { strip_attribute(:replaceable).replace_newlines }
88
+ wont { strip_attribute(:unreplaceable).replace_newlines }
89
+
90
+ wont { strip_attribute(:upcased) }
91
+ wont { strip_attribute(:upcased).using("fOO") }
92
+ must { strip_attribute(:upcased).using("BIG") }
73
93
  end
74
94
 
75
95
  it "should fail when testing for strip on an unstripped attribute" do
@@ -204,10 +204,12 @@ class StripAttributesTest < Minitest::Test
204
204
  assert_equal "", record.bang
205
205
  end
206
206
 
207
- def test_should_skip_frozen_values
208
- record = StripAllMockRecord.new(frozen: " ice ".freeze)
207
+ def test_should_not_mutate_values
208
+ record = StripAllMockRecord.new(foo: " foo ")
209
+ old_value = record.foo
209
210
  record.valid?
210
- assert_equal " ice ", record.frozen
211
+ assert_equal "foo", record.foo
212
+ refute_equal old_value, record.foo
211
213
  end
212
214
 
213
215
  def test_should_collapse_duplicate_spaces
@@ -428,7 +430,7 @@ class StripAttributesTest < Minitest::Test
428
430
  skip "multi-byte characters not supported by this version of Ruby" unless StripAttributes::MULTIBYTE_SUPPORTED
429
431
 
430
432
  assert_equal "foo", StripAttributes.strip("\u200A\u200B foo\u200A\u200B ")
431
- assert_equal "foo\u20AC".force_encoding("ASCII-8BIT"), StripAttributes.strip("foo\u20AC ".force_encoding("ASCII-8BIT"))
433
+ assert_equal "foo\u20AC".b, StripAttributes.strip("foo\u20AC ".b)
432
434
  end
433
435
  end
434
436
  end
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: strip_attributes
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.14.1
4
+ version: 2.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan McGeary
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2024-12-10 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: activemodel
@@ -112,6 +111,8 @@ files:
112
111
  - LICENSE.txt
113
112
  - README.md
114
113
  - Rakefile
114
+ - bin/console
115
+ - bin/rake
115
116
  - lib/strip_attributes.rb
116
117
  - lib/strip_attributes/matchers.rb
117
118
  - lib/strip_attributes/shoulda.rb
@@ -128,7 +129,6 @@ metadata:
128
129
  changelog_uri: https://github.com/rmm5t/strip_attributes/blob/master/CHANGELOG.md
129
130
  source_code_uri: https://github.com/rmm5t/strip_attributes
130
131
  funding_uri: https://github.com/sponsors/rmm5t
131
- post_install_message:
132
132
  rdoc_options: []
133
133
  require_paths:
134
134
  - lib
@@ -143,8 +143,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
143
143
  - !ruby/object:Gem::Version
144
144
  version: '0'
145
145
  requirements: []
146
- rubygems_version: 3.5.23
147
- signing_key:
146
+ rubygems_version: 3.7.1
148
147
  specification_version: 4
149
148
  summary: Whitespace cleanup for ActiveModel attributes
150
149
  test_files: