strip_attributes 2.0.0 → 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 +4 -4
- data/README.md +5 -1
- data/bin/console +14 -0
- data/bin/rake +16 -0
- data/lib/strip_attributes/matchers.rb +29 -18
- data/lib/strip_attributes/version.rb +1 -1
- data/test/matchers_test.rb +32 -12
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6edf6d84e674dcc78436bec4732122630b57b187744c3e8b57b6eb2ffed615cb
|
4
|
+
data.tar.gz: ef1ef5a59f057bda5c4f6df19f8b867d225e80f8db646c4fdb9339e6eff56ff0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 915da1ef26816ff9d32886d44310ac77b927de8f0a49f67a76605d945779ec32a44ac30e0489e4ac9999757aea15dca2a738eeb69e03430a00fb1725cde2cc8c
|
7
|
+
data.tar.gz: f63d3b8dbc5555c787f0f4d5520f10f31f069ac5361dc67dacb4a237061370fc1e0d3e68bed868a3a261c14c4f2e5c340c6c91a2d3bc7a65c2b182e2b56300c5
|
data/README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# StripAttributes
|
2
2
|
|
3
3
|
[](https://rubygems.org/gems/strip_attributes)
|
4
|
-
[](https://github.com/rmm5t/strip_attributes/actions/workflows/ci.yml)
|
5
5
|
[](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
|
-
|
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}=", "
|
33
|
+
subject.send("#{@attribute}=", " #{value} ")
|
35
34
|
subject.valid?
|
36
|
-
subject.send(@attribute) ==
|
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
|
-
|
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
|
-
|
58
|
+
alias failure_message_for_should failure_message # RSpec 1.2, 2.x, and minitest-matchers
|
54
59
|
|
55
|
-
|
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
|
-
|
59
|
-
|
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
|
-
|
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
|
79
|
+
return true unless @options[:collapse_spaces]
|
69
80
|
|
70
|
-
subject.send("#{@attribute}=", "
|
81
|
+
subject.send("#{@attribute}=", " #{value} #{value} ")
|
71
82
|
subject.valid?
|
72
|
-
subject.send(@attribute) == "
|
83
|
+
subject.send(@attribute) == "#{value} #{value}"
|
73
84
|
end
|
74
85
|
|
75
|
-
def expectation(past
|
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"
|
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
|
94
|
+
return true unless @options[:replace_newlines]
|
84
95
|
|
85
|
-
subject.send("#{@attribute}=", "
|
96
|
+
subject.send("#{@attribute}=", "#{value}\n#{value}")
|
86
97
|
subject.valid?
|
87
|
-
subject.send(@attribute) == "
|
98
|
+
subject.send(@attribute) == "#{value} #{value}"
|
88
99
|
end
|
89
100
|
end
|
90
101
|
end
|
data/test/matchers_test.rb
CHANGED
@@ -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
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
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
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: strip_attributes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.
|
4
|
+
version: 2.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryan McGeary
|
8
8
|
bindir: bin
|
9
9
|
cert_chain: []
|
10
|
-
date:
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
11
11
|
dependencies:
|
12
12
|
- !ruby/object:Gem::Dependency
|
13
13
|
name: activemodel
|
@@ -111,6 +111,8 @@ files:
|
|
111
111
|
- LICENSE.txt
|
112
112
|
- README.md
|
113
113
|
- Rakefile
|
114
|
+
- bin/console
|
115
|
+
- bin/rake
|
114
116
|
- lib/strip_attributes.rb
|
115
117
|
- lib/strip_attributes/matchers.rb
|
116
118
|
- lib/strip_attributes/shoulda.rb
|
@@ -141,7 +143,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
141
143
|
- !ruby/object:Gem::Version
|
142
144
|
version: '0'
|
143
145
|
requirements: []
|
144
|
-
rubygems_version: 3.
|
146
|
+
rubygems_version: 3.7.1
|
145
147
|
specification_version: 4
|
146
148
|
summary: Whitespace cleanup for ActiveModel attributes
|
147
149
|
test_files:
|