strip_attributes 1.4.2 → 1.4.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -134,7 +134,7 @@ end
134
134
 
135
135
  ```ruby
136
136
  describe User do
137
- it { should strip_attribute :name }
137
+ it { should strip_attribute(:name).collapse_spaces }
138
138
  it { should strip_attribute :email }
139
139
  it { should_not strip_attribute :password }
140
140
  end
@@ -144,7 +144,7 @@ end
144
144
 
145
145
  ```ruby
146
146
  class UserTest < ActiveSupport::TestCase
147
- should strip_attribute :name
147
+ should strip_attribute(:name).collapse_spaces
148
148
  should strip_attribute :email
149
149
  should_not strip_attribute :password
150
150
  end
@@ -156,7 +156,7 @@ end
156
156
  describe User do
157
157
  subject { User.new }
158
158
 
159
- must { strip_attribute :name }
159
+ must { strip_attribute(:name).collapse_spaces }
160
160
  must { strip_attribute :email }
161
161
  wont { strip_attribute :password }
162
162
  end
@@ -19,26 +19,48 @@ module StripAttributes
19
19
  class StripAttributeMatcher
20
20
  def initialize(attribute)
21
21
  @attribute = attribute
22
+ @options = {}
22
23
  end
23
24
 
24
25
  def matches?(subject)
25
26
  subject.send("#{@attribute}=", " string ")
26
27
  subject.valid?
27
- subject.send(@attribute) == "string"
28
+ subject.send(@attribute) == "string" and collapse_spaces?(subject)
28
29
  end
29
30
 
30
- def failure_message
31
- "Expected whitespace to be stripped from `#{@attribute}`, but it was not"
31
+ def collapse_spaces
32
+ @options[:collapse_spaces] = true
33
+ self
32
34
  end
33
35
 
34
- def negative_failure_message
35
- "Expected whitespace to remain on `#{@attribute}`, but it was stripped"
36
+ def failure_message_for_should
37
+ "Expected whitespace to be #{expectation} from ##{@attribute}, but it was not"
36
38
  end
39
+ alias_method :failure_message, :failure_message_for_should
40
+
41
+ def failure_message_for_should_not
42
+ "Expected whitespace to remain on ##{@attribute}, but it was #{expectation}"
43
+ end
44
+ alias_method :negative_failure_message, :failure_message_for_should_not
37
45
 
38
46
  def description
39
- "strip whitespace from ##{@attribute}"
47
+ "#{expectation(false)} whitespace from ##{@attribute}"
40
48
  end
41
- end
42
49
 
50
+ private
51
+
52
+ def collapse_spaces?(subject)
53
+ return true if !@options[:collapse_spaces]
54
+
55
+ subject.send("#{@attribute}=", " string string ")
56
+ subject.valid?
57
+ subject.send(@attribute) == "string string"
58
+ end
59
+
60
+ def expectation(past = true)
61
+ expectation = past ? "stripped" : "strip"
62
+ expectation += past ? " and collapsed" : " and collapse" if @options[:collapse_spaces]
63
+ end
64
+ end
43
65
  end
44
66
  end
@@ -1,3 +1,3 @@
1
1
  module StripAttributes
2
- VERSION = "1.4.2"
2
+ VERSION = "1.4.3"
3
3
  end
@@ -10,6 +10,10 @@ class SampleMockRecord < Tableless
10
10
  attribute :unstripped2
11
11
  attribute :unstripped3
12
12
  strip_attributes :only => [:stripped1, :stripped2, :stripped3]
13
+
14
+ attribute :collapsed
15
+ attribute :uncollapsed
16
+ strip_attributes :only => [:collapsed], :collapse_spaces => true
13
17
  end
14
18
 
15
19
  describe SampleMockRecord do
@@ -24,6 +28,9 @@ describe SampleMockRecord do
24
28
  wont { strip_attribute :unstripped2 }
25
29
  wont { strip_attribute :unstripped3 }
26
30
 
31
+ must { strip_attribute(:collapsed).collapse_spaces }
32
+ wont { strip_attribute(:uncollapsed).collapse_spaces }
33
+
27
34
  it "should fail when testing for strip on an unstripped attribute" do
28
35
  begin
29
36
  assert_must strip_attribute(:unstripped1)
@@ -41,4 +48,22 @@ describe SampleMockRecord do
41
48
  assert true
42
49
  end
43
50
  end
51
+
52
+ it "should fail when testing for collapse on an uncollapsed attribute" do
53
+ begin
54
+ assert_must collapse_attribute(:uncollapsed)
55
+ assert false
56
+ rescue
57
+ assert true
58
+ end
59
+ end
60
+
61
+ it "should fail when testing for no collapse on a collapsed attribute" do
62
+ begin
63
+ assert_wont collapse_attribute(:collapsed)
64
+ assert false
65
+ rescue
66
+ assert true
67
+ end
68
+ end
44
69
  end
metadata CHANGED
@@ -2,14 +2,14 @@
2
2
  name: strip_attributes
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 1.4.2
5
+ version: 1.4.3
6
6
  platform: ruby
7
7
  authors:
8
8
  - Ryan McGeary
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-24 00:00:00.000000000 Z
12
+ date: 2013-04-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activemodel
@@ -112,7 +112,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
112
112
  - !ruby/object:Gem::Version
113
113
  segments:
114
114
  - 0
115
- hash: 4359305486142774400
115
+ hash: -2985851846884596940
116
116
  version: '0'
117
117
  none: false
118
118
  required_rubygems_version: !ruby/object:Gem::Requirement
@@ -121,7 +121,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
121
121
  - !ruby/object:Gem::Version
122
122
  segments:
123
123
  - 0
124
- hash: 4359305486142774400
124
+ hash: -2985851846884596940
125
125
  version: '0'
126
126
  none: false
127
127
  requirements: []