normatron 0.1.0 → 0.1.1
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.
- data/README.textile +27 -18
- data/lib/normatron.rb +2 -2
- data/lib/normatron/extensions/active_record.rb +10 -5
- data/lib/normatron/filters.rb +12 -0
- data/lib/normatron/filters/conversions.rb +29 -0
- data/lib/normatron/filters/string_inflections.rb +55 -19
- data/spec/lib/normatron/extensions/active_record_spec.rb +90 -55
- data/spec/lib/normatron/filters/conversions_spec.rb +40 -0
- data/spec/lib/normatron/filters/string_inflections_spec.rb +27 -25
- data/spec/lib/normatron/filters_spec.rb +22 -0
- data/spec/spec_helper.rb +5 -2
- metadata +27 -5
data/README.textile
CHANGED
|
@@ -53,7 +53,7 @@ h2. Usage
|
|
|
53
53
|
h3. The default filters
|
|
54
54
|
|
|
55
55
|
Normatron uses *:squish* and *:blank* as default filters.
|
|
56
|
-
These filters are applied to all attributes in *normalize* function, since no options
|
|
56
|
+
These filters are applied to all attributes in *normalize* function, since no options is given.
|
|
57
57
|
|
|
58
58
|
<pre>
|
|
59
59
|
# ./app/models/products.rb
|
|
@@ -64,18 +64,18 @@ end
|
|
|
64
64
|
|
|
65
65
|
$ rails console
|
|
66
66
|
> p = Product.create name: " memory card "
|
|
67
|
-
> p
|
|
68
|
-
=> "memory card"
|
|
69
|
-
>
|
|
70
|
-
>
|
|
71
|
-
=> nil
|
|
67
|
+
> p
|
|
68
|
+
=> #<Product id: nil, name: "memory card", price: nil>
|
|
69
|
+
> q = Product.create name: " "
|
|
70
|
+
> q
|
|
71
|
+
=> #<Product id: nil, name: nil, price: nil>
|
|
72
72
|
</pre>
|
|
73
73
|
|
|
74
74
|
You can also apply the default filters to multiple attributes as following:
|
|
75
75
|
|
|
76
76
|
<pre>normalize :login, :email, :address, :city</pre>
|
|
77
77
|
|
|
78
|
-
h3.
|
|
78
|
+
h3. Changes occur before validation
|
|
79
79
|
|
|
80
80
|
All filters are automatically applyied in the *before_validation* callback.
|
|
81
81
|
So, the changes occur when you call any method that validates de model.
|
|
@@ -106,7 +106,7 @@ It's possible to call the *apply_normalizations* method. That perform the normal
|
|
|
106
106
|
|
|
107
107
|
h3. Specifying filters
|
|
108
108
|
|
|
109
|
-
Just add the *:with* option and pass the
|
|
109
|
+
Just add the *:with* option and pass the filters you want.
|
|
110
110
|
All Symbols outside the *:with* Hash are flagged as an attribute.
|
|
111
111
|
|
|
112
112
|
<pre>normalize :login, :address, :city, :with => :upcase</pre>
|
|
@@ -118,7 +118,7 @@ normalize :login, :address, :city, :with => [:upcase, :blank, :strip]
|
|
|
118
118
|
normalize :email, :with => [:downcase, :blank, :strip]
|
|
119
119
|
</pre>
|
|
120
120
|
|
|
121
|
-
h3.
|
|
121
|
+
h3. Stacking Filters
|
|
122
122
|
|
|
123
123
|
You can stack filters to an attribute calling the *normalize* method multiple times.
|
|
124
124
|
|
|
@@ -139,11 +139,14 @@ end
|
|
|
139
139
|
> u.login = " iKICKasses"
|
|
140
140
|
> u.email = "GMAIL@CHUCKNORRIS.COM"
|
|
141
141
|
> u.valid?
|
|
142
|
+
=> true
|
|
142
143
|
> u
|
|
143
144
|
=> #<User id: nil, name: nil, surname: "NORRIS", login: "iKICKasses", email: "gmail@chucknorris.com">
|
|
144
145
|
</pre>
|
|
145
146
|
|
|
146
|
-
h3.
|
|
147
|
+
h3. Model instance filter
|
|
148
|
+
|
|
149
|
+
Create an instance method returning the value as you want. The method must have an unique mandatory parameter.
|
|
147
150
|
|
|
148
151
|
<pre>
|
|
149
152
|
# ./app/models/client.rb
|
|
@@ -153,27 +156,27 @@ class Client < ActiveRecord::Base
|
|
|
153
156
|
normalize :phone, :with => :my_phone_filter
|
|
154
157
|
|
|
155
158
|
def my_phone_filter(value)
|
|
156
|
-
digits = value.gsub(
|
|
159
|
+
digits = value.gsub(/\D/, '')
|
|
157
160
|
"(%s) %s-%s" % [digits[0..1], digits[2..5], digits[6..9]]
|
|
158
161
|
end
|
|
159
162
|
end
|
|
160
163
|
|
|
161
164
|
$ rails console
|
|
162
|
-
> p = Client.create :name => "Jim Raynor", :phone => "
|
|
165
|
+
> p = Client.create :name => "Jim Raynor", :phone => "!a1b@c2d#e3f$g4h i5j%k6l&m7n*o8p(q9r)s1t "
|
|
163
166
|
> p.phone
|
|
164
167
|
=> "(12) 3456-7891"
|
|
165
168
|
</pre>
|
|
166
169
|
|
|
167
|
-
Create an instance method that returns the value as you want. The method must have an unique mandatory parameter.
|
|
168
|
-
|
|
169
170
|
h2. Filters
|
|
170
171
|
|
|
171
|
-
table
|
|
172
|
+
table.
|
|
172
173
|
|_. FILTER|_. DESCRIPTION|
|
|
174
|
+
|:alphas|Remove all non letter characters|
|
|
173
175
|
|:blank|Convert a blank String into a Nil object|
|
|
174
|
-
|:capitalize|
|
|
176
|
+
|:capitalize|The first character will be uppercased, all others will be lowercased|
|
|
175
177
|
|:dasherize|Replace all underscores with dashes|
|
|
176
|
-
|:
|
|
178
|
+
|:digits|Remove all non digit characters|
|
|
179
|
+
|:downcase|Lowercase all characters on the sequence|
|
|
177
180
|
|:lstrip|Remove trailing spaces|
|
|
178
181
|
|:rstrip|Remove leading spaces|
|
|
179
182
|
|:squeeze|Replace followed occurrences of the same character by a single one|
|
|
@@ -184,6 +187,12 @@ table{border:1px solid black}.
|
|
|
184
187
|
|
|
185
188
|
h1. Changelog
|
|
186
189
|
|
|
190
|
+
- 0.1.1 :=
|
|
191
|
+
* Two new filters: alphas and digits
|
|
192
|
+
* Blank filter extracted from StringInflections to Conversors
|
|
193
|
+
* Fixed bug filter stack to same attribute
|
|
194
|
+
* Nil attributes will be ignored
|
|
195
|
+
=:
|
|
187
196
|
- 0.1.0 :=
|
|
188
197
|
* Two new filters: dasherize and squeeze
|
|
189
198
|
* Native filters replaced by StringInflections
|
|
@@ -205,7 +214,7 @@ h1. Special Thanks
|
|
|
205
214
|
This gem was inspired on work of "fnando (fnando's Github)":https://github.com/fnando called "normalize_attributes (normalize_attributes gem)":https://github.com/fnando/normalize_attributes.
|
|
206
215
|
He have a lot of interesting gems on his repo, please take a look.
|
|
207
216
|
|
|
208
|
-
h1. Licence
|
|
217
|
+
h1. MIT Licence
|
|
209
218
|
|
|
210
219
|
<pre>
|
|
211
220
|
Copyright 2012 Fernando Rodrigues da Silva
|
data/lib/normatron.rb
CHANGED
|
@@ -2,7 +2,7 @@ require "normatron/configuration"
|
|
|
2
2
|
require "normatron/extensions/active_record"
|
|
3
3
|
|
|
4
4
|
module Normatron
|
|
5
|
-
VERSION = "0.1.
|
|
5
|
+
VERSION = "0.1.1"
|
|
6
6
|
|
|
7
7
|
class << self
|
|
8
8
|
def configuration
|
|
@@ -16,4 +16,4 @@ module Normatron
|
|
|
16
16
|
end
|
|
17
17
|
end
|
|
18
18
|
|
|
19
|
-
ActiveRecord::Base.send
|
|
19
|
+
ActiveRecord::Base.send(:include, Normatron::Extensions::ActiveRecord) if defined?(ActiveRecord::Base)
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
require "normatron/filters
|
|
1
|
+
require "normatron/filters"
|
|
2
2
|
|
|
3
3
|
module Normatron
|
|
4
4
|
module Extensions
|
|
@@ -12,13 +12,16 @@ module Normatron
|
|
|
12
12
|
|
|
13
13
|
def normalize(*args)
|
|
14
14
|
options = args.extract_options!
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
return nil unless args
|
|
16
|
+
|
|
17
|
+
options[:with] = [options[:with]].flatten.compact
|
|
18
|
+
options[:with] = Normatron.configuration.default_filters if options[:with].empty?
|
|
17
19
|
|
|
18
20
|
@normalize_options ||= {}
|
|
19
21
|
args.each do |attribute|
|
|
20
22
|
@normalize_options[attribute] ||= []
|
|
21
|
-
@normalize_options[attribute]
|
|
23
|
+
@normalize_options[attribute] += options[:with]
|
|
24
|
+
@normalize_options[attribute] = @normalize_options[attribute].uniq
|
|
22
25
|
end
|
|
23
26
|
@normalize_options
|
|
24
27
|
end
|
|
@@ -33,8 +36,10 @@ module Normatron
|
|
|
33
36
|
filters.each do |filter|
|
|
34
37
|
if self.respond_to? filter
|
|
35
38
|
value = send(filter, value)
|
|
36
|
-
|
|
39
|
+
elsif Normatron::Filters::StringInflections.respond_to? filter
|
|
37
40
|
value = Normatron::Filters::StringInflections.send(filter, value)
|
|
41
|
+
else
|
|
42
|
+
value = Normatron::Filters::Conversions.send(filter, value)
|
|
38
43
|
end
|
|
39
44
|
end
|
|
40
45
|
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
require "normatron/filters/conversions"
|
|
2
|
+
require "normatron/filters/string_inflections"
|
|
3
|
+
|
|
4
|
+
module Normatron
|
|
5
|
+
module Filters
|
|
6
|
+
class << self
|
|
7
|
+
def is_a_string?(value)
|
|
8
|
+
value.is_a?(ActiveSupport::Multibyte::Chars) || value.is_a?(String)
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
module Normatron
|
|
2
|
+
module Filters
|
|
3
|
+
module Conversions
|
|
4
|
+
class << self
|
|
5
|
+
|
|
6
|
+
##
|
|
7
|
+
# Convert a blank string on a nil object.
|
|
8
|
+
#
|
|
9
|
+
# @example
|
|
10
|
+
# StringInflections.blank("") #=> nil
|
|
11
|
+
# StringInflections.blank(" ") #=> nil
|
|
12
|
+
# StringInflections.blank("\n") #=> nil
|
|
13
|
+
# StringInflections.blank("1") #=> "1"
|
|
14
|
+
# StringInflections.blank(0) #=> 0
|
|
15
|
+
# @param [String] value Any character sequence
|
|
16
|
+
# @return [String, nil] The object itself or nil
|
|
17
|
+
#
|
|
18
|
+
# @see http://api.rubyonrails.org/classes/String.html#method-i-blank-3F ActiveSupport::CoreExt::Object#blank?
|
|
19
|
+
def blank(value)
|
|
20
|
+
if Filters.is_a_string?(value)
|
|
21
|
+
value.to_s.blank? ? nil : value
|
|
22
|
+
else
|
|
23
|
+
value
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
@@ -1,33 +1,39 @@
|
|
|
1
|
-
require "active_support/core_ext/string"
|
|
2
|
-
|
|
3
1
|
module Normatron
|
|
4
2
|
module Filters
|
|
3
|
+
##
|
|
4
|
+
# Contains methods that perform modifications on Strings.
|
|
5
|
+
# In general, modifications are only for removal and/or character replacement.
|
|
6
|
+
# All methods take String or ActiveSupport::Multibyte::Chars variables as main argument.
|
|
7
|
+
# Where character case need to be changed, all accented characters will also be affected.
|
|
8
|
+
# There is no type coersion, ie all filters return the same object type passed by the argument.
|
|
9
|
+
#
|
|
10
|
+
# @author Fernando Rodrigues da Silva
|
|
5
11
|
module StringInflections
|
|
6
12
|
class << self
|
|
7
13
|
|
|
8
14
|
##
|
|
9
|
-
#
|
|
15
|
+
# Remove all non letter characters.
|
|
10
16
|
#
|
|
11
17
|
# @example
|
|
12
|
-
# StringInflections.
|
|
13
|
-
# StringInflections.
|
|
14
|
-
# StringInflections.
|
|
15
|
-
# StringInflections.
|
|
16
|
-
# StringInflections.blank(0) #=> 0
|
|
18
|
+
# StringInflections.alphas("Doom 3") #=> "Doom"
|
|
19
|
+
# StringInflections.alphas("\n") #=> ""
|
|
20
|
+
# StringInflections.alphas("1") #=> ""
|
|
21
|
+
# StringInflections.alphas(0) #=> 0
|
|
17
22
|
# @param [String] value Any character sequence
|
|
18
|
-
# @return [String, nil] The object itself or
|
|
23
|
+
# @return [String, nil] The object itself or a alpha characters sequence
|
|
19
24
|
#
|
|
20
|
-
# @see http://
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
25
|
+
# @see http://www.ruby-doc.org/core-1.9.3/Regexp.html Regexp
|
|
26
|
+
# @see http://www.ruby-doc.org/core-1.9.3/String.html#method-i-gsub String#gsub
|
|
27
|
+
def alphas(value)
|
|
28
|
+
if Filters.is_a_string?(value)
|
|
29
|
+
value.gsub(/[^\p{L}]/u, '')
|
|
24
30
|
else
|
|
25
31
|
value
|
|
26
32
|
end
|
|
27
33
|
end
|
|
28
34
|
|
|
29
35
|
##
|
|
30
|
-
#
|
|
36
|
+
# The first character will be uppercased, all others will be lowercased.
|
|
31
37
|
#
|
|
32
38
|
# @example
|
|
33
39
|
# StringInflections.capitalize("mASTER OF PUPPETS") #=> "Master of puppets"
|
|
@@ -55,6 +61,40 @@ module Normatron
|
|
|
55
61
|
evaluate(:dasherize, value)
|
|
56
62
|
end
|
|
57
63
|
|
|
64
|
+
##
|
|
65
|
+
# Remove all non digit characters.
|
|
66
|
+
#
|
|
67
|
+
# @example
|
|
68
|
+
# StringInflections.digits("Quake 3") #=> "3"
|
|
69
|
+
# StringInflections.digits("\n") #=> ""
|
|
70
|
+
# StringInflections.digits("1") #=> "1"
|
|
71
|
+
# StringInflections.digits(0) #=> 0
|
|
72
|
+
# @param [String] value Any character sequence
|
|
73
|
+
# @return [String, nil] The object itself or a digit characters sequence
|
|
74
|
+
#
|
|
75
|
+
# @see http://www.ruby-doc.org/core-1.9.3/Regexp.html Regexp
|
|
76
|
+
# @see http://www.ruby-doc.org/core-1.9.3/String.html#method-i-gsub String#gsub
|
|
77
|
+
def digits(value)
|
|
78
|
+
if Filters.is_a_string?(value)
|
|
79
|
+
value.gsub(/\D/, '')
|
|
80
|
+
else
|
|
81
|
+
value
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
##
|
|
86
|
+
# Lowercase all characters on the sequence.
|
|
87
|
+
#
|
|
88
|
+
# @example
|
|
89
|
+
# StringInflections.downcase("KILL'EM ALL") #=> "kill'em all"
|
|
90
|
+
# StringInflections.downcase("ÊXITO") #=> "êxito"
|
|
91
|
+
# StringInflections.downcase("1") #=> "1"
|
|
92
|
+
# StringInflections.downcase(0) #=> 0
|
|
93
|
+
# @param [String] value Any character sequence
|
|
94
|
+
# @return [String, nil] The object itself or a downcased string
|
|
95
|
+
#
|
|
96
|
+
# @see http://api.rubyonrails.org/classes/ActiveSupport/Multibyte/Chars.html#method-i-downcase ActiveSupport::Multibyte::Chars#downcase
|
|
97
|
+
# @see http://www.ruby-doc.org/core-1.9.3/String.html#method-i-downcase String#downcase
|
|
58
98
|
def downcase(value)
|
|
59
99
|
evaluate(:downcase, value)
|
|
60
100
|
end
|
|
@@ -92,7 +132,7 @@ module Normatron
|
|
|
92
132
|
def evaluate(method_name, value)
|
|
93
133
|
if need_type_cast?(method_name, value)
|
|
94
134
|
value.mb_chars.send(method_name)
|
|
95
|
-
elsif is_a_string?(value)
|
|
135
|
+
elsif Filters.is_a_string?(value)
|
|
96
136
|
value.send(method_name)
|
|
97
137
|
else
|
|
98
138
|
value
|
|
@@ -109,10 +149,6 @@ module Normatron
|
|
|
109
149
|
false
|
|
110
150
|
end
|
|
111
151
|
end
|
|
112
|
-
|
|
113
|
-
def is_a_string?(value)
|
|
114
|
-
value.is_a?(ActiveSupport::Multibyte::Chars) || value.is_a?(String)
|
|
115
|
-
end
|
|
116
152
|
end
|
|
117
153
|
end
|
|
118
154
|
end
|
|
@@ -7,73 +7,108 @@ describe Normatron::Extensions::ActiveRecord do
|
|
|
7
7
|
model_class.normalize_options = {}
|
|
8
8
|
end
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
10
|
+
describe :normalize do
|
|
11
|
+
pending "should remove nil normalization" do
|
|
12
|
+
pending "attributes"
|
|
13
|
+
pending "filters"
|
|
14
|
+
end
|
|
15
|
+
it "should remove nil normalization filters" do
|
|
16
|
+
model_class.normalize(:name, :with => [:a, :a, :b, :c, :c, nil])
|
|
17
|
+
model_class.normalize_options.should == {:name => [:a, :b, :c]}
|
|
14
18
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
+
model_class.normalize_options = {}
|
|
20
|
+
model_class.normalize(:name, :with => [nil])
|
|
21
|
+
model_class.normalize_options.should == {:name => [:squish, :blank]}
|
|
22
|
+
end
|
|
19
23
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
end
|
|
24
|
+
it "should remove repeated normalization filters" do
|
|
25
|
+
model_class.normalize(:name, :with => [:a, :a, :b, :c, :c])
|
|
26
|
+
model_class.normalize_options.should == {:name => [:a, :b, :c]}
|
|
24
27
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
28
|
+
model_class.normalize(:name, :with => [:a, :d, :e])
|
|
29
|
+
model_class.normalize_options.should == {:name => [:a, :b, :c, :d, :e]}
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
it "should stack repeated normalization attributes" do
|
|
33
|
+
model_class.normalize(:name, :name, :with => [:a, :b, :c])
|
|
34
|
+
model_class.normalize_options.should == {:name => [:a, :b, :c]}
|
|
35
|
+
|
|
36
|
+
model_class.normalize(:name, :with => [:d, :e])
|
|
37
|
+
model_class.normalize_options.should == {:name => [:a, :b, :c, :d, :e]}
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
context "with single normalization attribute" do
|
|
41
|
+
it "should bind to a single attribute" do
|
|
42
|
+
model_class.normalize(:name, with: :a)
|
|
43
|
+
model_class.normalize_options.should == {:name => [:a]}
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
it "should bind to multiple attributes" do
|
|
47
|
+
model_class.normalize(:name, :cpf, with: [:a])
|
|
48
|
+
model_class.normalize_options.should == {:name => [:a], cpf: [:a]}
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
context "with multiple normalization attributes" do
|
|
53
|
+
it "should bind to a single attribute" do
|
|
54
|
+
model_class.normalize(:name, with: [:a, :b])
|
|
55
|
+
model_class.normalize_options.should == {:name => [:a, :b]}
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
it "should bind to multiple attributes" do
|
|
59
|
+
model_class.normalize(:name, :cpf, with: [:a, :b])
|
|
60
|
+
model_class.normalize_options.should == {:name => [:a, :b], cpf: [:a, :b]}
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
context "when :with isn't present" do
|
|
65
|
+
describe "default filters" do
|
|
66
|
+
it "should bind to a single attribute" do
|
|
67
|
+
model_class.normalize(:name)
|
|
68
|
+
model_class.normalize_options.should == {:name => [:squish, :blank]}
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
it "should bind to multiple attributes" do
|
|
72
|
+
model_class.normalize(:name, :phone)
|
|
73
|
+
model_class.normalize_options.should == {:name => [:squish, :blank], phone: [:squish, :blank]}
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|
|
29
77
|
|
|
30
|
-
it "should include default filters" do
|
|
31
|
-
model_class.normalize(:name)
|
|
32
|
-
model_class.normalize_options.should include(name: [:squish, :blank])
|
|
33
78
|
end
|
|
34
79
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
80
|
+
describe :apply_normalizations do
|
|
81
|
+
it "should apply Conversion normalizations" do
|
|
82
|
+
model_class.normalize(:name, :with => :blank)
|
|
83
|
+
instance = model_class.new name: " "
|
|
84
|
+
instance.apply_normalizations
|
|
85
|
+
instance.name.should be_nil
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
it "should apply StringInflections normalizations" do
|
|
89
|
+
model_class.normalize(:name, :with => :upcase)
|
|
90
|
+
instance = model_class.new name: "a"
|
|
91
|
+
instance.apply_normalizations
|
|
92
|
+
instance.name.should eq "A"
|
|
93
|
+
end
|
|
47
94
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
95
|
+
it "should apply instance methods normalizations" do
|
|
96
|
+
class Client < ActiveRecord::Base
|
|
97
|
+
def my_filter(value)
|
|
98
|
+
value.split(//) * "-"
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
model_class.normalize(:name, :with => :my_filter)
|
|
103
|
+
instance = model_class.new name: "aaa"
|
|
104
|
+
instance.apply_normalizations
|
|
105
|
+
instance.name.should eq "a-a-a"
|
|
106
|
+
end
|
|
53
107
|
end
|
|
54
108
|
|
|
55
|
-
pending "should apply default normalizations when :with is ommited"
|
|
56
|
-
pending "should apply instance methods normalizations"
|
|
57
109
|
pending "should apply blocks normalizations"
|
|
58
110
|
pending "should apply modules normalizations"
|
|
59
111
|
pending "should apply configuration blocks normalizations"
|
|
60
|
-
|
|
61
|
-
pending "should set default normalization filters" do
|
|
62
|
-
pending "to single attribute"
|
|
63
|
-
pending "to multiple attributes"
|
|
64
|
-
end
|
|
65
|
-
pending "should set single normalization filter" do
|
|
66
|
-
pending "to single attribute"
|
|
67
|
-
pending "to multiple attributes"
|
|
68
|
-
end
|
|
69
|
-
pending "should set multiple normalization filters" do
|
|
70
|
-
pending "to single attribute"
|
|
71
|
-
pending "to multiple attributes"
|
|
72
|
-
end
|
|
73
|
-
pending "should remove repeated normalization" do
|
|
74
|
-
pending "attributes"
|
|
75
|
-
pending "filters"
|
|
76
|
-
end
|
|
77
112
|
pending "should remove nil normalization" do
|
|
78
113
|
pending "attributes"
|
|
79
114
|
pending "filters"
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# encoding: UTF-8
|
|
2
|
+
require "spec_helper"
|
|
3
|
+
|
|
4
|
+
describe Normatron::Filters::Conversions do
|
|
5
|
+
|
|
6
|
+
let(:mod) { Normatron::Filters::Conversions }
|
|
7
|
+
let(:val) { @value }
|
|
8
|
+
|
|
9
|
+
describe :blank do
|
|
10
|
+
it "should return nil for empty strings" do
|
|
11
|
+
value = ""
|
|
12
|
+
mod.blank(value).should be_nil
|
|
13
|
+
mod.blank(value.mb_chars).should be_nil
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
it "should return nil for blank spaces strings" do
|
|
17
|
+
value = " "
|
|
18
|
+
mod.blank(value).should be_nil
|
|
19
|
+
mod.blank(value.mb_chars).should be_nil
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
it 'should return nil for \n \t \r \f strings' do
|
|
23
|
+
value = "\n \t \r \f"
|
|
24
|
+
mod.blank(value).should be_nil
|
|
25
|
+
mod.blank(value.mb_chars).should be_nil
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
it "should not affect filled string" do
|
|
29
|
+
value = "baCon"
|
|
30
|
+
expected = "baCon"
|
|
31
|
+
mod.blank(value).should eq expected
|
|
32
|
+
mod.blank(value.mb_chars).should eq expected.mb_chars
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
it "should not affect non string objects" do
|
|
36
|
+
mod.blank(nil).should eq nil
|
|
37
|
+
mod.blank(1).should eq 1
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
# encoding: UTF-8
|
|
2
|
-
|
|
3
2
|
require "spec_helper"
|
|
4
3
|
|
|
5
4
|
describe Normatron::Filters::StringInflections do
|
|
@@ -7,35 +6,24 @@ describe Normatron::Filters::StringInflections do
|
|
|
7
6
|
let(:mod) { Normatron::Filters::StringInflections }
|
|
8
7
|
let(:val) { @value }
|
|
9
8
|
|
|
10
|
-
describe :
|
|
11
|
-
it "should
|
|
12
|
-
value = ""
|
|
13
|
-
|
|
14
|
-
mod.
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
it "should return nil for blank spaces strings" do
|
|
18
|
-
value = " "
|
|
19
|
-
mod.blank(value).should be_nil
|
|
20
|
-
mod.blank(value.mb_chars).should be_nil
|
|
9
|
+
describe :alphas do
|
|
10
|
+
it "should remove the number and space" do
|
|
11
|
+
value = "Doom 3"
|
|
12
|
+
expected = "Doom"
|
|
13
|
+
mod.alphas(value).should eq expected
|
|
14
|
+
mod.alphas(value.mb_chars).should eq expected.mb_chars
|
|
21
15
|
end
|
|
22
16
|
|
|
23
|
-
it
|
|
24
|
-
value = "\n
|
|
25
|
-
|
|
26
|
-
mod.
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
it "should not affect filled string" do
|
|
30
|
-
value = "baCon"
|
|
31
|
-
expected = "baCon"
|
|
32
|
-
mod.blank(value).should eq expected
|
|
33
|
-
mod.blank(value.mb_chars).should eq expected.mb_chars
|
|
17
|
+
it "should remove line break" do
|
|
18
|
+
value = " \n "
|
|
19
|
+
expected = ""
|
|
20
|
+
mod.alphas(value).should eq expected
|
|
21
|
+
mod.alphas(value.mb_chars).should eq expected.mb_chars
|
|
34
22
|
end
|
|
35
23
|
|
|
36
24
|
it "should not affect non string objects" do
|
|
37
|
-
mod.
|
|
38
|
-
mod.
|
|
25
|
+
mod.alphas(nil).should eq nil
|
|
26
|
+
mod.alphas(1).should eq 1
|
|
39
27
|
end
|
|
40
28
|
end
|
|
41
29
|
|
|
@@ -79,6 +67,20 @@ describe Normatron::Filters::StringInflections do
|
|
|
79
67
|
end
|
|
80
68
|
end
|
|
81
69
|
|
|
70
|
+
describe :digits do
|
|
71
|
+
it "should remove non digit characters" do
|
|
72
|
+
value = "Quake 3"
|
|
73
|
+
expected = "3"
|
|
74
|
+
mod.digits(value).should eq expected
|
|
75
|
+
mod.digits(value.mb_chars).should eq expected.mb_chars
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
it "should not affect non string objects" do
|
|
79
|
+
mod.digits(nil).should eq nil
|
|
80
|
+
mod.digits(1).should eq 1
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
|
|
82
84
|
describe :downcase do
|
|
83
85
|
it "should downcase all characters" do
|
|
84
86
|
value = "KILL'EM ALL"
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# encoding: UTF-8
|
|
2
|
+
require "spec_helper"
|
|
3
|
+
|
|
4
|
+
describe Normatron::Filters do
|
|
5
|
+
describe :is_a_string? do
|
|
6
|
+
it "should be true Multibyte::Chars object" do
|
|
7
|
+
subject.is_a_string?("".mb_chars).should be_true
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
it "should be true String object" do
|
|
11
|
+
subject.is_a_string?("").should be_true
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
it "should not be true nil object" do
|
|
15
|
+
subject.is_a_string?(nil).should be_false
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
it "should not be true Integer object" do
|
|
19
|
+
subject.is_a_string?(1).should be_false
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
data/spec/spec_helper.rb
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
|
-
require "
|
|
1
|
+
require "active_support/core_ext/string"
|
|
2
2
|
require "active_record"
|
|
3
3
|
require "normatron"
|
|
4
4
|
|
|
5
5
|
ActiveRecord::Base.establish_connection :adapter => "sqlite3", :database => ":memory:"
|
|
6
|
-
|
|
6
|
+
|
|
7
|
+
Dir[File.dirname(__FILE__) + "/support/**/*.rb"].each { |f| require f }
|
|
8
|
+
|
|
9
|
+
ActiveRecord::Base.send(:include, Normatron::Extensions::ActiveRecord) if defined?(ActiveRecord::Base)
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: normatron
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.1
|
|
5
5
|
prerelease:
|
|
6
6
|
platform: ruby
|
|
7
7
|
authors:
|
|
@@ -9,16 +9,16 @@ authors:
|
|
|
9
9
|
autorequire:
|
|
10
10
|
bindir: bin
|
|
11
11
|
cert_chain: []
|
|
12
|
-
date: 2012-09-
|
|
12
|
+
date: 2012-09-16 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
|
14
14
|
- !ruby/object:Gem::Dependency
|
|
15
|
-
name:
|
|
15
|
+
name: activerecord
|
|
16
16
|
requirement: !ruby/object:Gem::Requirement
|
|
17
17
|
none: false
|
|
18
18
|
requirements:
|
|
19
19
|
- - ! '>='
|
|
20
20
|
- !ruby/object:Gem::Version
|
|
21
|
-
version: 3.
|
|
21
|
+
version: 3.2.0
|
|
22
22
|
type: :runtime
|
|
23
23
|
prerelease: false
|
|
24
24
|
version_requirements: !ruby/object:Gem::Requirement
|
|
@@ -26,7 +26,23 @@ dependencies:
|
|
|
26
26
|
requirements:
|
|
27
27
|
- - ! '>='
|
|
28
28
|
- !ruby/object:Gem::Version
|
|
29
|
-
version: 3.
|
|
29
|
+
version: 3.2.0
|
|
30
|
+
- !ruby/object:Gem::Dependency
|
|
31
|
+
name: activesupport
|
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
|
33
|
+
none: false
|
|
34
|
+
requirements:
|
|
35
|
+
- - ! '>='
|
|
36
|
+
- !ruby/object:Gem::Version
|
|
37
|
+
version: 3.2.0
|
|
38
|
+
type: :runtime
|
|
39
|
+
prerelease: false
|
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
41
|
+
none: false
|
|
42
|
+
requirements:
|
|
43
|
+
- - ! '>='
|
|
44
|
+
- !ruby/object:Gem::Version
|
|
45
|
+
version: 3.2.0
|
|
30
46
|
- !ruby/object:Gem::Dependency
|
|
31
47
|
name: sqlite3
|
|
32
48
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -69,7 +85,9 @@ executables: []
|
|
|
69
85
|
extensions: []
|
|
70
86
|
extra_rdoc_files: []
|
|
71
87
|
files:
|
|
88
|
+
- lib/normatron/filters.rb
|
|
72
89
|
- lib/normatron/configuration.rb
|
|
90
|
+
- lib/normatron/filters/conversions.rb
|
|
73
91
|
- lib/normatron/filters/string_inflections.rb
|
|
74
92
|
- lib/normatron/extensions/active_record.rb
|
|
75
93
|
- lib/normatron.rb
|
|
@@ -78,6 +96,8 @@ files:
|
|
|
78
96
|
- README.textile
|
|
79
97
|
- spec/spec_helper.rb
|
|
80
98
|
- spec/lib/normatron_spec.rb
|
|
99
|
+
- spec/lib/normatron/filters_spec.rb
|
|
100
|
+
- spec/lib/normatron/filters/conversions_spec.rb
|
|
81
101
|
- spec/lib/normatron/filters/string_inflections_spec.rb
|
|
82
102
|
- spec/lib/normatron/configuration_spec.rb
|
|
83
103
|
- spec/lib/normatron/extensions/active_record_spec.rb
|
|
@@ -110,6 +130,8 @@ summary: Normalize attributes for ActiveRecord objects.
|
|
|
110
130
|
test_files:
|
|
111
131
|
- spec/spec_helper.rb
|
|
112
132
|
- spec/lib/normatron_spec.rb
|
|
133
|
+
- spec/lib/normatron/filters_spec.rb
|
|
134
|
+
- spec/lib/normatron/filters/conversions_spec.rb
|
|
113
135
|
- spec/lib/normatron/filters/string_inflections_spec.rb
|
|
114
136
|
- spec/lib/normatron/configuration_spec.rb
|
|
115
137
|
- spec/lib/normatron/extensions/active_record_spec.rb
|