attribute_normalizer 1.0.0.pre1 → 1.0.0.pre2

Sign up to get free protection for your applications and to get access to all the features.
data/README.textile CHANGED
@@ -68,6 +68,7 @@ p. The following normalizers are already included with the +0.3 version of the g
68
68
  * _:blank_ Will return _nil_ on empty strings
69
69
  * _:phone_ Will strip out all non-digit characters and return nil on empty strings
70
70
  * _:strip_ Will strip leading and trailing whitespace.
71
+ * _:squish_ Will strip leading and trailing whitespace and convert any consecutive spaces to one space each
71
72
 
72
73
  p. And lets predefine some normalizers that we may use in other classes/models or that we don't want to clutter up our class/model's readability with.
73
74
 
@@ -144,16 +145,16 @@ h3. Rails 2
144
145
  # ...
145
146
  Spec::Runner.configure do |config|
146
147
  # ...
147
- config.include AttributeNormalizer::RSpecMatcher, :type => :models
148
+ config.include AttributeNormalizer::RSpecMatcher, :type => :model
148
149
  end</code></pre>
149
150
 
150
151
  h3. Rails 3
151
152
 
152
153
  <pre><code># spec/spec_helper.rb
153
154
  # ...
154
- Spec::Runner.configure do |config|
155
+ RSpec.configure do |config|
155
156
  # ...
156
- config.include AttributeNormalizer::RSpecMatcher # Models group type is missing at this time rspec-rails 2.0.0.beta.11
157
+ config.include AttributeNormalizer::RSpecMatcher, :type => :model
157
158
  end</code></pre>
158
159
 
159
160
  p. _I will gladly take a patch to add a macro to Test::Unit if someone submits it._
@@ -55,7 +55,7 @@ module AttributeNormalizer
55
55
 
56
56
  def inherited(subclass)
57
57
  super
58
- subclass.normalize_default_attributes
58
+ subclass.normalize_default_attributes if subclass.table_exists?
59
59
  end
60
60
  end
61
61
  end
@@ -2,7 +2,7 @@ module AttributeNormalizer
2
2
  module Normalizers
3
3
  module BlankNormalizer
4
4
  def self.normalize(value, options)
5
- value.nil? || (value.is_a?(String) && value.empty?) ? nil : value
5
+ value.nil? || (value.is_a?(String) && value !~ /\S/) ? nil : value
6
6
  end
7
7
  end
8
8
  end
@@ -0,0 +1,9 @@
1
+ module AttributeNormalizer
2
+ module Normalizers
3
+ module SquishNormalizer
4
+ def self.normalize(value, options)
5
+ value.is_a?(String) ? value.strip.gsub(/\s+/, ' ') : value
6
+ end
7
+ end
8
+ end
9
+ end
@@ -21,6 +21,10 @@ module AttributeNormalizer
21
21
  "#{@attribute} did not normalize as expected! \"#{@subject.send(@attribute)}\" != #{@to.nil? ? 'nil' : "\"#{@to}\""}"
22
22
  end
23
23
 
24
+ def negative_failure_message
25
+ "expected #{@attribute} to not be normalized from #{@from.nil? ? 'nil' : "\"#{@from}\""} to #{@to.nil? ? 'nil' : "\"#{@to}\""}"
26
+ end
27
+
24
28
  def from(value)
25
29
  @from = value
26
30
  self
@@ -1,6 +1,7 @@
1
1
  require 'attribute_normalizer/normalizers/blank_normalizer'
2
2
  require 'attribute_normalizer/normalizers/phone_normalizer'
3
3
  require 'attribute_normalizer/normalizers/strip_normalizer'
4
+ require 'attribute_normalizer/normalizers/squish_normalizer'
4
5
 
5
6
  module AttributeNormalizer
6
7
 
@@ -41,6 +42,7 @@ module AttributeNormalizer
41
42
  @normalizers[ :blank ] = AttributeNormalizer::Normalizers::BlankNormalizer
42
43
  @normalizers[ :phone ] = AttributeNormalizer::Normalizers::PhoneNormalizer
43
44
  @normalizers[ :strip ] = AttributeNormalizer::Normalizers::StripNormalizer
45
+ @normalizers[ :squish ] = AttributeNormalizer::Normalizers::SquishNormalizer
44
46
  @default_normalizers = [ :strip, :blank ]
45
47
  @default_attributes = {}
46
48
  end
data/spec/author_spec.rb CHANGED
@@ -12,8 +12,12 @@ describe Author do
12
12
  it { should normalize_attribute(:first_name).from(' this ').to('this') }
13
13
  it { should normalize_attribute(:first_name).from(' ').to('') }
14
14
 
15
+ # :squish normalizer
16
+ it { should normalize_attribute(:nickname).from(' this nickname ').to('this nickname') }
17
+
15
18
  # :blank normalizer
16
19
  it { should normalize_attribute(:last_name).from('').to(nil) }
20
+ it { should normalize_attribute(:last_name).from(' ').to(nil) }
17
21
  it { should normalize_attribute(:last_name).from(' this ').to(' this ') }
18
22
 
19
23
  # :phone normalizer
@@ -7,6 +7,7 @@ ActiveRecord::Base.establish_connection({
7
7
  ActiveRecord::Schema.define do
8
8
  create_table :authors, :force => true do |t|
9
9
  t.string :name
10
+ t.string :nickname
10
11
  t.string :first_name
11
12
  t.string :last_name
12
13
  t.string :phone_number
@@ -1,6 +1,7 @@
1
1
  class Author < ActiveRecord::Base
2
2
 
3
3
  normalize_attribute :name
4
+ normalize_attribute :nickname, :with => :squish
4
5
  normalize_attribute :first_name, :with => :strip
5
6
  normalize_attribute :last_name, :with => :blank
6
7
  normalize_attribute :phone_number, :with => :phone
metadata CHANGED
@@ -1,13 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: attribute_normalizer
3
3
  version: !ruby/object:Gem::Version
4
+ hash: -1876988175
4
5
  prerelease: true
5
6
  segments:
6
7
  - 1
7
8
  - 0
8
9
  - 0
9
- - pre1
10
- version: 1.0.0.pre1
10
+ - pre2
11
+ version: 1.0.0.pre2
11
12
  platform: ruby
12
13
  authors:
13
14
  - Michael Deering
@@ -15,7 +16,7 @@ autorequire:
15
16
  bindir: bin
16
17
  cert_chain: []
17
18
 
18
- date: 2010-11-15 00:00:00 -06:00
19
+ date: 2011-01-16 00:00:00 -07:00
19
20
  default_executable:
20
21
  dependencies: []
21
22
 
@@ -37,6 +38,7 @@ files:
37
38
  - lib/attribute_normalizer/model_inclusions.rb
38
39
  - lib/attribute_normalizer/normalizers/blank_normalizer.rb
39
40
  - lib/attribute_normalizer/normalizers/phone_normalizer.rb
41
+ - lib/attribute_normalizer/normalizers/squish_normalizer.rb
40
42
  - lib/attribute_normalizer/normalizers/strip_normalizer.rb
41
43
  - lib/attribute_normalizer/rspec_matcher.rb
42
44
  - rails/init.rb
@@ -67,6 +69,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
67
69
  requirements:
68
70
  - - ">="
69
71
  - !ruby/object:Gem::Version
72
+ hash: 3
70
73
  segments:
71
74
  - 0
72
75
  version: "0"
@@ -75,6 +78,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
75
78
  requirements:
76
79
  - - ">"
77
80
  - !ruby/object:Gem::Version
81
+ hash: 25
78
82
  segments:
79
83
  - 1
80
84
  - 3