attribute_normalizer 1.0.0.pre3 → 1.0.0.pre4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.textile +10 -5
- data/Rakefile +1 -51
- data/lib/attribute_normalizer.rb +14 -4
- data/lib/attribute_normalizer/model_inclusions.rb +25 -14
- data/spec/article_spec.rb +2 -2
- data/spec/attribute_normalizer_spec.rb +1 -1
- data/spec/author_spec.rb +1 -1
- data/spec/book_spec.rb +2 -2
- data/spec/journal_spec.rb +1 -1
- data/spec/models/author.rb +4 -4
- data/spec/models/book.rb +2 -2
- data/spec/test_helper.rb +2 -0
- metadata +6 -8
data/README.textile
CHANGED
@@ -4,6 +4,11 @@ p. A little normalization goes a long way in helping maintain data integrity.
|
|
4
4
|
|
5
5
|
h2. Recent Changes
|
6
6
|
|
7
|
+
* 1.0.0.pre
|
8
|
+
* -DSL Changes-
|
9
|
+
* Default attributes to normalize
|
10
|
+
* mongid support
|
11
|
+
|
7
12
|
* 0.3.0
|
8
13
|
** Normalizer Chaining
|
9
14
|
** Built-in common normalizers
|
@@ -90,16 +95,16 @@ AttributeNormalizer.configure do |config|
|
|
90
95
|
end
|
91
96
|
end
|
92
97
|
|
93
|
-
# The default normalizers if no
|
98
|
+
# The default normalizers if no :with option or block is given is to apply the :strip and :blank normalizers (in that order).
|
94
99
|
# You can change this if you would like as follows:
|
95
|
-
# config.default_normalizers =
|
100
|
+
# config.default_normalizers = :strip, :blank
|
96
101
|
|
97
102
|
# You can enable the attribute normalizers automatically if the specified attributes exist in your column_names. It will use
|
98
103
|
# the default normalizers for each attribute (e.g. config.default_normalizers)
|
99
104
|
# config.default_attributes = :name, :title
|
100
105
|
|
101
106
|
# Also, You can add an specific attribute to default_attributes using one or more normalizers:
|
102
|
-
# config.add_default_attribute :name, :
|
107
|
+
# config.add_default_attribute :name, :with => :truncate
|
103
108
|
end
|
104
109
|
</code></pre>
|
105
110
|
|
@@ -112,10 +117,10 @@ The _normalize_attributes_ method is eager loaded into your ORM. _normalize_att
|
|
112
117
|
normalize_attributes :author, :publisher
|
113
118
|
|
114
119
|
# Using one of our predefined normalizers.
|
115
|
-
normalize_attribute :price, :
|
120
|
+
normalize_attribute :price, :with => :currency
|
116
121
|
|
117
122
|
# Using more then one of our predefined normalizers including one with options
|
118
|
-
normalize_attribute :summary, :
|
123
|
+
normalize_attribute :summary, :with => [ :strip, { :truncate => { :length => 12 } } ]
|
119
124
|
|
120
125
|
# You can also define your normalization block inline.
|
121
126
|
normalize_attribute :title do |value|
|
data/Rakefile
CHANGED
@@ -1,42 +1,7 @@
|
|
1
1
|
require 'rake'
|
2
2
|
require 'rake/rdoctask'
|
3
3
|
require 'spec/rake/spectask'
|
4
|
-
|
5
|
-
begin
|
6
|
-
AUTHOR = "Michael Deering"
|
7
|
-
EMAIL = "mdeering@mdeering.com"
|
8
|
-
GEM = "attribute_normalizer"
|
9
|
-
HOMEPAGE = "http://github.com/mdeering/attribute_normalizer"
|
10
|
-
SUMMARY = "Attribute normalizer that excepts code blocks."
|
11
|
-
|
12
|
-
require 'jeweler'
|
13
|
-
Jeweler::Tasks.new do |s|
|
14
|
-
s.author = AUTHOR
|
15
|
-
s.email = EMAIL
|
16
|
-
s.files = %w(install.rb install.txt MIT-LICENSE README.textile Rakefile) + Dir.glob("{rails,lib,spec}/**/*")
|
17
|
-
s.homepage = HOMEPAGE
|
18
|
-
s.name = GEM
|
19
|
-
s.require_path = 'lib'
|
20
|
-
s.summary = SUMMARY
|
21
|
-
s.post_install_message = %q[
|
22
|
-
-----------------------------------------------------------------------
|
23
|
-
Attribute Normalizer News:
|
24
|
-
|
25
|
-
New with the 0.3.X release is the ability to change the default
|
26
|
-
normalization and also the ability to chain normalizers together.
|
27
|
-
|
28
|
-
After the flow of patches slows down on this release I will likely
|
29
|
-
freeze the feature set and API for a 1.0 release of the gem.
|
30
|
-
|
31
|
-
Cheers,
|
32
|
-
Michael Deering http://mdeering.com
|
33
|
-
-----------------------------------------------------------------------
|
34
|
-
]
|
35
|
-
end
|
36
|
-
Jeweler::GemcutterTasks.new
|
37
|
-
rescue LoadError
|
38
|
-
puts "Jeweler, or one of its dependencies, is not available. Install it with: sudo gem install jeweler"
|
39
|
-
end
|
4
|
+
require 'bundler/gem_tasks'
|
40
5
|
|
41
6
|
desc 'Default: spec tests.'
|
42
7
|
task :default => :spec
|
@@ -47,18 +12,3 @@ Spec::Rake::SpecTask.new('spec') do |t|
|
|
47
12
|
t.spec_opts = ["-c"]
|
48
13
|
end
|
49
14
|
|
50
|
-
desc "Run all examples with RCov"
|
51
|
-
Spec::Rake::SpecTask.new('examples_with_rcov') do |t|
|
52
|
-
t.spec_files = FileList['spec/**/*_spec.rb']
|
53
|
-
t.rcov = true
|
54
|
-
t.rcov_opts = ['--exclude', '/opt,spec,Library']
|
55
|
-
end
|
56
|
-
|
57
|
-
desc 'Generate documentation for the attribute_normalizer plugin.'
|
58
|
-
Rake::RDocTask.new(:rdoc) do |rdoc|
|
59
|
-
rdoc.rdoc_dir = 'rdoc'
|
60
|
-
rdoc.title = 'AttributeNormalizer'
|
61
|
-
rdoc.options << '--line-numbers' << '--inline-source'
|
62
|
-
rdoc.rdoc_files.include('README.textile')
|
63
|
-
rdoc.rdoc_files.include('lib/**/*.rb')
|
64
|
-
end
|
data/lib/attribute_normalizer.rb
CHANGED
@@ -24,17 +24,17 @@ module AttributeNormalizer
|
|
24
24
|
attr_accessor :default_normalizers, :normalizers, :default_attributes
|
25
25
|
|
26
26
|
def default_normalizers=(normalizers)
|
27
|
-
@default_normalizers = normalizers
|
27
|
+
@default_normalizers = normalizers.is_a?(Array) ? normalizers : [ normalizers ]
|
28
28
|
end
|
29
29
|
|
30
30
|
def default_attributes=(attributes)
|
31
31
|
[attributes].flatten.each do |attribute|
|
32
|
-
add_default_attribute(attribute, default_normalizers)
|
32
|
+
add_default_attribute(attribute, :with => default_normalizers)
|
33
33
|
end
|
34
34
|
end
|
35
35
|
|
36
|
-
def add_default_attribute(attribute,
|
37
|
-
@default_attributes[attribute.to_s] =
|
36
|
+
def add_default_attribute(attribute, options)
|
37
|
+
@default_attributes[attribute.to_s] = { :with => default_normalizers }.merge(options)
|
38
38
|
end
|
39
39
|
|
40
40
|
def initialize
|
@@ -63,6 +63,16 @@ def include_attribute_normalizer(class_or_module)
|
|
63
63
|
end
|
64
64
|
end
|
65
65
|
|
66
|
+
|
67
|
+
|
66
68
|
include_attribute_normalizer(ActiveModel::Base) if defined?(ActiveModel::Base)
|
67
69
|
include_attribute_normalizer(ActiveRecord::Base) if defined?(ActiveRecord::Base)
|
68
70
|
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
|
@@ -6,8 +6,10 @@ module AttributeNormalizer
|
|
6
6
|
|
7
7
|
module ClassMethods
|
8
8
|
def normalize_attributes(*attributes, &block)
|
9
|
-
|
10
|
-
|
9
|
+
options = attributes.last.is_a?(::Hash) ? attributes.pop : {}
|
10
|
+
|
11
|
+
normalizers = [ options.delete(:with) ].flatten.compact
|
12
|
+
|
11
13
|
if normalizers.empty? && !block_given?
|
12
14
|
normalizers = AttributeNormalizer.configuration.default_normalizers # the default normalizers
|
13
15
|
end
|
@@ -20,16 +22,14 @@ module AttributeNormalizer
|
|
20
22
|
else
|
21
23
|
define_method "normalize_#{attribute}" do |value|
|
22
24
|
normalized = value
|
23
|
-
|
24
|
-
normalizers.each do |
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
normalized = if normalizer.respond_to?(:normalize)
|
29
|
-
normalizer.normalize(normalized, TrueClass === options ? {} : options)
|
30
|
-
else
|
31
|
-
normalizer.call(normalized, TrueClass === options ? {} : options)
|
25
|
+
|
26
|
+
normalizers.each do |normalizer_name|
|
27
|
+
unless normalizer_name.kind_of?(Symbol)
|
28
|
+
normalizer_name, options = normalizer_name.keys[0], normalizer_name[ normalizer_name.keys[0] ]
|
32
29
|
end
|
30
|
+
normalizer = AttributeNormalizer.configuration.normalizers[normalizer_name]
|
31
|
+
raise AttributeNormalizer::MissingNormalizer.new("No normalizer was found for #{normalizer_name}") unless normalizer
|
32
|
+
normalized = normalizer.respond_to?(:normalize) ? normalizer.normalize( normalized , options) : normalizer.call(normalized, options)
|
33
33
|
end
|
34
34
|
normalized
|
35
35
|
end
|
@@ -37,8 +37,17 @@ module AttributeNormalizer
|
|
37
37
|
|
38
38
|
self.send :private, "normalize_#{attribute}"
|
39
39
|
|
40
|
+
if method_defined?(:"#{attribute}=")
|
41
|
+
alias_method "old_#{attribute}=", "#{attribute}="
|
42
|
+
end
|
43
|
+
|
40
44
|
define_method "#{attribute}=" do |value|
|
41
|
-
|
45
|
+
begin
|
46
|
+
super(self.send(:"normalize_#{attribute}", value))
|
47
|
+
rescue NoMethodError
|
48
|
+
normalized_value = self.send(:"normalize_#{attribute}", value)
|
49
|
+
self.send("old_#{attribute}=", normalized_value)
|
50
|
+
end
|
42
51
|
end
|
43
52
|
|
44
53
|
end
|
@@ -46,14 +55,16 @@ module AttributeNormalizer
|
|
46
55
|
alias :normalize_attribute :normalize_attributes
|
47
56
|
|
48
57
|
def normalize_default_attributes
|
49
|
-
AttributeNormalizer.configuration.default_attributes.each do |attribute_name, options|
|
58
|
+
AttributeNormalizer.configuration.default_attributes.each do |attribute_name, options|
|
50
59
|
normalize_attribute(attribute_name, options) if self.column_names.include?(attribute_name)
|
51
60
|
end
|
52
61
|
end
|
53
62
|
|
54
63
|
def inherited(subclass)
|
55
64
|
super
|
56
|
-
subclass.
|
65
|
+
if subclass.respond_to?(:table_exists?) && subclass.table_exists?
|
66
|
+
subclass.normalize_default_attributes
|
67
|
+
end
|
57
68
|
end
|
58
69
|
end
|
59
70
|
end
|
data/spec/article_spec.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/test_helper'
|
1
|
+
require File.dirname(File.expand_path(__FILE__)) + '/test_helper'
|
2
2
|
|
3
3
|
describe Article do
|
4
4
|
it { should normalize_attribute(:title).from(' Social Life at the Edge of Chaos ').to('Social Life at the Edge of Chaos') }
|
5
5
|
it { should normalize_attribute(:authors).from(' Octavio Miramontes and Pedro Miramontes ').to('Octavio Miramontes and Pedro Miramontes') }
|
6
6
|
|
7
|
-
context 'normalization should not
|
7
|
+
context 'normalization should not interfere with other hooks and aliases on the attribute assignment' do
|
8
8
|
before do
|
9
9
|
@article = Article.create!(:title => 'Original Title')
|
10
10
|
end
|
data/spec/author_spec.rb
CHANGED
data/spec/book_spec.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/test_helper'
|
1
|
+
require File.dirname(File.expand_path(__FILE__)) + '/test_helper'
|
2
2
|
|
3
3
|
describe Book do
|
4
4
|
|
@@ -11,7 +11,7 @@ describe Book do
|
|
11
11
|
|
12
12
|
it { should normalize_attribute(:title).from('pick up chicks with magic tricks').to('Pick Up Chicks With Magic Tricks') }
|
13
13
|
|
14
|
-
context 'normalization should not
|
14
|
+
context 'normalization should not interfere with other hooks and aliases on the attribute assignment' do
|
15
15
|
before do
|
16
16
|
@book = Book.create!(:title => 'Original Title')
|
17
17
|
end
|
data/spec/journal_spec.rb
CHANGED
data/spec/models/author.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
class Author < ActiveRecord::Base
|
2
2
|
|
3
3
|
normalize_attribute :name
|
4
|
-
normalize_attribute :nickname, :
|
5
|
-
normalize_attribute :first_name, :
|
6
|
-
normalize_attribute :last_name, :
|
7
|
-
normalize_attribute :phone_number, :
|
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
8
|
|
9
9
|
end
|
data/spec/models/book.rb
CHANGED
@@ -2,9 +2,9 @@ class Book < ActiveRecord::Base
|
|
2
2
|
|
3
3
|
normalize_attribute :author
|
4
4
|
|
5
|
-
normalize_attribute :us_price, :cnd_price, :
|
5
|
+
normalize_attribute :us_price, :cnd_price, :with => :currency
|
6
6
|
|
7
|
-
normalize_attributes :summary, :
|
7
|
+
normalize_attributes :summary, :with => [ :strip, { :truncate => { :length => 12 } }, :blank ]
|
8
8
|
|
9
9
|
normalize_attributes :title do |value|
|
10
10
|
value.is_a?(String) ? value.titleize.strip : value
|
data/spec/test_helper.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'spec'
|
3
3
|
require 'active_record'
|
4
|
+
require 'mongoid'
|
4
5
|
|
5
6
|
$LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib')
|
6
7
|
|
@@ -40,6 +41,7 @@ require 'models/book'
|
|
40
41
|
require 'models/author'
|
41
42
|
require 'models/journal'
|
42
43
|
require 'models/article'
|
44
|
+
require 'models/magazine'
|
43
45
|
|
44
46
|
|
45
47
|
Spec::Runner.configure do |config|
|
metadata
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: attribute_normalizer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 1923831937
|
5
5
|
prerelease: 6
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 0
|
9
9
|
- 0
|
10
10
|
- pre
|
11
|
-
-
|
12
|
-
version: 1.0.0.
|
11
|
+
- 4
|
12
|
+
version: 1.0.0.pre4
|
13
13
|
platform: ruby
|
14
14
|
authors:
|
15
15
|
- Michael Deering
|
@@ -17,7 +17,7 @@ autorequire:
|
|
17
17
|
bindir: bin
|
18
18
|
cert_chain: []
|
19
19
|
|
20
|
-
date: 2011-
|
20
|
+
date: 2011-07-28 00:00:00 -06:00
|
21
21
|
default_executable:
|
22
22
|
dependencies: []
|
23
23
|
|
@@ -58,9 +58,7 @@ has_rdoc: true
|
|
58
58
|
homepage: http://github.com/mdeering/attribute_normalizer
|
59
59
|
licenses: []
|
60
60
|
|
61
|
-
post_install_message:
|
62
|
-
-----------------------------------------------------------------------\n Attribute Normalizer News:\n\n New with the 0.3.X release is the ability to change the default\n normalization and also the ability to chain normalizers together.\n\n After the flow of patches slows down on this release I will likely\n freeze the feature set and API for a 1.0 release of the gem.\n\n Cheers,\n Michael Deering http://mdeering.com\n\
|
63
|
-
-----------------------------------------------------------------------\n "
|
61
|
+
post_install_message:
|
64
62
|
rdoc_options:
|
65
63
|
- --charset=UTF-8
|
66
64
|
require_paths:
|
@@ -88,7 +86,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
88
86
|
requirements: []
|
89
87
|
|
90
88
|
rubyforge_project:
|
91
|
-
rubygems_version: 1.
|
89
|
+
rubygems_version: 1.4.2
|
92
90
|
signing_key:
|
93
91
|
specification_version: 3
|
94
92
|
summary: Attribute normalizer that excepts code blocks.
|