strip_attributes 1.3.0 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # StripAttributes [![Build Status](https://secure.travis-ci.org/rmm5t/strip_attributes.png)](http://travis-ci.org/rmm5t/strip_attributes)
1
+ # StripAttributes [![Gem Version](https://badge.fury.io/rb/strip_attributes.png)](http://badge.fury.io/rb/strip_attributes) [![Build Status](https://secure.travis-ci.org/rmm5t/strip_attributes.png)](http://travis-ci.org/rmm5t/strip_attributes)
2
2
 
3
3
  StripAttributes is an ActiveModel extension that automatically strips all
4
4
  attributes of leading and trailing whitespace before validation. If the
@@ -55,6 +55,15 @@ class BrokePokerPlayer < ActiveRecord::Base
55
55
  end
56
56
  ```
57
57
 
58
+ ### Using `collapse_spaces`
59
+
60
+ ```ruby
61
+ # Sequential spaces in attributes will be collapsed to one space
62
+ class EloquentPokerPlayer < ActiveRecord::Base
63
+ strip_attributes :collapse_spaces => true
64
+ end
65
+ ```
66
+
58
67
  ## Usage Patterns
59
68
 
60
69
  ### Other ORMs implementing `ActiveModel`
@@ -3,15 +3,10 @@ require "active_model"
3
3
  module ActiveModel::Validations::HelperMethods
4
4
  # Strips whitespace from model fields and converts blank values to nil.
5
5
  def strip_attributes(options = nil)
6
- before_validation do |record|
7
- allow_empty = options.delete(:allow_empty) if options
6
+ StripAttributes.validate_options(options)
8
7
 
9
- attributes = StripAttributes.narrow(record.attributes, options)
10
- attributes.each do |attr, value|
11
- if value.respond_to?(:strip)
12
- record[attr] = (value.blank? && !allow_empty) ? nil : value.strip
13
- end
14
- end
8
+ before_validation do |record|
9
+ StripAttributes.strip(record, options)
15
10
  end
16
11
  end
17
12
 
@@ -24,20 +19,47 @@ module ActiveModel::Validations::HelperMethods
24
19
  end
25
20
 
26
21
  module StripAttributes
22
+ VALID_OPTIONS = [:only, :except, :allow_empty, :collapse_spaces]
23
+
27
24
  # Necessary because Rails has removed the narrowing of attributes using :only
28
25
  # and :except on Base#attributes
29
- def self.narrow(attributes, options)
30
- if options.nil? || options.empty?
31
- attributes
26
+ def self.narrow(attributes, options = {})
27
+ if except = options && options[:except]
28
+ except = Array(except).collect { |attribute| attribute.to_s }
29
+ attributes.except(*except)
30
+ elsif only = options && options[:only]
31
+ only = Array(only).collect { |attribute| attribute.to_s }
32
+ attributes.slice(*only)
32
33
  else
33
- if except = options[:except]
34
- except = Array(except).collect { |attribute| attribute.to_s }
35
- attributes.except(*except)
36
- elsif only = options[:only]
37
- only = Array(only).collect { |attribute| attribute.to_s }
38
- attributes.slice(*only)
39
- else
40
- raise ArgumentError, "Options does not specify :except or :only (#{options.keys.inspect})"
34
+ attributes
35
+ end
36
+ end
37
+
38
+ def self.strip(record, options)
39
+ attributes = self.narrow(record.attributes, options)
40
+
41
+ if options
42
+ allow_empty = options[:allow_empty]
43
+ collapse_spaces = options[:collapse_spaces]
44
+ end
45
+
46
+ attributes.each do |attr, value|
47
+ if value.respond_to?(:strip)
48
+ value = (value.blank? && !allow_empty) ? nil : value.strip
49
+ end
50
+
51
+ if collapse_spaces && value.respond_to?(:squeeze!)
52
+ value.squeeze!(' ')
53
+ end
54
+
55
+ record[attr] = value
56
+ end
57
+ end
58
+
59
+ def self.validate_options(options)
60
+ if keys = options && options.keys
61
+ unless (keys - VALID_OPTIONS).empty?
62
+ raise ArgumentError, "Options does not specify #{VALID_OPTIONS} (#{options.keys.inspect})"
41
63
  end
42
64
  end
43
65
  end
@@ -1,3 +1,3 @@
1
1
  module StripAttributes
2
- VERSION = "1.3.0"
2
+ VERSION = "1.4.0"
3
3
  end
@@ -2,7 +2,7 @@ require "test_helper"
2
2
 
3
3
  module MockAttributes
4
4
  def self.included(base)
5
- base.attributes :foo, :bar, :biz, :baz, :bang
5
+ base.attributes :foo, :bar, :biz, :baz, :bang, :foz
6
6
  end
7
7
  end
8
8
 
@@ -36,10 +36,14 @@ class StripAllowEmpty < Tableless
36
36
  strip_attributes :allow_empty => true
37
37
  end
38
38
 
39
+ class CollapseDuplicateSpaces < Tableless
40
+ include MockAttributes
41
+ strip_attributes :collapse_spaces => true
42
+ end
39
43
 
40
44
  class StripAttributesTest < MiniTest::Unit::TestCase
41
45
  def setup
42
- @init_params = { :foo => "\tfoo", :bar => "bar \t ", :biz => "\tbiz ", :baz => "", :bang => " " }
46
+ @init_params = { :foo => "\tfoo", :bar => "bar \t ", :biz => "\tbiz ", :baz => "", :bang => " ", :foz => " foz foz" }
43
47
  end
44
48
 
45
49
  def test_should_exist
@@ -49,9 +53,10 @@ class StripAttributesTest < MiniTest::Unit::TestCase
49
53
  def test_should_strip_all_fields
50
54
  record = StripAllMockRecord.new(@init_params)
51
55
  record.valid?
52
- assert_equal "foo", record.foo
53
- assert_equal "bar", record.bar
54
- assert_equal "biz", record.biz
56
+ assert_equal "foo", record.foo
57
+ assert_equal "bar", record.bar
58
+ assert_equal "biz", record.biz
59
+ assert_equal "foz foz", record.foz
55
60
  assert_nil record.baz
56
61
  assert_nil record.bang
57
62
  end
@@ -59,29 +64,32 @@ class StripAttributesTest < MiniTest::Unit::TestCase
59
64
  def test_should_strip_only_one_field
60
65
  record = StripOnlyOneMockRecord.new(@init_params)
61
66
  record.valid?
62
- assert_equal "foo", record.foo
63
- assert_equal "bar \t ", record.bar
64
- assert_equal "\tbiz ", record.biz
65
- assert_equal "", record.baz
66
- assert_equal " ", record.bang
67
+ assert_equal "foo", record.foo
68
+ assert_equal "bar \t ", record.bar
69
+ assert_equal "\tbiz ", record.biz
70
+ assert_equal " foz foz", record.foz
71
+ assert_equal "", record.baz
72
+ assert_equal " ", record.bang
67
73
  end
68
74
 
69
75
  def test_should_strip_only_three_fields
70
76
  record = StripOnlyThreeMockRecord.new(@init_params)
71
77
  record.valid?
72
- assert_equal "foo", record.foo
73
- assert_equal "bar", record.bar
74
- assert_equal "biz", record.biz
75
- assert_equal "", record.baz
76
- assert_equal " ", record.bang
78
+ assert_equal "foo", record.foo
79
+ assert_equal "bar", record.bar
80
+ assert_equal "biz", record.biz
81
+ assert_equal " foz foz", record.foz
82
+ assert_equal "", record.baz
83
+ assert_equal " ", record.bang
77
84
  end
78
85
 
79
86
  def test_should_strip_all_except_one_field
80
87
  record = StripExceptOneMockRecord.new(@init_params)
81
88
  record.valid?
82
- assert_equal "\tfoo", record.foo
83
- assert_equal "bar", record.bar
84
- assert_equal "biz", record.biz
89
+ assert_equal "\tfoo", record.foo
90
+ assert_equal "bar", record.bar
91
+ assert_equal "biz", record.biz
92
+ assert_equal "foz foz", record.foz
85
93
  assert_nil record.baz
86
94
  assert_nil record.bang
87
95
  end
@@ -89,9 +97,10 @@ class StripAttributesTest < MiniTest::Unit::TestCase
89
97
  def test_should_strip_all_except_three_fields
90
98
  record = StripExceptThreeMockRecord.new(@init_params)
91
99
  record.valid?
92
- assert_equal "\tfoo", record.foo
93
- assert_equal "bar \t ", record.bar
94
- assert_equal "\tbiz ", record.biz
100
+ assert_equal "\tfoo", record.foo
101
+ assert_equal "bar \t ", record.bar
102
+ assert_equal "\tbiz ", record.biz
103
+ assert_equal "foz foz", record.foz
95
104
  assert_nil record.baz
96
105
  assert_nil record.bang
97
106
  end
@@ -99,10 +108,35 @@ class StripAttributesTest < MiniTest::Unit::TestCase
99
108
  def test_should_strip_and_allow_empty
100
109
  record = StripAllowEmpty.new(@init_params)
101
110
  record.valid?
102
- assert_equal "foo", record.foo
103
- assert_equal "bar", record.bar
104
- assert_equal "biz", record.biz
105
- assert_equal "", record.baz
106
- assert_equal "", record.bang
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 "", record.baz
116
+ assert_equal "", record.bang
117
+ end
118
+
119
+ def test_should_collapse_duplicate_spaces
120
+ record = CollapseDuplicateSpaces.new(@init_params)
121
+ record.valid?
122
+ assert_equal "foo", record.foo
123
+ assert_equal "bar", record.bar
124
+ assert_equal "biz", record.biz
125
+ assert_equal "foz foz", record.foz
126
+ assert_equal nil, record.baz
127
+ assert_equal nil, record.bang
128
+ end
129
+
130
+ def test_should_strip_and_allow_empty_always
131
+ record = StripAllowEmpty.new(@init_params)
132
+ record.valid?
133
+ record.assign_attributes(@init_params)
134
+ record.valid?
135
+ assert_equal "foo", record.foo
136
+ assert_equal "bar", record.bar
137
+ assert_equal "biz", record.biz
138
+ assert_equal "foz foz", record.foz
139
+ assert_equal "", record.baz
140
+ assert_equal "", record.bang
107
141
  end
108
142
  end
data/test/test_helper.rb CHANGED
@@ -21,6 +21,10 @@ class Tableless
21
21
  attrs
22
22
  end
23
23
 
24
+ def assign_attributes(attributes = {})
25
+ attributes.each { |name, value| send("#{name}=", value) }
26
+ end
27
+
24
28
  def [](name)
25
29
  send name
26
30
  end
metadata CHANGED
@@ -2,78 +2,78 @@
2
2
  name: strip_attributes
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 1.3.0
5
+ version: 1.4.0
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-02-06 00:00:00.000000000 Z
12
+ date: 2013-03-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
+ type: :runtime
15
16
  name: activemodel
16
17
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
18
  requirements:
19
19
  - - ~>
20
20
  - !ruby/object:Gem::Version
21
21
  version: '3.0'
22
- type: :runtime
23
- version_requirements: !ruby/object:Gem::Requirement
24
22
  none: false
23
+ version_requirements: !ruby/object:Gem::Requirement
25
24
  requirements:
26
25
  - - ~>
27
26
  - !ruby/object:Gem::Version
28
27
  version: '3.0'
28
+ none: false
29
29
  prerelease: false
30
30
  - !ruby/object:Gem::Dependency
31
+ type: :development
31
32
  name: minitest-matchers
32
33
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
34
  requirements:
35
35
  - - ~>
36
36
  - !ruby/object:Gem::Version
37
37
  version: '1.2'
38
- type: :development
39
- version_requirements: !ruby/object:Gem::Requirement
40
38
  none: false
39
+ version_requirements: !ruby/object:Gem::Requirement
41
40
  requirements:
42
41
  - - ~>
43
42
  - !ruby/object:Gem::Version
44
43
  version: '1.2'
44
+ none: false
45
45
  prerelease: false
46
46
  - !ruby/object:Gem::Dependency
47
+ type: :development
47
48
  name: activerecord
48
49
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
50
  requirements:
51
51
  - - ~>
52
52
  - !ruby/object:Gem::Version
53
53
  version: '3.0'
54
- type: :development
55
- version_requirements: !ruby/object:Gem::Requirement
56
54
  none: false
55
+ version_requirements: !ruby/object:Gem::Requirement
57
56
  requirements:
58
57
  - - ~>
59
58
  - !ruby/object:Gem::Version
60
59
  version: '3.0'
60
+ none: false
61
61
  prerelease: false
62
62
  - !ruby/object:Gem::Dependency
63
+ type: :development
63
64
  name: rake
64
65
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
66
  requirements:
67
67
  - - ~>
68
68
  - !ruby/object:Gem::Version
69
69
  version: '10.0'
70
- type: :development
71
- version_requirements: !ruby/object:Gem::Requirement
72
70
  none: false
71
+ version_requirements: !ruby/object:Gem::Requirement
73
72
  requirements:
74
73
  - - ~>
75
74
  - !ruby/object:Gem::Version
76
75
  version: '10.0'
76
+ none: false
77
77
  prerelease: false
78
78
  description: StripAttributes automatically strips all ActiveRecord model attributes
79
79
  of leading and trailing whitespace before validation. If the attribute is blank,
@@ -94,29 +94,30 @@ files:
94
94
  - test/test_helper.rb
95
95
  - README.md
96
96
  homepage: https://github.com/rmm5t/strip_attributes
97
- licenses: []
97
+ licenses:
98
+ - MIT
98
99
  post_install_message:
99
100
  rdoc_options: []
100
101
  require_paths:
101
102
  - lib
102
103
  required_ruby_version: !ruby/object:Gem::Requirement
103
- none: false
104
104
  requirements:
105
105
  - - ! '>='
106
106
  - !ruby/object:Gem::Version
107
+ hash: 1684897256707613928
107
108
  segments:
108
109
  - 0
109
- hash: -2456424087513438655
110
110
  version: '0'
111
- required_rubygems_version: !ruby/object:Gem::Requirement
112
111
  none: false
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
113
  requirements:
114
114
  - - ! '>='
115
115
  - !ruby/object:Gem::Version
116
+ hash: 1684897256707613928
116
117
  segments:
117
118
  - 0
118
- hash: -2456424087513438655
119
119
  version: '0'
120
+ none: false
120
121
  requirements: []
121
122
  rubyforge_project:
122
123
  rubygems_version: 1.8.23