attribute_normalizer 1.0.0.pre2 → 1.0.0.pre3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.textile +10 -14
- data/lib/attribute_normalizer/model_inclusions.rb +12 -14
- data/lib/attribute_normalizer/normalizers/blank_normalizer.rb +1 -1
- data/lib/attribute_normalizer/normalizers/phone_normalizer.rb +1 -1
- data/lib/attribute_normalizer/normalizers/squish_normalizer.rb +1 -1
- data/lib/attribute_normalizer/normalizers/strip_normalizer.rb +1 -1
- data/lib/attribute_normalizer.rb +6 -6
- data/spec/models/author.rb +4 -4
- data/spec/models/book.rb +2 -6
- data/spec/test_helper.rb +1 -1
- metadata +7 -6
data/README.textile
CHANGED
@@ -41,7 +41,7 @@ p. Then just required it. Rails usages is as follows.
|
|
41
41
|
h3. Rails 2
|
42
42
|
|
43
43
|
<pre><code># config/environment.rb
|
44
|
-
|
44
|
+
config.gem 'attribute_normalizer'</code></pre>
|
45
45
|
|
46
46
|
h3. Rails 3
|
47
47
|
<pre><code># Gemfile
|
@@ -60,7 +60,7 @@ describe Book do
|
|
60
60
|
it { should normalize_attribute(:author) }
|
61
61
|
it { should normalize_attribute(:price).from('$3,450.98').to(3450.98) }
|
62
62
|
it { should normalize_attribute(:summary).from(' Here is my summary that is a little to long ').to('Here is m...') }
|
63
|
-
it { should normalize_attribute(:title).from(' pick up chicks with magic tricks ').to('Pick Up Chicks With Magic Tricks')}
|
63
|
+
it { should normalize_attribute(:title).from(' pick up chicks with magic tricks ').to('Pick Up Chicks With Magic Tricks') }
|
64
64
|
end</code></pre>
|
65
65
|
|
66
66
|
p. The following normalizers are already included with the +0.3 version of the gem.
|
@@ -90,16 +90,16 @@ AttributeNormalizer.configure do |config|
|
|
90
90
|
end
|
91
91
|
end
|
92
92
|
|
93
|
-
# The default normalizers if no
|
93
|
+
# The default normalizers if no normalizers or block is given is to apply the :strip and :blank normalizers (in that order).
|
94
94
|
# You can change this if you would like as follows:
|
95
|
-
# config.default_normalizers = :strip, :blank
|
95
|
+
# config.default_normalizers = { :strip => true, :blank => true }
|
96
96
|
|
97
|
-
# You can enable the attribute normalizers automatically if the specified attributes exist in your column_names. It will use
|
97
|
+
# You can enable the attribute normalizers automatically if the specified attributes exist in your column_names. It will use
|
98
98
|
# the default normalizers for each attribute (e.g. config.default_normalizers)
|
99
99
|
# config.default_attributes = :name, :title
|
100
100
|
|
101
101
|
# Also, You can add an specific attribute to default_attributes using one or more normalizers:
|
102
|
-
# config.add_default_attribute :name, :
|
102
|
+
# config.add_default_attribute :name, :truncate => true
|
103
103
|
end
|
104
104
|
</code></pre>
|
105
105
|
|
@@ -112,10 +112,10 @@ The _normalize_attributes_ method is eager loaded into your ORM. _normalize_att
|
|
112
112
|
normalize_attributes :author, :publisher
|
113
113
|
|
114
114
|
# Using one of our predefined normalizers.
|
115
|
-
normalize_attribute :price, :
|
115
|
+
normalize_attribute :price, :currency => true
|
116
116
|
|
117
117
|
# Using more then one of our predefined normalizers including one with options
|
118
|
-
normalize_attribute :summary, :
|
118
|
+
normalize_attribute :summary, :strip => true, :truncate => { :length => 12 }
|
119
119
|
|
120
120
|
# You can also define your normalization block inline.
|
121
121
|
normalize_attribute :title do |value|
|
@@ -142,18 +142,14 @@ p. If you are running RSpec there is matcher available for use. Usage can been
|
|
142
142
|
h3. Rails 2
|
143
143
|
|
144
144
|
<pre><code># spec/spec_helper.rb
|
145
|
-
# ...
|
146
145
|
Spec::Runner.configure do |config|
|
147
|
-
|
148
|
-
config.include AttributeNormalizer::RSpecMatcher, :type => :model
|
146
|
+
config.include AttributeNormalizer::RSpecMatcher, :type => :models
|
149
147
|
end</code></pre>
|
150
148
|
|
151
149
|
h3. Rails 3
|
152
150
|
|
153
151
|
<pre><code># spec/spec_helper.rb
|
154
|
-
|
155
|
-
RSpec.configure do |config|
|
156
|
-
# ...
|
152
|
+
Spec::Runner.configure do |config|
|
157
153
|
config.include AttributeNormalizer::RSpecMatcher, :type => :model
|
158
154
|
end</code></pre>
|
159
155
|
|
@@ -6,9 +6,8 @@ module AttributeNormalizer
|
|
6
6
|
|
7
7
|
module ClassMethods
|
8
8
|
def normalize_attributes(*attributes, &block)
|
9
|
-
|
10
|
-
|
11
|
-
normalizers = [options.delete(:with)].flatten.compact
|
9
|
+
normalizers = attributes.last.is_a?(::Hash) ? attributes.pop : {}
|
10
|
+
|
12
11
|
if normalizers.empty? && !block_given?
|
13
12
|
normalizers = AttributeNormalizer.configuration.default_normalizers # the default normalizers
|
14
13
|
end
|
@@ -20,18 +19,17 @@ module AttributeNormalizer
|
|
20
19
|
end
|
21
20
|
else
|
22
21
|
define_method "normalize_#{attribute}" do |value|
|
23
|
-
normalized=value
|
24
|
-
|
25
|
-
|
26
|
-
|
22
|
+
normalized = value
|
23
|
+
|
24
|
+
normalizers.each do |key, options|
|
25
|
+
normalizer = AttributeNormalizer.configuration.normalizers[key]
|
26
|
+
raise AttributeNormalizer::MissingNormalizer.new("No normalizer was found for #{key}") unless normalizer
|
27
|
+
|
28
|
+
normalized = if normalizer.respond_to?(:normalize)
|
29
|
+
normalizer.normalize(normalized, TrueClass === options ? {} : options)
|
30
|
+
else
|
31
|
+
normalizer.call(normalized, TrueClass === options ? {} : options)
|
27
32
|
end
|
28
|
-
|
29
|
-
normalizer=AttributeNormalizer.configuration.normalizers[normalizer_name]
|
30
|
-
raise AttributeNormalizer::MissingNormalizer.new("No normalizer was found for #{normalizer_name}") unless normalizer
|
31
|
-
|
32
|
-
normalized= normalizer.respond_to?(:normalize) ? normalizer.normalize(normalized,options) :
|
33
|
-
normalizer.call(normalized, options)
|
34
|
-
#puts "#{normalizer_name} : ->'#{normalized}' (#{normalized.class})"
|
35
33
|
end
|
36
34
|
normalized
|
37
35
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
module AttributeNormalizer
|
2
2
|
module Normalizers
|
3
3
|
module PhoneNormalizer
|
4
|
-
def self.normalize(value, options)
|
4
|
+
def self.normalize(value, options = {})
|
5
5
|
value = value.is_a?(String) ? value.gsub(/[^0-9]+/, '') : value
|
6
6
|
value.is_a?(String) && value.empty? ? nil : value
|
7
7
|
end
|
data/lib/attribute_normalizer.rb
CHANGED
@@ -10,9 +10,9 @@ module AttributeNormalizer
|
|
10
10
|
class << self
|
11
11
|
attr_accessor :configuration
|
12
12
|
end
|
13
|
-
|
13
|
+
|
14
14
|
def self.configuration
|
15
|
-
@configuration ||= Configuration.new
|
15
|
+
@configuration ||= Configuration.new
|
16
16
|
end
|
17
17
|
|
18
18
|
def self.configure
|
@@ -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
|
28
28
|
end
|
29
29
|
|
30
30
|
def default_attributes=(attributes)
|
31
31
|
[attributes].flatten.each do |attribute|
|
32
|
-
add_default_attribute(attribute,
|
32
|
+
add_default_attribute(attribute, 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, normalizers)
|
37
|
+
@default_attributes[attribute.to_s] = normalizers
|
38
38
|
end
|
39
39
|
|
40
40
|
def initialize
|
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, :squish => true
|
5
|
+
normalize_attribute :first_name, :strip => true
|
6
|
+
normalize_attribute :last_name, :blank => true
|
7
|
+
normalize_attribute :phone_number, :phone => true
|
8
8
|
|
9
9
|
end
|
data/spec/models/book.rb
CHANGED
@@ -2,13 +2,9 @@ class Book < ActiveRecord::Base
|
|
2
2
|
|
3
3
|
normalize_attribute :author
|
4
4
|
|
5
|
-
normalize_attribute :us_price, :cnd_price, :
|
6
|
-
|
7
|
-
normalize_attributes :summary, :with => [ :strip,
|
8
|
-
{ :truncate => { :length => 12 } },
|
9
|
-
:blank
|
10
|
-
]
|
5
|
+
normalize_attribute :us_price, :cnd_price, :currency => true
|
11
6
|
|
7
|
+
normalize_attributes :summary, :strip => true, :truncate => { :length => 12 }, :blank => true
|
12
8
|
|
13
9
|
normalize_attributes :title do |value|
|
14
10
|
value.is_a?(String) ? value.titleize.strip : value
|
data/spec/test_helper.rb
CHANGED
@@ -30,7 +30,7 @@ AttributeNormalizer.configure do |config|
|
|
30
30
|
|
31
31
|
config.default_normalizers = :strip, :special_normalizer, :blank
|
32
32
|
config.default_attributes = :name, :title
|
33
|
-
config.add_default_attribute :authors, :
|
33
|
+
config.add_default_attribute :authors, :strip => true, :blank => true
|
34
34
|
|
35
35
|
end
|
36
36
|
|
metadata
CHANGED
@@ -1,14 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: attribute_normalizer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
5
|
-
prerelease:
|
4
|
+
hash: 1923831951
|
5
|
+
prerelease: 6
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 0
|
9
9
|
- 0
|
10
|
-
-
|
11
|
-
|
10
|
+
- pre
|
11
|
+
- 3
|
12
|
+
version: 1.0.0.pre3
|
12
13
|
platform: ruby
|
13
14
|
authors:
|
14
15
|
- Michael Deering
|
@@ -16,7 +17,7 @@ autorequire:
|
|
16
17
|
bindir: bin
|
17
18
|
cert_chain: []
|
18
19
|
|
19
|
-
date: 2011-
|
20
|
+
date: 2011-03-18 00:00:00 -06:00
|
20
21
|
default_executable:
|
21
22
|
dependencies: []
|
22
23
|
|
@@ -87,7 +88,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
87
88
|
requirements: []
|
88
89
|
|
89
90
|
rubyforge_project:
|
90
|
-
rubygems_version: 1.
|
91
|
+
rubygems_version: 1.5.2
|
91
92
|
signing_key:
|
92
93
|
specification_version: 3
|
93
94
|
summary: Attribute normalizer that excepts code blocks.
|