attribute_normalizer 1.1.0 → 1.2.0.a
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 +15 -0
- data/README.textile +4 -2
- data/lib/attribute_normalizer.rb +8 -25
- data/lib/attribute_normalizer/model_inclusions.rb +18 -14
- data/lib/attribute_normalizer/version.rb +16 -0
- metadata +20 -51
- data/MIT-LICENSE +0 -20
- data/Rakefile +0 -12
- data/install.rb +0 -1
- data/install.txt +0 -12
- data/rails/init.rb +0 -1
- data/spec/article_spec.rb +0 -49
- data/spec/attribute_normalizer_spec.rb +0 -26
- data/spec/author_spec.rb +0 -29
- data/spec/book_spec.rb +0 -52
- data/spec/connection_and_schema.rb +0 -35
- data/spec/journal_spec.rb +0 -12
- data/spec/models/article.rb +0 -11
- data/spec/models/author.rb +0 -9
- data/spec/models/book.rb +0 -13
- data/spec/models/journal.rb +0 -2
- data/spec/test_helper.rb +0 -49
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
MzI3ODAzNTM2ZDcwZDg1NWQxYWI5OGI4YzFiM2Y0YjMyMTk1MDg0MA==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
OWE0MGFiZWY4NDA1ZmY3ZWY1MjlhMzM0MDkwNWJhZGJhNzJjNjc2OQ==
|
7
|
+
!binary "U0hBNTEy":
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
ODc4YTc2MThlNjZlZWU4YWJkNmU5NTkxYzk5NmE3MzZkM2IyY2VjZWQ1NzYz
|
10
|
+
NmRmZWY3YjhmYzQ5YjJjMzE5YzJkZjI1MTRmN2VmMDcwYzgyNjZlNTMyMjM5
|
11
|
+
N2IxZGI4ZWU2NmU3YTRjNjEzMTFkNWExOGRlZDQzZmVkYTk0Mzk=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
MWZkZWRjMTQyODEyOGYyMzZhZTA4MzQ2NTViNjcwZjY4MjA3MWQxZGNmNTgw
|
14
|
+
YjY1ZmRmN2IzZmFiNDEyZDgwNTJkMzkzNDE4YTNmOWMyZDkyNDE5YmFjYjgw
|
15
|
+
YzBmYjJkZDhiMWI3Y2I4MzhkOWEyMjM5YTk5YzhhOGE4MTc1ODI=
|
data/README.textile
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
h1. Attribute Normalizer
|
2
2
|
|
3
|
+
!https://secure.travis-ci.org/mdeering/attribute_normalizer.png?branch=master(Build Status)!:http://travis-ci.org/mdeering/attribute_normalizer
|
4
|
+
|
3
5
|
p. A little normalization goes a long way in helping maintain data integrity.
|
4
6
|
|
5
7
|
h2. Change History
|
@@ -167,14 +169,14 @@ p. If you are running RSpec there is matcher available for use. Usage can been
|
|
167
169
|
h3. Rails 2
|
168
170
|
|
169
171
|
<pre><code># spec/spec_helper.rb
|
170
|
-
|
172
|
+
RSpec.configure do |config|
|
171
173
|
config.include AttributeNormalizer::RSpecMatcher, :type => :models
|
172
174
|
end</code></pre>
|
173
175
|
|
174
176
|
h3. Rails 3
|
175
177
|
|
176
178
|
<pre><code># spec/spec_helper.rb
|
177
|
-
|
179
|
+
RSpec.configure do |config|
|
178
180
|
config.include AttributeNormalizer::RSpecMatcher, :type => :model
|
179
181
|
end</code></pre>
|
180
182
|
|
data/lib/attribute_normalizer.rb
CHANGED
@@ -27,26 +27,18 @@ module AttributeNormalizer
|
|
27
27
|
@default_normalizers = normalizers.is_a?(Array) ? normalizers : [ normalizers ]
|
28
28
|
end
|
29
29
|
|
30
|
-
def
|
31
|
-
[attributes].flatten.each do |attribute|
|
32
|
-
add_default_attribute(attribute, :with => default_normalizers)
|
33
|
-
end
|
34
|
-
end
|
30
|
+
def initialize
|
35
31
|
|
36
|
-
|
37
|
-
|
38
|
-
|
32
|
+
@normalizers = {
|
33
|
+
:blank => AttributeNormalizer::Normalizers::BlankNormalizer,
|
34
|
+
:phone => AttributeNormalizer::Normalizers::PhoneNormalizer,
|
35
|
+
:squish => AttributeNormalizer::Normalizers::SquishNormalizer,
|
36
|
+
:strip => AttributeNormalizer::Normalizers::StripNormalizer
|
37
|
+
}
|
39
38
|
|
40
|
-
def initialize
|
41
|
-
@normalizers = {}
|
42
|
-
@normalizers[ :blank ] = AttributeNormalizer::Normalizers::BlankNormalizer
|
43
|
-
@normalizers[ :phone ] = AttributeNormalizer::Normalizers::PhoneNormalizer
|
44
|
-
@normalizers[ :strip ] = AttributeNormalizer::Normalizers::StripNormalizer
|
45
|
-
@normalizers[ :squish ] = AttributeNormalizer::Normalizers::SquishNormalizer
|
46
39
|
@default_normalizers = [ :strip, :blank ]
|
47
|
-
@default_attributes = {}
|
48
|
-
end
|
49
40
|
|
41
|
+
end
|
50
42
|
|
51
43
|
end
|
52
44
|
|
@@ -64,15 +56,6 @@ def include_attribute_normalizer(class_or_module)
|
|
64
56
|
end
|
65
57
|
|
66
58
|
|
67
|
-
|
68
59
|
include_attribute_normalizer(ActiveModel::Base) if defined?(ActiveModel::Base)
|
69
60
|
include_attribute_normalizer(ActiveRecord::Base) if defined?(ActiveRecord::Base)
|
70
61
|
include_attribute_normalizer(CassandraObject::Base) if defined?(CassandraObject::Base)
|
71
|
-
|
72
|
-
if defined?(Mongoid::Document)
|
73
|
-
Mongoid::Document.class_eval do
|
74
|
-
included do
|
75
|
-
include AttributeNormalizer
|
76
|
-
end
|
77
|
-
end
|
78
|
-
end
|
@@ -5,12 +5,13 @@ module AttributeNormalizer
|
|
5
5
|
end
|
6
6
|
|
7
7
|
module ClassMethods
|
8
|
+
|
8
9
|
def normalize_attributes(*attributes, &block)
|
9
10
|
options = attributes.last.is_a?(::Hash) ? attributes.pop : {}
|
10
11
|
|
11
|
-
normalizers = [ options
|
12
|
-
normalizers = [ options
|
13
|
-
post_normalizers = [ options
|
12
|
+
normalizers = [ options[:with] ].flatten.compact
|
13
|
+
normalizers = [ options[:before] ].flatten.compact if block_given? && normalizers.empty?
|
14
|
+
post_normalizers = [ options[:after] ].flatten.compact if block_given?
|
14
15
|
|
15
16
|
if normalizers.empty? && !block_given?
|
16
17
|
normalizers = AttributeNormalizer.configuration.default_normalizers # the default normalizers
|
@@ -49,19 +50,20 @@ module AttributeNormalizer
|
|
49
50
|
|
50
51
|
if method_defined?(:"#{attribute}=")
|
51
52
|
alias_method "old_#{attribute}=", "#{attribute}="
|
52
|
-
end
|
53
53
|
|
54
|
-
|
55
|
-
begin
|
56
|
-
super(self.send(:"normalize_#{attribute}", value))
|
57
|
-
rescue NoMethodError
|
54
|
+
define_method "#{attribute}=" do |value|
|
58
55
|
normalized_value = self.send(:"normalize_#{attribute}", value)
|
59
56
|
self.send("old_#{attribute}=", normalized_value)
|
60
57
|
end
|
58
|
+
else
|
59
|
+
define_method "#{attribute}=" do |value|
|
60
|
+
super(self.send(:"normalize_#{attribute}", value))
|
61
|
+
end
|
61
62
|
end
|
62
63
|
|
63
64
|
end
|
64
65
|
end
|
66
|
+
|
65
67
|
alias :normalize_attribute :normalize_attributes
|
66
68
|
|
67
69
|
def normalize_default_attributes
|
@@ -70,11 +72,13 @@ module AttributeNormalizer
|
|
70
72
|
end
|
71
73
|
end
|
72
74
|
|
73
|
-
def inherited(subclass)
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
end
|
75
|
+
# def inherited(subclass)
|
76
|
+
# super
|
77
|
+
# if subclass.name.present? && subclass.respond_to?(:table_exists?) && (subclass.table_exists? rescue false)
|
78
|
+
# subclass.normalize_default_attributes
|
79
|
+
# end
|
80
|
+
# end
|
81
|
+
|
79
82
|
end
|
83
|
+
|
80
84
|
end
|
metadata
CHANGED
@@ -1,84 +1,53 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: attribute_normalizer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
5
|
-
prerelease:
|
4
|
+
version: 1.2.0.a
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Michael Deering
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2013-08-09 00:00:00.000000000 Z
|
13
12
|
dependencies: []
|
14
|
-
description:
|
15
|
-
email:
|
13
|
+
description: ''
|
14
|
+
email:
|
15
|
+
- mdeering@mdeering.com
|
16
16
|
executables: []
|
17
17
|
extensions: []
|
18
|
-
extra_rdoc_files:
|
19
|
-
- README.textile
|
18
|
+
extra_rdoc_files: []
|
20
19
|
files:
|
21
|
-
- MIT-LICENSE
|
22
|
-
- README.textile
|
23
|
-
- Rakefile
|
24
|
-
- install.rb
|
25
|
-
- install.txt
|
26
|
-
- lib/attribute_normalizer.rb
|
27
20
|
- lib/attribute_normalizer/model_inclusions.rb
|
28
|
-
- lib/attribute_normalizer/
|
21
|
+
- lib/attribute_normalizer/rspec_matcher.rb
|
22
|
+
- lib/attribute_normalizer/normalizers/strip_normalizer.rb
|
29
23
|
- lib/attribute_normalizer/normalizers/phone_normalizer.rb
|
24
|
+
- lib/attribute_normalizer/normalizers/blank_normalizer.rb
|
30
25
|
- lib/attribute_normalizer/normalizers/squish_normalizer.rb
|
31
|
-
- lib/attribute_normalizer/
|
32
|
-
- lib/attribute_normalizer
|
33
|
-
-
|
34
|
-
|
35
|
-
- spec/attribute_normalizer_spec.rb
|
36
|
-
- spec/author_spec.rb
|
37
|
-
- spec/book_spec.rb
|
38
|
-
- spec/connection_and_schema.rb
|
39
|
-
- spec/journal_spec.rb
|
40
|
-
- spec/models/article.rb
|
41
|
-
- spec/models/author.rb
|
42
|
-
- spec/models/book.rb
|
43
|
-
- spec/models/journal.rb
|
44
|
-
- spec/test_helper.rb
|
45
|
-
homepage: http://github.com/mdeering/attribute_normalizer
|
26
|
+
- lib/attribute_normalizer/version.rb
|
27
|
+
- lib/attribute_normalizer.rb
|
28
|
+
- README.textile
|
29
|
+
homepage: https://github.com/mdeering/attribute_normalizer
|
46
30
|
licenses: []
|
31
|
+
metadata: {}
|
47
32
|
post_install_message:
|
48
|
-
rdoc_options:
|
49
|
-
- --charset=UTF-8
|
33
|
+
rdoc_options: []
|
50
34
|
require_paths:
|
51
35
|
- lib
|
52
36
|
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
-
none: false
|
54
37
|
requirements:
|
55
38
|
- - ! '>='
|
56
39
|
- !ruby/object:Gem::Version
|
57
40
|
version: '0'
|
58
|
-
segments:
|
59
|
-
- 0
|
60
|
-
hash: -3251542831906071239
|
61
41
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
-
none: false
|
63
42
|
requirements:
|
64
43
|
- - ! '>'
|
65
44
|
- !ruby/object:Gem::Version
|
66
45
|
version: 1.3.1
|
67
46
|
requirements: []
|
68
47
|
rubyforge_project:
|
69
|
-
rubygems_version:
|
48
|
+
rubygems_version: 2.0.5
|
70
49
|
signing_key:
|
71
|
-
specification_version:
|
72
|
-
summary:
|
73
|
-
test_files:
|
74
|
-
|
75
|
-
- spec/attribute_normalizer_spec.rb
|
76
|
-
- spec/author_spec.rb
|
77
|
-
- spec/book_spec.rb
|
78
|
-
- spec/connection_and_schema.rb
|
79
|
-
- spec/journal_spec.rb
|
80
|
-
- spec/models/article.rb
|
81
|
-
- spec/models/author.rb
|
82
|
-
- spec/models/book.rb
|
83
|
-
- spec/models/journal.rb
|
84
|
-
- spec/test_helper.rb
|
50
|
+
specification_version: 4
|
51
|
+
summary: ''
|
52
|
+
test_files: []
|
53
|
+
has_rdoc:
|
data/MIT-LICENSE
DELETED
@@ -1,20 +0,0 @@
|
|
1
|
-
Copyright (c) 2010 Michael Deering
|
2
|
-
|
3
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
-
a copy of this software and associated documentation files (the
|
5
|
-
"Software"), to deal in the Software without restriction, including
|
6
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
-
permit persons to whom the Software is furnished to do so, subject to
|
9
|
-
the following conditions:
|
10
|
-
|
11
|
-
The above copyright notice and this permission notice shall be
|
12
|
-
included in all copies or substantial portions of the Software.
|
13
|
-
|
14
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
-
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
-
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
-
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
DELETED
data/install.rb
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
puts IO.read(File.join(File.dirname(__FILE__), 'install.txt'))
|
data/install.txt
DELETED
@@ -1,12 +0,0 @@
|
|
1
|
-
-----------------------------------------------------------------------
|
2
|
-
Attribute Normalizer News:
|
3
|
-
|
4
|
-
New with the 0.3.X release is the ability to change the default
|
5
|
-
normalization and also the ability to chain normalizers together.
|
6
|
-
|
7
|
-
After the flow of patches slows down on this release I will likely
|
8
|
-
freeze the feature set and API for a 1.0 release of the gem.
|
9
|
-
|
10
|
-
Cheers,
|
11
|
-
Michael Deering http://mdeering.com
|
12
|
-
-----------------------------------------------------------------------
|
data/rails/init.rb
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
require 'attribute_normalizer'
|
data/spec/article_spec.rb
DELETED
@@ -1,49 +0,0 @@
|
|
1
|
-
require File.dirname(File.expand_path(__FILE__)) + '/test_helper'
|
2
|
-
|
3
|
-
describe Article do
|
4
|
-
it { should normalize_attribute(:title).from(' Social Life at the Edge of Chaos ').to('Social Life at the Edge of Chaos') }
|
5
|
-
it { should normalize_attribute(:authors).from(' Octavio Miramontes and Pedro Miramontes ').to('Octavio Miramontes and Pedro Miramontes') }
|
6
|
-
it { should normalize_attribute(:slug) }
|
7
|
-
it { should normalize_attribute(:slug).from(' Social Life at the Edge of Chaos ').to('social-life-at-the-edge-of-chaos') }
|
8
|
-
it { should normalize_attribute(:limited_slug) }
|
9
|
-
it { should normalize_attribute(:limited_slug).from(' Social Life at the Edge of Chaos ').to('social-life') }
|
10
|
-
|
11
|
-
context 'normalization should not interfere with other hooks and aliases on the attribute assignment' do
|
12
|
-
before do
|
13
|
-
@article = Article.create!(:title => 'Original Title')
|
14
|
-
end
|
15
|
-
|
16
|
-
it 'should still reflect that the attribute has been changed through the call to super' do
|
17
|
-
lambda { @article.title = 'New Title' }.should change(@article, :title_changed?).from(false).to(true)
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
context 'when another instance of the same saved record has been changed' do
|
22
|
-
before do
|
23
|
-
@article = Article.create!(:title => 'Original Title')
|
24
|
-
@article2 = Article.find(@article.id)
|
25
|
-
@article2.update_attributes(:title => 'New Title')
|
26
|
-
end
|
27
|
-
|
28
|
-
it "should reflect the change when the record is reloaded" do
|
29
|
-
lambda { @article.reload }.should change(@article, :title).from('Original Title').to('New Title')
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
context 'normalization should work with multiple attributes at the same time' do
|
34
|
-
before do
|
35
|
-
@article = Article.new(:title => ' Bad Title ', :authors => ' Bad Authors ')
|
36
|
-
end
|
37
|
-
|
38
|
-
it "should apply normalizations to both attributes" do
|
39
|
-
@article.title.should == 'Bad Title'
|
40
|
-
@article.authors.should == 'Bad Authors'
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
context 'with the default normalizer changed' do
|
45
|
-
@article = Article.new :authors => 'testing the default normalizer'
|
46
|
-
@article.authors.should == 'testing the default normalizer'
|
47
|
-
end
|
48
|
-
|
49
|
-
end
|
@@ -1,26 +0,0 @@
|
|
1
|
-
require File.dirname(File.expand_path(__FILE__)) + '/test_helper'
|
2
|
-
|
3
|
-
describe AttributeNormalizer do
|
4
|
-
|
5
|
-
it 'should add the class method Class#normalize_attributes and Class#normalize_attribute when included' do
|
6
|
-
klass = Class.new do
|
7
|
-
include AttributeNormalizer
|
8
|
-
end
|
9
|
-
|
10
|
-
klass.should respond_to(:normalize_attributes)
|
11
|
-
klass.should respond_to(:normalize_attribute)
|
12
|
-
end
|
13
|
-
|
14
|
-
it 'should not fail due to database exceptions raised by table_exists?' do
|
15
|
-
class PGError < RuntimeError; end
|
16
|
-
|
17
|
-
Class.new(ActiveRecord::Base) do
|
18
|
-
def self.table_exists?
|
19
|
-
raise PGError, "FATAL: something bad happened trying to probe for table existence"
|
20
|
-
end
|
21
|
-
|
22
|
-
include AttributeNormalizer
|
23
|
-
end
|
24
|
-
|
25
|
-
end
|
26
|
-
end
|
data/spec/author_spec.rb
DELETED
@@ -1,29 +0,0 @@
|
|
1
|
-
require File.dirname(File.expand_path(__FILE__)) + '/test_helper'
|
2
|
-
|
3
|
-
describe Author do
|
4
|
-
|
5
|
-
context 'Testing the built in normalizers' do
|
6
|
-
# default normalization [ :strip, :blank ]
|
7
|
-
it { should normalize_attribute(:name) }
|
8
|
-
it { should normalize_attribute(:name).from(' this ').to('this') }
|
9
|
-
it { should normalize_attribute(:name).from(' ').to(nil) }
|
10
|
-
|
11
|
-
# :strip normalizer
|
12
|
-
it { should normalize_attribute(:first_name).from(' this ').to('this') }
|
13
|
-
it { should normalize_attribute(:first_name).from(' ').to('') }
|
14
|
-
|
15
|
-
# :squish normalizer
|
16
|
-
it { should normalize_attribute(:nickname).from(' this nickname ').to('this nickname') }
|
17
|
-
|
18
|
-
# :blank normalizer
|
19
|
-
it { should normalize_attribute(:last_name).from('').to(nil) }
|
20
|
-
it { should normalize_attribute(:last_name).from(' ').to(nil) }
|
21
|
-
it { should normalize_attribute(:last_name).from(' this ').to(' this ') }
|
22
|
-
|
23
|
-
# :phone normalizer
|
24
|
-
it { should normalize_attribute(:phone_number).from('no-numbers-here').to(nil) }
|
25
|
-
it { should normalize_attribute(:phone_number).from('1.877.987.9875').to('18779879875') }
|
26
|
-
it { should normalize_attribute(:phone_number).from('+ 1 (877) 987-9875').to('18779879875') }
|
27
|
-
end
|
28
|
-
|
29
|
-
end
|
data/spec/book_spec.rb
DELETED
@@ -1,52 +0,0 @@
|
|
1
|
-
require File.dirname(File.expand_path(__FILE__)) + '/test_helper'
|
2
|
-
|
3
|
-
describe Book do
|
4
|
-
|
5
|
-
it { should normalize_attribute(:author).from(' Michael Deering ').to('Michael Deering') }
|
6
|
-
|
7
|
-
it { should normalize_attribute(:us_price).from('$3.50').to(3.50) }
|
8
|
-
it { should normalize_attribute(:cnd_price).from('$3,450.98').to(3450.98) }
|
9
|
-
|
10
|
-
it { should normalize_attribute(:summary).from(' Here is my summary that is a little to long ').to('Here is m...') }
|
11
|
-
|
12
|
-
it { should normalize_attribute(:title).from('pick up chicks with magic tricks').to('Pick Up Chicks With Magic Tricks') }
|
13
|
-
|
14
|
-
context 'normalization should not interfere with other hooks and aliases on the attribute assignment' do
|
15
|
-
before do
|
16
|
-
@book = Book.create!(:title => 'Original Title')
|
17
|
-
end
|
18
|
-
|
19
|
-
it 'should still reflect that the attribute has been changed through the call to super' do
|
20
|
-
lambda { @book.title = 'New Title' }.should change(@book, :title_changed?).from(false).to(true)
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
context 'when another instance of the same saved record has been changed' do
|
25
|
-
before do
|
26
|
-
@book = Book.create!(:title => 'Original Title')
|
27
|
-
@book2 = Book.find(@book.id)
|
28
|
-
@book2.update_attributes(:title => 'New Title')
|
29
|
-
end
|
30
|
-
|
31
|
-
it "should reflect the change when the record is reloaded" do
|
32
|
-
lambda { @book.reload }.should change(@book, :title).from('Original Title').to('New Title')
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
context 'normalization should work with multiple attributes at the same time' do
|
37
|
-
before do
|
38
|
-
@book = Book.new(:title => ' Bad Title ', :author => ' Bad Author ')
|
39
|
-
end
|
40
|
-
|
41
|
-
it "should apply normalizations to both attributes" do
|
42
|
-
@book.title.should == 'Bad Title'
|
43
|
-
@book.author.should == 'Bad Author'
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
context 'with the default normalizer changed' do
|
48
|
-
@book = Book.new :author => 'testing the default normalizer'
|
49
|
-
@book.author.should == 'testing the default normalizer'
|
50
|
-
end
|
51
|
-
|
52
|
-
end
|
@@ -1,35 +0,0 @@
|
|
1
|
-
ActiveRecord::Base.establish_connection({
|
2
|
-
:database => ":memory:",
|
3
|
-
:adapter => 'sqlite3',
|
4
|
-
:timeout => 500
|
5
|
-
})
|
6
|
-
|
7
|
-
ActiveRecord::Schema.define do
|
8
|
-
create_table :authors, :force => true do |t|
|
9
|
-
t.string :name
|
10
|
-
t.string :nickname
|
11
|
-
t.string :first_name
|
12
|
-
t.string :last_name
|
13
|
-
t.string :phone_number
|
14
|
-
end
|
15
|
-
|
16
|
-
create_table :books, :force => true do |t|
|
17
|
-
t.string :author
|
18
|
-
t.string :isbn
|
19
|
-
t.decimal :cnd_price
|
20
|
-
t.decimal :us_price
|
21
|
-
t.string :summary
|
22
|
-
t.string :title
|
23
|
-
end
|
24
|
-
|
25
|
-
create_table :journals, :force => true do |t|
|
26
|
-
t.string :name
|
27
|
-
end
|
28
|
-
|
29
|
-
create_table :articles, :force => true do |t|
|
30
|
-
t.string :title
|
31
|
-
t.string :authors
|
32
|
-
t.string :slug
|
33
|
-
t.string :limited_slug
|
34
|
-
end
|
35
|
-
end
|
data/spec/journal_spec.rb
DELETED
@@ -1,12 +0,0 @@
|
|
1
|
-
require File.dirname(File.expand_path(__FILE__)) + '/test_helper'
|
2
|
-
|
3
|
-
describe Journal do
|
4
|
-
|
5
|
-
context 'Testing the built in normalizers' do
|
6
|
-
# default normalization [ :strip, :blank ]
|
7
|
-
it { should normalize_attribute(:name) }
|
8
|
-
it { should normalize_attribute(:name).from(' Physical Review ').to('Physical Review') }
|
9
|
-
it { should normalize_attribute(:name).from(' ').to(nil) }
|
10
|
-
end
|
11
|
-
|
12
|
-
end
|
data/spec/models/article.rb
DELETED
@@ -1,11 +0,0 @@
|
|
1
|
-
class Article < ActiveRecord::Base
|
2
|
-
|
3
|
-
normalize_attribute :slug, :with => [ :strip, :blank ] do |value|
|
4
|
-
value.present? && value.is_a?(String) ? value.downcase.gsub(/\s+/, '-') : value
|
5
|
-
end
|
6
|
-
|
7
|
-
normalize_attribute :limited_slug, :before => [ :strip, :blank ], :after => [ { :truncate => { :length => 11, :omission => '' } } ] do |value|
|
8
|
-
value.present? && value.is_a?(String) ? value.downcase.gsub(/\s+/, '-') : value
|
9
|
-
end
|
10
|
-
|
11
|
-
end
|
data/spec/models/author.rb
DELETED
@@ -1,9 +0,0 @@
|
|
1
|
-
class Author < ActiveRecord::Base
|
2
|
-
|
3
|
-
normalize_attribute :name
|
4
|
-
normalize_attribute :nickname, :with => :squish
|
5
|
-
normalize_attribute :first_name, :with => :strip
|
6
|
-
normalize_attribute :last_name, :with => :blank
|
7
|
-
normalize_attribute :phone_number, :with => :phone
|
8
|
-
|
9
|
-
end
|
data/spec/models/book.rb
DELETED
@@ -1,13 +0,0 @@
|
|
1
|
-
class Book < ActiveRecord::Base
|
2
|
-
|
3
|
-
normalize_attribute :author
|
4
|
-
|
5
|
-
normalize_attribute :us_price, :cnd_price, :with => :currency
|
6
|
-
|
7
|
-
normalize_attributes :summary, :with => [ :strip, { :truncate => { :length => 12 } }, :blank ]
|
8
|
-
|
9
|
-
normalize_attributes :title do |value|
|
10
|
-
value.is_a?(String) ? value.titleize.strip : value
|
11
|
-
end
|
12
|
-
|
13
|
-
end
|
data/spec/models/journal.rb
DELETED
data/spec/test_helper.rb
DELETED
@@ -1,49 +0,0 @@
|
|
1
|
-
require 'rubygems'
|
2
|
-
require 'rspec'
|
3
|
-
require 'active_record'
|
4
|
-
require 'mongoid'
|
5
|
-
|
6
|
-
$LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib')
|
7
|
-
|
8
|
-
require 'attribute_normalizer'
|
9
|
-
|
10
|
-
|
11
|
-
AttributeNormalizer.configure do |config|
|
12
|
-
|
13
|
-
config.normalizers[:currency] = lambda do |value, options|
|
14
|
-
value.is_a?(String) ? value.gsub(/[^0-9\.]+/, '') : value
|
15
|
-
end
|
16
|
-
|
17
|
-
config.normalizers[:truncate] = lambda do |text, options|
|
18
|
-
if text.is_a?(String)
|
19
|
-
options.reverse_merge!(:length => 30, :omission => "...")
|
20
|
-
l = options[:length] - options[:omission].mb_chars.length
|
21
|
-
chars = text.mb_chars
|
22
|
-
(chars.length > options[:length] ? chars[0...l] + options[:omission] : text).to_s
|
23
|
-
else
|
24
|
-
text
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
config.normalizers[:special_normalizer] = lambda do |value, options|
|
29
|
-
(value.is_a?(String) && value.match(/testing the default normalizer/)) ? 'testing the default normalizer' : value
|
30
|
-
end
|
31
|
-
|
32
|
-
config.default_normalizers = :strip, :special_normalizer, :blank
|
33
|
-
config.default_attributes = :name, :title
|
34
|
-
config.add_default_attribute :authors, :strip => true, :blank => true
|
35
|
-
|
36
|
-
end
|
37
|
-
|
38
|
-
|
39
|
-
require 'connection_and_schema'
|
40
|
-
require 'models/book'
|
41
|
-
require 'models/author'
|
42
|
-
require 'models/journal'
|
43
|
-
require 'models/article'
|
44
|
-
require 'models/magazine'
|
45
|
-
|
46
|
-
|
47
|
-
RSpec.configure do |config|
|
48
|
-
config.include AttributeNormalizer::RSpecMatcher
|
49
|
-
end
|