attribute_normalizer 0.1.2 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +1 -1
- data/README.textile +91 -47
- data/Rakefile +17 -17
- data/install.txt +5 -5
- data/lib/attribute_normalizer.rb +75 -8
- data/spec/attribute_normalizer_spec.rb +2 -110
- data/spec/book_spec.rb +39 -0
- data/spec/test_helper.rb +42 -4
- metadata +18 -7
data/MIT-LICENSE
CHANGED
data/README.textile
CHANGED
@@ -1,84 +1,128 @@
|
|
1
1
|
h1. Attribute Normalizer
|
2
2
|
|
3
|
-
p.
|
3
|
+
p. A little normalization goes a long way in helping maintain data integrity.
|
4
4
|
|
5
|
-
h2.
|
5
|
+
h2. Recent Changes
|
6
6
|
|
7
|
-
|
7
|
+
* 0.2.0
|
8
|
+
** Removed the normalization on reads.
|
9
|
+
** Added out of the box support for CassandraObjects
|
10
|
+
** Included RSpec matcher _normalizer_attribute_
|
11
|
+
** Added the ability to define global normalizers
|
12
|
+
** *Strings are no longer get 'strip' called on them before getting passed on to your defined normalization blocks*
|
8
13
|
|
9
|
-
|
14
|
+
* 0.1.2
|
15
|
+
** Changed post-normalization attribute assignment to call super to avoid preventing plugins/gems/hooks to work on the same attribute.
|
10
16
|
|
11
|
-
|
17
|
+
h2. Supported ORMs
|
12
18
|
|
13
|
-
|
19
|
+
* Active Record
|
20
|
+
* CassandraObjects
|
14
21
|
|
15
|
-
p.
|
22
|
+
p. _I will gladly take pull requests to automatically load Attribute Normalizer into your ORM of choice if requested. To test it out on your ORM just include the AttributeNormalizer module after requiring it._
|
16
23
|
|
17
|
-
<pre><code
|
24
|
+
<pre><code># your_initializer.rb
|
25
|
+
require 'attribute_normalizer'
|
26
|
+
YourORM::Base.send :include, AttributeNormalizer</code></pre>
|
18
27
|
|
19
|
-
|
20
|
-
|
21
|
-
<pre><code>$ gem tumble
|
22
|
-
Thanks for using Gemcutter!
|
23
|
-
Your gem sources are now:
|
24
|
-
- http://gemcutter.org
|
25
|
-
- http://gems.rubyforge.org/
|
26
|
-
- http://gems.github.com
|
27
|
-
</code></pre>
|
28
|
-
|
29
|
-
h3. Install the Attribute Normalizer gem
|
28
|
+
h2. Install
|
30
29
|
|
31
30
|
<pre><code>sudo gem install attribute_normalizer</code></pre>
|
32
31
|
|
33
|
-
|
32
|
+
p. Then just required it. Rails usages is as follows.
|
34
33
|
|
35
|
-
|
34
|
+
<pre><code># config/environment.rb
|
35
|
+
gem.config 'attribute_normalizer'</code></pre>
|
36
36
|
|
37
|
-
|
38
|
-
|
39
|
-
or the old-school but still c00l way!
|
37
|
+
p. It also still works as a traditional Rails plugin.
|
40
38
|
|
41
|
-
|
39
|
+
./script/plugin install git://github.com/mdeering/attribute_normalizer.git
|
42
40
|
|
43
|
-
|
41
|
+
h2. Usage
|
44
42
|
|
45
|
-
|
46
|
-
git submodule init
|
43
|
+
p. Lets create a quick test/spec for what we want to accomplish as far as normalization using the built in RSpec matcher.
|
47
44
|
|
48
|
-
|
45
|
+
<pre><code># spec/models/book_spec.rb
|
46
|
+
describe Book do
|
47
|
+
it { should normalize_attribute(:author) }
|
48
|
+
it { should normalize_attribute(:price).from('$3,450.98').to(3450.98) }
|
49
|
+
it { should normalize_attribute(:summary).from('Here is my summary that is a little to long').to('Here is m...') }
|
50
|
+
it { should normalize_attribute(:title).from('pick up chicks with magic tricks').to('Pick Up Chicks With Magic Tricks')}
|
51
|
+
end</code></pre>
|
49
52
|
|
50
|
-
|
53
|
+
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.
|
51
54
|
|
52
|
-
<pre><code
|
53
|
-
|
55
|
+
<pre><code># config/initializers/attribute_normalizer.rb
|
56
|
+
AttributeNormalizer.configure do |config|
|
54
57
|
|
55
|
-
|
56
|
-
|
58
|
+
config.normalizers[:currency] = lambda do |value, options|
|
59
|
+
value.is_a?(String) ? value.gsub(/[^0-9\.]+/, '') : value
|
60
|
+
end
|
57
61
|
|
58
|
-
|
59
|
-
|
62
|
+
config.normalizers[:truncate] = lambda do |text, options|
|
63
|
+
if text.is_a?(String)
|
64
|
+
options.reverse_merge!(:length => 30, :omission => "...")
|
65
|
+
l = options[:length] - options[:omission].mb_chars.length
|
66
|
+
chars = text.mb_chars
|
67
|
+
(chars.length > options[:length] ? chars[0...l] + options[:omission] : text).to_s
|
68
|
+
else
|
69
|
+
text
|
70
|
+
end
|
60
71
|
end
|
61
72
|
|
62
73
|
end
|
74
|
+
</code></pre>
|
75
|
+
|
76
|
+
The _normalize_attributes_ method is eager loaded into your ORM. _normalize_attribute_ is aliased to _normalized_attributes_ and both can take in a single attribute or an array of attributes.
|
63
77
|
|
64
|
-
|
65
|
-
# Blank normalizes to nil
|
66
|
-
object.first_name = ''
|
67
|
-
object.first_name # => nil
|
78
|
+
<pre><code>class Book < ActiveRecord::Base
|
68
79
|
|
69
|
-
#
|
70
|
-
|
71
|
-
|
80
|
+
# By default it will strip leading and trailing whitespace
|
81
|
+
# and set to nil if blank.
|
82
|
+
normalize_attributes :author, :publisher
|
72
83
|
|
73
|
-
#
|
74
|
-
|
75
|
-
|
84
|
+
# Using one of our predefined normalizers.
|
85
|
+
normalize_attribute :price, :with => :currency
|
86
|
+
|
87
|
+
# Using one of our predefined normalizers with options
|
88
|
+
normalize_attribute :summary, :with => :truncate, :length => 12
|
89
|
+
|
90
|
+
# You can also define your normalization block inline.
|
91
|
+
normalize_attribute :title do |value|
|
92
|
+
value.is_a?(String) ? value.titleize.strip : value
|
93
|
+
end
|
94
|
+
|
95
|
+
end</code></pre>
|
96
|
+
|
97
|
+
p. All the specs will pass now. Here is quick look at the behaviour from a console.
|
98
|
+
|
99
|
+
<pre><code>summary = 'Here is my summary that is a little to long'
|
100
|
+
title = 'pick up chicks with magic tricks'
|
101
|
+
book = Book.create!(:author => '', :price => '$3,450.89', :summary => summary, :title => title)
|
102
|
+
book.author # => nil
|
103
|
+
book.price # => 3450.89
|
104
|
+
book.summary # => 'Here is m...'
|
105
|
+
book.title # => 'Pick Up Chicks With Magic Tricks'
|
76
106
|
</code></pre>
|
77
107
|
|
108
|
+
h2. Test Helpers
|
109
|
+
|
110
|
+
p. If you are running RSpec there is matcher available for use. Usage can been seen above. Include it as follows.
|
111
|
+
|
112
|
+
<pre><code># spec/spec_helper.rb
|
113
|
+
# ...
|
114
|
+
|
115
|
+
Spec::Runner.configure do |config|
|
116
|
+
# ...
|
117
|
+
config.include AttributeNormalizer::RSpecMatcher, :type => :models
|
118
|
+
end</code></pre>
|
119
|
+
|
120
|
+
p. _I will gladly take a patch to add a macro to Test::Unit if someone submits it._
|
121
|
+
|
78
122
|
h2. Credits
|
79
123
|
|
80
124
|
Original module code and concept was taken from "Dan Kubb":http://github.com/dkubb during a project we worked on together. I found that I was consistently using this across all my projects so I wanted to plugin-er-size and gem this up for easy reuse.
|
81
125
|
|
82
126
|
h2. Copyright
|
83
127
|
|
84
|
-
Copyright (c) 2009 "Michael Deering(Edmonton Ruby on Rails)":http://mdeering.com See MIT-LICENSE for details.
|
128
|
+
Copyright (c) 2009-2010 "Michael Deering(Edmonton Ruby on Rails)":http://mdeering.com See MIT-LICENSE for details.
|
data/Rakefile
CHANGED
@@ -4,36 +4,36 @@ require 'spec/rake/spectask'
|
|
4
4
|
|
5
5
|
begin
|
6
6
|
AUTHOR = "Michael Deering"
|
7
|
-
EMAIL = "mdeering@mdeering.com"
|
7
|
+
EMAIL = "mdeering@mdeering.com"
|
8
8
|
GEM = "attribute_normalizer"
|
9
|
-
HOMEPAGE = "http://github.com/mdeering/attribute_normalizer"
|
10
|
-
SUMMARY = "
|
11
|
-
|
9
|
+
HOMEPAGE = "http://github.com/mdeering/attribute_normalizer"
|
10
|
+
SUMMARY = "Attribute normalizer that excepts code blocks."
|
11
|
+
|
12
12
|
require 'jeweler'
|
13
13
|
Jeweler::Tasks.new do |s|
|
14
14
|
s.author = AUTHOR
|
15
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'
|
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
20
|
s.summary = SUMMARY
|
21
21
|
s.post_install_message = %q[
|
22
|
-
|
23
|
-
|
22
|
+
-----------------------------------------------------------------------
|
23
|
+
Attribute Normalizer News:
|
24
24
|
|
25
|
-
|
26
|
-
|
27
|
-
|
25
|
+
There are lots of changes from the 0.1.2 release to the 0.2.0 release!
|
26
|
+
Change log is here http://github.com/mdeering/attribute_normalizer/blob/master/ROADMAP.textile
|
27
|
+
Docs have been updated here http://github.com/mdeering/attribute_normalizer
|
28
28
|
|
29
|
-
|
30
|
-
|
31
|
-
|
29
|
+
Cheers,
|
30
|
+
Michael Deering http://mdeering.com
|
31
|
+
-----------------------------------------------------------------------
|
32
32
|
]
|
33
33
|
end
|
34
34
|
Jeweler::GemcutterTasks.new
|
35
35
|
rescue LoadError
|
36
|
-
puts "Jeweler, or one of its dependencies, is not available. Install it with: sudo gem install
|
36
|
+
puts "Jeweler, or one of its dependencies, is not available. Install it with: sudo gem install jeweler"
|
37
37
|
end
|
38
38
|
|
39
39
|
desc 'Default: spec tests.'
|
data/install.txt
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
-----------------------------------------------------------------------
|
2
2
|
Attribute Normalizer News:
|
3
|
-
|
4
|
-
|
5
|
-
http://github.com/mdeering/attribute_normalizer/blob/master/ROADMAP.textile
|
6
|
-
|
7
|
-
|
3
|
+
|
4
|
+
There are lots of changes from the 0.1.2 release to the 0.2.0 release!
|
5
|
+
Change log is here http://github.com/mdeering/attribute_normalizer/blob/master/ROADMAP.textile
|
6
|
+
Docs have been updated here http://github.com/mdeering/attribute_normalizer
|
7
|
+
|
8
8
|
Cheers,
|
9
9
|
Michael Deering http://mdeering.com
|
10
10
|
-----------------------------------------------------------------------
|
data/lib/attribute_normalizer.rb
CHANGED
@@ -1,31 +1,54 @@
|
|
1
1
|
module AttributeNormalizer
|
2
2
|
|
3
|
+
class MissingNormalizer < ArgumentError; end
|
4
|
+
|
5
|
+
class << self
|
6
|
+
attr_accessor :configuration
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.configure
|
10
|
+
self.configuration ||= Configuration.new
|
11
|
+
yield(configuration)
|
12
|
+
end
|
13
|
+
|
3
14
|
def self.included(base)
|
4
15
|
base.extend ClassMethods
|
5
16
|
end
|
6
17
|
|
18
|
+
class Configuration
|
19
|
+
|
20
|
+
attr_accessor :normalizers
|
21
|
+
|
22
|
+
def initialize
|
23
|
+
@normalizers = {}
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
7
28
|
module ClassMethods
|
8
29
|
|
9
30
|
def normalize_attributes(*attributes, &block)
|
31
|
+
options = attributes.extract_options!
|
10
32
|
|
11
33
|
attributes.each do |attribute|
|
12
34
|
|
13
35
|
klass = class << self; self end
|
14
36
|
|
15
37
|
klass.send :define_method, "normalize_#{attribute}" do |value|
|
16
|
-
|
17
|
-
|
38
|
+
normalized = if block_given? && !value.blank?
|
39
|
+
yield(value)
|
40
|
+
elsif !options[:with].nil? && !value.blank?
|
41
|
+
raise AttributeNormalizer::MissingNormalizer.new("No normalizer was found for #{options[:with]}") unless AttributeNormalizer.configuration.normalizers.has_key?(options[:with])
|
42
|
+
this = AttributeNormalizer.configuration.normalizers[options.delete(:with)].call(value, options)
|
43
|
+
else
|
44
|
+
value.is_a?(String) ? value.strip : value
|
45
|
+
end
|
18
46
|
normalized.nil? || (normalized.is_a?(String) && normalized == '') ? nil : normalized
|
19
47
|
end
|
20
48
|
|
21
49
|
klass.send :private, "normalize_#{attribute}"
|
22
50
|
|
23
51
|
src = <<-end_src
|
24
|
-
def #{attribute}
|
25
|
-
value = super
|
26
|
-
value.nil? ? value : self.class.send(:normalize_#{attribute}, value)
|
27
|
-
end
|
28
|
-
|
29
52
|
def #{attribute}=(value)
|
30
53
|
super(self.class.send(:normalize_#{attribute}, value))
|
31
54
|
end
|
@@ -40,6 +63,50 @@ module AttributeNormalizer
|
|
40
63
|
alias :normalize_attribute :normalize_attributes
|
41
64
|
|
42
65
|
end
|
66
|
+
|
67
|
+
module RSpecMatcher
|
68
|
+
|
69
|
+
def normalize_attribute(attribute)
|
70
|
+
NormalizeAttribute.new(attribute)
|
71
|
+
end
|
72
|
+
|
73
|
+
class NormalizeAttribute
|
74
|
+
|
75
|
+
def description
|
76
|
+
"normalize #{@attribute} from #{@from.nil? ? 'nil' : "\"#{@from}\""} to #{@to.nil? ? 'nil' : "\"#{@to}\""}"
|
77
|
+
end
|
78
|
+
|
79
|
+
def failure_message
|
80
|
+
"#{@attribute} did not normalize as expected! \"#{@subject.send(@attribute)}\" != #{@to.nil? ? 'nil' : "\"#{@to}\""}"
|
81
|
+
end
|
82
|
+
|
83
|
+
def from(value)
|
84
|
+
@from = value
|
85
|
+
self
|
86
|
+
end
|
87
|
+
|
88
|
+
def initialize(attribute)
|
89
|
+
@attribute = attribute
|
90
|
+
@from = ''
|
91
|
+
end
|
92
|
+
|
93
|
+
def matches?(subject)
|
94
|
+
@subject = subject
|
95
|
+
@subject.send("#{@attribute}=", @from)
|
96
|
+
return false unless @subject.send(@attribute) == @to
|
97
|
+
true
|
98
|
+
end
|
99
|
+
|
100
|
+
def to(value)
|
101
|
+
@to = value
|
102
|
+
self
|
103
|
+
end
|
104
|
+
|
105
|
+
end
|
106
|
+
|
107
|
+
end
|
108
|
+
|
43
109
|
end
|
44
110
|
|
45
|
-
ActiveRecord::Base.send(:include, AttributeNormalizer)
|
111
|
+
ActiveRecord::Base.send(:include, AttributeNormalizer) if defined?(ActiveRecord::Base)
|
112
|
+
CassandraObject::Base.send(:include, AttributeNormalizer) if defined?(CassandraObject::Base)
|
@@ -1,123 +1,15 @@
|
|
1
1
|
require File.dirname(__FILE__) + '/test_helper'
|
2
|
-
require 'attribute_normalizer'
|
3
2
|
|
4
3
|
describe AttributeNormalizer do
|
5
4
|
|
6
|
-
it 'should add the class method Class#normalize_attributes when included' do
|
5
|
+
it 'should add the class method Class#normalize_attributes and Class#normalize_attribute when included' do
|
7
6
|
|
8
7
|
klass = Class.new do
|
9
8
|
include AttributeNormalizer
|
10
9
|
end
|
11
10
|
|
12
11
|
klass.respond_to?(:normalize_attributes).should be_true
|
13
|
-
|
14
|
-
|
15
|
-
end
|
16
|
-
|
17
|
-
describe '#normalize_attributes without a block' do
|
18
|
-
|
19
|
-
before do
|
20
|
-
|
21
|
-
class Klass
|
22
|
-
attr_accessor :attribute
|
23
|
-
include AttributeNormalizer
|
24
|
-
normalize_attributes :attribute
|
25
|
-
end
|
26
|
-
|
27
|
-
end
|
28
|
-
|
29
|
-
{
|
30
|
-
' spaces in front and back ' => 'spaces in front and back',
|
31
|
-
"\twe hate tabs!\t" => 'we hate tabs!'
|
32
|
-
}.each do |key, value|
|
33
|
-
it "should normalize '#{key}' to '#{value}'" do
|
34
|
-
Klass.send(:normalize_attribute, key).should == value
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
end
|
39
|
-
|
40
|
-
describe '#normalize_attributes with a block' do
|
41
|
-
|
42
|
-
before do
|
43
|
-
|
44
|
-
class Klass
|
45
|
-
attr_accessor :attribute
|
46
|
-
include AttributeNormalizer
|
47
|
-
normalize_attributes :attribute do |value|
|
48
|
-
value = value.strip.upcase if value.is_a?(String)
|
49
|
-
value = value * 2 if value.is_a?(Fixnum)
|
50
|
-
value = value * 0.5 if value.is_a?(Float)
|
51
|
-
value
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
@object = Klass.new
|
56
|
-
|
57
|
-
end
|
58
|
-
|
59
|
-
{
|
60
|
-
"\tMichael Deering" => 'MICHAEL DEERING',
|
61
|
-
2 => 4,
|
62
|
-
2.0 => 1.0
|
63
|
-
}.each do |key, value|
|
64
|
-
it "should normalize '#{key}' to '#{value}'" do
|
65
|
-
Klass.send(:normalize_attribute, key).should == value
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
end
|
70
|
-
|
71
|
-
describe 'with an instance' do
|
72
|
-
|
73
|
-
before do
|
74
|
-
User.class_eval do
|
75
|
-
normalize_attributes :name
|
76
|
-
end
|
77
|
-
@user = User.new
|
78
|
-
end
|
79
|
-
|
80
|
-
{
|
81
|
-
' spaces in front and back ' => 'spaces in front and back',
|
82
|
-
"\twe hate tabs!\t" => 'we hate tabs!'
|
83
|
-
}.each do |key, value|
|
84
|
-
it "should normalize '#{key}' to '#{value}'" do
|
85
|
-
@user.name = key
|
86
|
-
@user.name.should == value
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
|
-
context 'when another instance of the same saved record has been changed' do
|
91
|
-
|
92
|
-
before do
|
93
|
-
@user = User.create!(:name => 'Jimi Hendrix')
|
94
|
-
@user2 = User.find(@user.id)
|
95
|
-
@user2.update_attributes(:name => 'Thom Yorke')
|
96
|
-
end
|
97
|
-
|
98
|
-
it "should reflect the change when the record is reloaded" do
|
99
|
-
lambda { @user.reload }.should change(@user, :name).from('Jimi Hendrix').to('Thom Yorke')
|
100
|
-
end
|
101
|
-
end
|
102
|
-
|
103
|
-
end
|
104
|
-
|
105
|
-
describe 'normalize_attribute is aliased to normalize_attributes' do
|
106
|
-
before do
|
107
|
-
User.class_eval do
|
108
|
-
normalize_attribute :name
|
109
|
-
end
|
110
|
-
@user = User.new
|
111
|
-
end
|
112
|
-
|
113
|
-
{
|
114
|
-
' spaces in front and back ' => 'spaces in front and back',
|
115
|
-
"\twe hate tabs!\t" => 'we hate tabs!'
|
116
|
-
}.each do |key, value|
|
117
|
-
it "should normalize '#{key}' to '#{value}'" do
|
118
|
-
@user.name = key
|
119
|
-
@user.name.should == value
|
120
|
-
end
|
12
|
+
klass.respond_to?(:normalize_attribute).should be_true
|
121
13
|
end
|
122
14
|
|
123
15
|
end
|
data/spec/book_spec.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require File.dirname(__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(:price).from('$3,450.98').to(3450.98) }
|
8
|
+
|
9
|
+
it { should normalize_attribute(:summary).from('Here is my summary that is a little to long').to('Here is m...') }
|
10
|
+
|
11
|
+
it { should normalize_attribute(:title).from('pick up chicks with magic tricks').to('Pick Up Chicks With Magic Tricks')}
|
12
|
+
|
13
|
+
context 'normalization should not interfear with other hooks and aliases on the attribute assignment' do
|
14
|
+
|
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
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
context 'when another instance of the same saved record has been changed' do
|
26
|
+
|
27
|
+
before do
|
28
|
+
@book = Book.create!(:title => 'Original Title')
|
29
|
+
@book2 = Book.find(@book.id)
|
30
|
+
@book2.update_attributes(:title => 'New Title')
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should reflect the change when the record is reloaded" do
|
34
|
+
lambda { @book.reload }.should change(@book, :title).from('Original Title').to('New Title')
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
data/spec/test_helper.rb
CHANGED
@@ -3,17 +3,55 @@ require 'spec'
|
|
3
3
|
require 'active_support'
|
4
4
|
require 'active_record'
|
5
5
|
|
6
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib')
|
7
|
+
require 'attribute_normalizer'
|
8
|
+
|
9
|
+
AttributeNormalizer.configure do |config|
|
10
|
+
|
11
|
+
config.normalizers[:currency] = lambda do |value, options|
|
12
|
+
value.is_a?(String) ? value.gsub(/[^0-9\.]+/, '') : value
|
13
|
+
end
|
14
|
+
|
15
|
+
config.normalizers[:truncate] = lambda do |text, options|
|
16
|
+
if text.is_a?(String)
|
17
|
+
options.reverse_merge!(:length => 30, :omission => "...")
|
18
|
+
l = options[:length] - options[:omission].mb_chars.length
|
19
|
+
chars = text.mb_chars
|
20
|
+
(chars.length > options[:length] ? chars[0...l] + options[:omission] : text).to_s
|
21
|
+
else
|
22
|
+
text
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
6
28
|
ActiveRecord::Base.establish_connection({ :database => ":memory:", :adapter => 'sqlite3', :timeout => 500 })
|
7
29
|
|
8
30
|
ActiveRecord::Schema.define do
|
9
|
-
create_table :
|
10
|
-
t.string
|
31
|
+
create_table :books, :force => true do |t|
|
32
|
+
t.string :author
|
33
|
+
t.string :isbn
|
34
|
+
t.decimal :price
|
35
|
+
t.string :summary
|
36
|
+
t.string :title
|
11
37
|
end
|
12
38
|
end
|
13
39
|
|
14
|
-
class
|
40
|
+
class Book < ActiveRecord::Base
|
41
|
+
|
42
|
+
normalize_attribute :author
|
43
|
+
|
44
|
+
normalize_attribute :price, :with => :currency
|
45
|
+
|
46
|
+
normalize_attributes :summary, :with => :truncate, :length => 12
|
47
|
+
|
48
|
+
normalize_attributes :title do |value|
|
49
|
+
value.is_a?(String) ? value.titleize.strip : value
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
15
53
|
|
16
54
|
Spec::Runner.configure do |config|
|
55
|
+
config.include AttributeNormalizer::RSpecMatcher, :type => :models
|
17
56
|
end
|
18
57
|
|
19
|
-
$LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib')
|
metadata
CHANGED
@@ -1,7 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: attribute_normalizer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 2
|
8
|
+
- 0
|
9
|
+
version: 0.2.0
|
5
10
|
platform: ruby
|
6
11
|
authors:
|
7
12
|
- Michael Deering
|
@@ -9,7 +14,7 @@ autorequire:
|
|
9
14
|
bindir: bin
|
10
15
|
cert_chain: []
|
11
16
|
|
12
|
-
date: 2010-
|
17
|
+
date: 2010-05-14 00:00:00 -06:00
|
13
18
|
default_executable:
|
14
19
|
dependencies: []
|
15
20
|
|
@@ -30,12 +35,15 @@ files:
|
|
30
35
|
- lib/attribute_normalizer.rb
|
31
36
|
- rails/init.rb
|
32
37
|
- spec/attribute_normalizer_spec.rb
|
38
|
+
- spec/book_spec.rb
|
33
39
|
- spec/test_helper.rb
|
34
40
|
has_rdoc: true
|
35
41
|
homepage: http://github.com/mdeering/attribute_normalizer
|
36
42
|
licenses: []
|
37
43
|
|
38
|
-
post_install_message: "\n
|
44
|
+
post_install_message: "\n\
|
45
|
+
-----------------------------------------------------------------------\n Attribute Normalizer News:\n\n There are lots of changes from the 0.1.2 release to the 0.2.0 release!\n Change log is here http://github.com/mdeering/attribute_normalizer/blob/master/ROADMAP.textile\n Docs have been updated here http://github.com/mdeering/attribute_normalizer\n\n Cheers,\n Michael Deering http://mdeering.com\n\
|
46
|
+
-----------------------------------------------------------------------\n "
|
39
47
|
rdoc_options:
|
40
48
|
- --charset=UTF-8
|
41
49
|
require_paths:
|
@@ -44,21 +52,24 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
44
52
|
requirements:
|
45
53
|
- - ">="
|
46
54
|
- !ruby/object:Gem::Version
|
55
|
+
segments:
|
56
|
+
- 0
|
47
57
|
version: "0"
|
48
|
-
version:
|
49
58
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
59
|
requirements:
|
51
60
|
- - ">="
|
52
61
|
- !ruby/object:Gem::Version
|
62
|
+
segments:
|
63
|
+
- 0
|
53
64
|
version: "0"
|
54
|
-
version:
|
55
65
|
requirements: []
|
56
66
|
|
57
67
|
rubyforge_project:
|
58
|
-
rubygems_version: 1.3.
|
68
|
+
rubygems_version: 1.3.6
|
59
69
|
signing_key:
|
60
70
|
specification_version: 3
|
61
|
-
summary:
|
71
|
+
summary: Attribute normalizer that excepts code blocks.
|
62
72
|
test_files:
|
63
73
|
- spec/attribute_normalizer_spec.rb
|
74
|
+
- spec/book_spec.rb
|
64
75
|
- spec/test_helper.rb
|