normatron 0.0.4 → 0.0.5
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.rdoc +208 -8
- data/lib/normatron.rb +1 -1
- data/lib/normatron/active_record.rb +16 -25
- data/lib/normatron/{conversors.rb → filters.rb} +23 -36
- data/lib/normatron/version.rb +1 -1
- data/spec/filters_spec.rb +77 -0
- data/spec/normatron_spec.rb +51 -113
- metadata +5 -3
data/README.rdoc
CHANGED
@@ -1,31 +1,231 @@
|
|
1
1
|
= Normatron
|
2
2
|
|
3
3
|
Normatron is a attribute normalizer for ActiveRecord.
|
4
|
+
With it you can convert attributes to the desired format before saving them in the database.
|
5
|
+
This gem inhibits the work of having to override attributes or create a specific method to perform the normalization.
|
4
6
|
|
5
7
|
= Installation
|
6
8
|
|
7
|
-
|
9
|
+
Let the bundler install the gem by adding the following into your application gemfile:
|
8
10
|
|
9
11
|
gem 'normatron'
|
10
12
|
|
11
|
-
|
13
|
+
And then bundle it up:
|
12
14
|
|
13
15
|
$ bundle install
|
14
16
|
|
17
|
+
Or install it by yourself:
|
18
|
+
|
19
|
+
$ gem install normatron
|
20
|
+
|
21
|
+
= The problem
|
22
|
+
|
23
|
+
I have a product model as the following:
|
24
|
+
|
25
|
+
# ./db/migrate/20120101010000_create_products.rb
|
26
|
+
|
27
|
+
class CreateProducts < ActiveRecord::Migration
|
28
|
+
def change
|
29
|
+
create_table :products do |t|
|
30
|
+
t.string :name
|
31
|
+
t.decimal :price, :precision => 10, :scale => 2
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
# ./app/models/products.rb
|
37
|
+
|
38
|
+
class Product < ActiveRecord::Base
|
39
|
+
attr_accessible :name, :price
|
40
|
+
end
|
41
|
+
|
42
|
+
And I want the product name to be uppercased before save.
|
43
|
+
I can do it basically by two ways:
|
44
|
+
|
45
|
+
== a. Overriding the 'name' method
|
46
|
+
|
47
|
+
# ./app/models/products.rb
|
48
|
+
|
49
|
+
class Product < ActiveRecord::Base
|
50
|
+
attr_accessible :name, :price
|
51
|
+
|
52
|
+
def name=(value)
|
53
|
+
super value.mb_chars.upcase
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
== b. Using the before_validation callback
|
58
|
+
|
59
|
+
# ./app/models/products.rb
|
60
|
+
|
61
|
+
class Product < ActiveRecord::Base
|
62
|
+
attr_accessible :name, :price
|
63
|
+
|
64
|
+
before_validation :do_upcase
|
65
|
+
|
66
|
+
def do_upcase
|
67
|
+
write_attribute :name, name.mb_chars.upcase
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
Both ways are ilenegant, boring, error prone and expensive.
|
72
|
+
Which led me to use a third method:
|
73
|
+
|
15
74
|
= Usage
|
16
75
|
|
17
|
-
|
76
|
+
# ./app/models/products.rb
|
77
|
+
|
78
|
+
class Product < ActiveRecord::Base
|
79
|
+
attr_accessible :name, :price
|
80
|
+
|
81
|
+
normalize :name, :with => :upcase
|
82
|
+
end
|
83
|
+
|
84
|
+
$ rails console
|
85
|
+
> p = Product.create :name => " memory card "
|
86
|
+
> p.name
|
87
|
+
=> " MEMORY CARD "
|
88
|
+
|
89
|
+
You can assign multiple attributes to the same filter:
|
90
|
+
|
91
|
+
# ./app/models/products.rb
|
92
|
+
|
93
|
+
class Product < ActiveRecord::Base
|
94
|
+
attr_accessible :name, :description, :brand, :price
|
95
|
+
|
96
|
+
normalize :name, :brand, :with => :capitalize
|
97
|
+
end
|
98
|
+
|
99
|
+
$ rails console
|
100
|
+
> p = Product.create :name => "memory card 4 GB ", :brand => "OEM"
|
101
|
+
> p.name
|
102
|
+
=> "Memory card 4 GB "
|
103
|
+
> p.brand
|
104
|
+
=> "Oem"
|
105
|
+
|
106
|
+
And/or assign multiple filters to multiple attributes:
|
107
|
+
|
108
|
+
# ./app/models/products.rb
|
18
109
|
|
19
|
-
app/models/product.rb
|
20
110
|
class Product < ActiveRecord::Base
|
111
|
+
attr_accessible :name, :description, :brand, :price
|
112
|
+
|
113
|
+
normalize :name, :description, :brand, :with => [:upcase, :squish, :nillify]
|
114
|
+
end
|
115
|
+
|
116
|
+
$ rails console
|
117
|
+
> p = Product.create :name => " memory card 4 GB ", :description => " ", :brand => "oem "
|
118
|
+
> p.name
|
119
|
+
=> "MEMORY CARD 4 GB"
|
120
|
+
> p.brand
|
121
|
+
=> "OEM"
|
122
|
+
> p.description
|
123
|
+
=> nil
|
124
|
+
|
125
|
+
= Filters
|
126
|
+
== :upcase and :downcase
|
127
|
+
Change all character to upcase and downcase, respectively.
|
128
|
+
Accented characters are affected too.
|
129
|
+
|
130
|
+
class User < ActiveRecord::Base
|
21
131
|
attr_accessible :name
|
22
132
|
|
23
133
|
normalize :name, :with => :upcase
|
24
134
|
end
|
25
135
|
|
26
136
|
$ rails console
|
27
|
-
>
|
28
|
-
>
|
29
|
-
|
137
|
+
> d = User.create :name => "Darth Vader"
|
138
|
+
> d.name
|
139
|
+
=> "DARTH VADER"
|
140
|
+
> j = User.create :name => " José Negrão "
|
141
|
+
> j.name
|
142
|
+
=> " JOSÉ NEGRÃO "
|
143
|
+
|
144
|
+
== :capitalize
|
145
|
+
Capitalize the first character of the sequence.
|
146
|
+
Accented characters are affected too.
|
147
|
+
|
148
|
+
class Player < ActiveRecord::Base
|
149
|
+
attr_accessible :name, :race
|
150
|
+
|
151
|
+
normalize :name, :race, :with => :capitalize
|
152
|
+
end
|
153
|
+
|
154
|
+
$ rails console
|
155
|
+
> a = Player.create :name => "ÉBANO PEREIRA", :race => " protoss"
|
156
|
+
> a.name
|
157
|
+
=> "Ébano pereira"
|
158
|
+
> a.race
|
159
|
+
=> " protoss"
|
160
|
+
|
161
|
+
== :titlecase or :titleize
|
162
|
+
Capitalize the first character of each word.
|
163
|
+
Accented characters are affected too.
|
164
|
+
|
165
|
+
class Player < ActiveRecord::Base
|
166
|
+
attr_accessible :name, :race
|
167
|
+
|
168
|
+
normalize :name, :race, :with => :titleize
|
169
|
+
end
|
170
|
+
|
171
|
+
$ rails console
|
172
|
+
> a = Player.create :name => "fernando rodrigues", :race => " zerg"
|
173
|
+
> a.name
|
174
|
+
=> "Fernando Rodrigues"
|
175
|
+
> a.race
|
176
|
+
=> " Zerg"
|
177
|
+
|
178
|
+
== :strip, :lstrip and :rstrip
|
179
|
+
Remove trailing and/or leading spaces.
|
180
|
+
|
181
|
+
class Address < ActiveRecord::Base
|
182
|
+
attr_accessible :city, :district, :street
|
183
|
+
|
184
|
+
normalize :city, :with => :strip
|
185
|
+
normalize :district, :with => :lstrip
|
186
|
+
normalize :street, :with => :rstrip
|
187
|
+
end
|
188
|
+
|
189
|
+
$ rails console
|
190
|
+
> a = Address.create :city => " gotham ", :district => " red light ", :street => " no name "
|
191
|
+
> a.city
|
192
|
+
=> "gotham"
|
193
|
+
> a.district
|
194
|
+
=> "red light "
|
195
|
+
> a.street
|
196
|
+
=> " no name"
|
197
|
+
|
198
|
+
== :squish
|
199
|
+
Remove trailing and leading spaces and then changing remaining consecutive whitespace groups into one space each.
|
200
|
+
|
201
|
+
class Product < ActiveRecord::Base
|
202
|
+
attr_accessible :description
|
203
|
+
|
204
|
+
normalize :description, :with => :squish
|
205
|
+
end
|
206
|
+
|
207
|
+
$ rails console
|
208
|
+
> p = Product.create :description => " \t My product is \n cheap! ! ! "
|
209
|
+
> p.description
|
210
|
+
=> "My product is cheap!!!"
|
211
|
+
|
212
|
+
== :nillify, :nullify or :nil
|
213
|
+
Convert a blank attribute into nil.
|
214
|
+
|
215
|
+
class Person < ActiveRecord::Base
|
216
|
+
attr_accessible :gender, :name
|
217
|
+
|
218
|
+
normalize :gender, :with => :nil
|
219
|
+
end
|
220
|
+
|
221
|
+
$ rails console
|
222
|
+
> p = Person.create :gender => " ", :name => " "
|
223
|
+
> p.gender
|
224
|
+
=> nil
|
30
225
|
> p.name
|
31
|
-
=> "
|
226
|
+
=> " "
|
227
|
+
|
228
|
+
= Special Thanks
|
229
|
+
This gem was inspired on work of {fnando}[https://github.com/fnando] called {normalize_attributes}[https://github.com/fnando/normalize_attributes].
|
230
|
+
I just forked his gem and added some functionalities.
|
231
|
+
He has a lot of interesting gems on his repo, please take a look.
|
data/lib/normatron.rb
CHANGED
@@ -16,53 +16,45 @@ module Normatron
|
|
16
16
|
end
|
17
17
|
|
18
18
|
module ClassMethods
|
19
|
-
|
20
|
-
# Args uses the attribute names following by a hash with conversion options.
|
21
|
-
#
|
22
|
-
# Example 1:
|
23
|
-
# normalize :attribute_name
|
24
|
-
#
|
25
|
-
# Example 2:
|
26
|
-
# normalize :attribute_name, :with => :upcase
|
27
|
-
#
|
28
|
-
# Example 3:
|
29
|
-
# normalize :attribute_name, :with => [:squish, :downcase]
|
19
|
+
|
30
20
|
def normalize(*args)
|
31
21
|
# Extract options
|
32
22
|
@normalization_options ||= {}
|
33
23
|
options = args.extract_options!
|
34
24
|
|
35
|
-
# Set
|
36
|
-
|
25
|
+
# Set callbacks
|
26
|
+
filters = []
|
37
27
|
if options.empty? # Default standardization
|
38
|
-
|
28
|
+
filters << [:squish, :nillify]
|
39
29
|
elsif options.has_key? :with
|
40
|
-
|
30
|
+
filters << options[:with]
|
41
31
|
else
|
42
32
|
raise "Wrong normalization key in #{self.name}, use :with instead of #{options.keys.first}"
|
43
33
|
end
|
44
34
|
|
45
35
|
# Make a prettier array
|
46
|
-
|
47
|
-
|
36
|
+
filters = filters.flatten.compact
|
37
|
+
filters.map! { |v| v = v.to_sym }
|
48
38
|
|
49
|
-
# Check
|
50
|
-
|
51
|
-
unless
|
52
|
-
raise "Normalization
|
39
|
+
# Check filters
|
40
|
+
filters.each do |f|
|
41
|
+
unless Filters::NAMES.include? f
|
42
|
+
raise "Normalization filter '#{f}' doesn't exist"
|
53
43
|
end
|
54
44
|
end
|
55
45
|
|
56
|
-
# Add normalization
|
46
|
+
# Add normalization callbacks
|
57
47
|
args.each do |attribute|
|
58
48
|
# Check attributes
|
59
49
|
unless self.column_names.include? attribute.to_s
|
60
50
|
raise "Attribute '#{attribute}' doesn't exist in #{self.name}"
|
61
51
|
end
|
62
52
|
|
63
|
-
@normalization_options[attribute]
|
53
|
+
@normalization_options[attribute] ||= []
|
54
|
+
@normalization_options[attribute] += filters
|
64
55
|
end
|
65
56
|
end
|
57
|
+
alias :normatron :normalize
|
66
58
|
end
|
67
59
|
|
68
60
|
def normalize_attributes
|
@@ -73,8 +65,7 @@ module Normatron
|
|
73
65
|
value = send("#{attribute}_before_type_cast") || send(attribute)
|
74
66
|
|
75
67
|
methods.each do |method|
|
76
|
-
|
77
|
-
value = Conversors.convert(method, value) unless value.nil?
|
68
|
+
value = Filters.do_filter(method, value) unless value.nil?
|
78
69
|
|
79
70
|
if value == :no_method
|
80
71
|
raise ArgumentError, "Method :#{method} cannot be resolved.
|
@@ -2,49 +2,54 @@ require "i18n"
|
|
2
2
|
require "active_support/all"
|
3
3
|
|
4
4
|
module Normatron
|
5
|
-
module
|
5
|
+
module Filters
|
6
|
+
|
7
|
+
private
|
6
8
|
|
7
9
|
MB_CHARS_METHODS = [:upcase, :downcase, :capitalize, :lstrip, :rstrip, :strip, :titlecase, :titleize]
|
8
|
-
SELF_METHODS = [:nillify, :nullify, :nil, :squish, :currency, :integer, :float
|
9
|
-
|
10
|
+
SELF_METHODS = [:nillify, :nullify, :nil, :squish, :currency, :integer, :float]
|
11
|
+
|
12
|
+
public
|
13
|
+
|
14
|
+
NAMES = MB_CHARS_METHODS + SELF_METHODS
|
10
15
|
|
11
|
-
def self.
|
16
|
+
def self.do_filter(method, value)
|
12
17
|
if MB_CHARS_METHODS.include? method
|
13
18
|
value.mb_chars.send(method).to_s
|
14
19
|
elsif SELF_METHODS.include? method
|
15
|
-
send("
|
20
|
+
send("filter_#{method}", value)
|
16
21
|
else
|
17
22
|
:no_method
|
18
23
|
end
|
19
24
|
end
|
20
25
|
|
21
26
|
# Return nil if value is blank or else value itself.
|
22
|
-
def self.
|
27
|
+
def self.filter_nillify(value)
|
23
28
|
value.blank? ? nil : value
|
24
29
|
end
|
25
30
|
class << self
|
26
|
-
alias :
|
27
|
-
alias :
|
31
|
+
alias :filter_nil :filter_nillify
|
32
|
+
alias :filter_nullify :filter_nillify
|
28
33
|
end
|
29
34
|
|
30
35
|
# Remove repeated spaces from the string.
|
31
|
-
def self.
|
32
|
-
value.
|
36
|
+
def self.filter_squish(value)
|
37
|
+
value.squish
|
33
38
|
end
|
34
39
|
|
35
|
-
def self.
|
36
|
-
|
40
|
+
def self.filter_currency(value)
|
41
|
+
filter_number value, :currency
|
37
42
|
end
|
38
43
|
|
39
|
-
def self.
|
40
|
-
|
44
|
+
def self.filter_float(value)
|
45
|
+
filter_number value, :float
|
41
46
|
end
|
42
47
|
|
43
|
-
def self.
|
44
|
-
|
48
|
+
def self.filter_integer(value)
|
49
|
+
filter_number value
|
45
50
|
end
|
46
51
|
|
47
|
-
def self.
|
52
|
+
def self.filter_number(value, type = :integer)
|
48
53
|
return value unless value.is_a?(String) && value.present?
|
49
54
|
|
50
55
|
# Find the first number in the sequence
|
@@ -63,30 +68,12 @@ module Normatron
|
|
63
68
|
decimal_places = value.size - chars_until_separator
|
64
69
|
|
65
70
|
# Build number
|
66
|
-
res =
|
71
|
+
res = filter_digits(value).to_f
|
67
72
|
res *= -1 if negative
|
68
73
|
res /= 10 ** decimal_places
|
69
74
|
res = res.to_i if type == :integer
|
70
75
|
|
71
76
|
res
|
72
77
|
end
|
73
|
-
|
74
|
-
def self.convert_postal_code(value)
|
75
|
-
res = convert_digits value
|
76
|
-
res.size == 8 ? "%s-%s" % [res[0..4], res[5..7]] : value
|
77
|
-
end
|
78
|
-
|
79
|
-
def self.convert_phone(value)
|
80
|
-
res = convert_digits value
|
81
|
-
res.size == 10 ? "(%s) %s-%s" % [res[0..1], res[2..5], res[6..9]] : value
|
82
|
-
end
|
83
|
-
|
84
|
-
def self.convert_digits(value)
|
85
|
-
value.to_s.gsub(/[^\d]/, '')
|
86
|
-
end
|
87
|
-
|
88
|
-
def self.convert_phrase(value)
|
89
|
-
convert(:squish, convert(:strip, value))
|
90
|
-
end
|
91
78
|
end
|
92
79
|
end
|
data/lib/normatron/version.rb
CHANGED
@@ -0,0 +1,77 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require "spec_helper"
|
4
|
+
|
5
|
+
describe Normatron::Filters do
|
6
|
+
|
7
|
+
before :each do
|
8
|
+
TestModel.normalization_options = nil
|
9
|
+
end
|
10
|
+
|
11
|
+
it :capitalize do
|
12
|
+
TestModel.normalize :string_column, :with => :capitalize
|
13
|
+
|
14
|
+
m = TestModel.create :string_column => " abc \f DEF \n 123 áÈç \t !*& \r 4gü "
|
15
|
+
m.string_column.should == " abc \f def \n 123 áèç \t !*& \r 4gü "
|
16
|
+
|
17
|
+
m.string_column = "abc \f DEF \n 123 áÈç \t !*& \r 4gü"
|
18
|
+
m.valid?
|
19
|
+
m.string_column.should == "Abc \f def \n 123 áèç \t !*& \r 4gü"
|
20
|
+
end
|
21
|
+
|
22
|
+
it :downcase do
|
23
|
+
TestModel.normalize :string_column, :with => :downcase
|
24
|
+
|
25
|
+
m = TestModel.create :string_column => " abc \f DEF \n 123 áÈç \t !*& \r 4gü "
|
26
|
+
m.string_column.should == " abc \f def \n 123 áèç \t !*& \r 4gü "
|
27
|
+
end
|
28
|
+
|
29
|
+
it :squish do
|
30
|
+
TestModel.normalize :string_column, :with => :squish
|
31
|
+
|
32
|
+
m = TestModel.create :string_column => " abc \f DEF \n 123 áÈç \t !*& \r 4gü "
|
33
|
+
m.string_column.should == "abc DEF 123 áÈç !*& 4gü"
|
34
|
+
end
|
35
|
+
|
36
|
+
it :strip do
|
37
|
+
TestModel.normalize :string_column, :with => :strip
|
38
|
+
|
39
|
+
m = TestModel.create :string_column => " abc \f DEF \n 123 áÈç \t !*& \r 4gü "
|
40
|
+
m.string_column.should == "abc \f DEF \n 123 áÈç \t !*& \r 4gü"
|
41
|
+
end
|
42
|
+
|
43
|
+
it :titlecase do
|
44
|
+
TestModel.normalize :string_column, :with => :titlecase
|
45
|
+
|
46
|
+
m = TestModel.create :string_column => " abc \f DEF \n 123 áÈç \t !*& \r 4gü "
|
47
|
+
m.string_column.should == " Abc \f Def \n 123 Áèç \t !*& \r 4gü "
|
48
|
+
end
|
49
|
+
|
50
|
+
it :upcase do
|
51
|
+
TestModel.normalize :string_column, :with => :upcase
|
52
|
+
|
53
|
+
m = TestModel.create :string_column => " abc \f DEF \n 123 áÈç \t !*& \r 4gü "
|
54
|
+
m.string_column.should == " ABC \f DEF \n 123 ÁÈÇ \t !*& \r 4GÜ "
|
55
|
+
end
|
56
|
+
|
57
|
+
it :nillify do
|
58
|
+
TestModel.normalize :string_column, :with => :nillify
|
59
|
+
|
60
|
+
m = TestModel.create :string_column => " "
|
61
|
+
m.string_column.should == nil
|
62
|
+
end
|
63
|
+
|
64
|
+
it :lstrip do
|
65
|
+
TestModel.normalize :string_column, :with => :lstrip
|
66
|
+
|
67
|
+
m = TestModel.create :string_column => " abc \f DEF \n 123 áÈç \t !*& \r 4gü "
|
68
|
+
m.string_column.should == "abc \f DEF \n 123 áÈç \t !*& \r 4gü "
|
69
|
+
end
|
70
|
+
|
71
|
+
it :rstrip do
|
72
|
+
TestModel.normalize :string_column, :with => :rstrip
|
73
|
+
|
74
|
+
m = TestModel.create :string_column => " abc \f DEF \n 123 áÈç \t !*& \r 4gü "
|
75
|
+
m.string_column.should == " abc \f DEF \n 123 áÈç \t !*& \r 4gü"
|
76
|
+
end
|
77
|
+
end
|
data/spec/normatron_spec.rb
CHANGED
@@ -5,35 +5,21 @@ require "spec_helper"
|
|
5
5
|
describe Normatron do
|
6
6
|
before :each do
|
7
7
|
TestModel.normalization_options = nil
|
8
|
-
@instance = TestModel.new
|
9
8
|
end
|
10
9
|
|
11
|
-
it "should
|
12
|
-
|
13
|
-
@instance.string_column = "Any string"
|
14
|
-
@instance.save!
|
15
|
-
end.should_not raise_error
|
16
|
-
end
|
17
|
-
|
18
|
-
it "should raise an error if attribute doesn't exist" do
|
19
|
-
lambda do
|
20
|
-
TestModel.normalize :ghost_attribute
|
21
|
-
end.should raise_error "Attribute 'ghost_attribute' doesn't exist in TestModel"
|
10
|
+
it "should respond to aliases" do
|
11
|
+
TestModel.should respond_to :normatron
|
22
12
|
end
|
23
13
|
|
24
|
-
it "should
|
14
|
+
it "should save without normalization option" do
|
25
15
|
lambda do
|
26
|
-
TestModel.
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
it "should raise an error if wrong conversor is called" do
|
31
|
-
lambda do
|
32
|
-
TestModel.normalize :string_column, :with => :wrong_conversor
|
33
|
-
end.should raise_error "Normalization callback 'wrong_conversor' doesn't exist"
|
16
|
+
instance = TestModel.new
|
17
|
+
instance.string_column = "Any string"
|
18
|
+
instance.save!
|
19
|
+
end.should_not raise_error
|
34
20
|
end
|
35
21
|
|
36
|
-
it "
|
22
|
+
it "filters should be accessible" do
|
37
23
|
TestModel.normalize :string_column, :with => [:capitalize, :upcase]
|
38
24
|
TestModel.normalize :integer_column, :with => :integer
|
39
25
|
TestModel.normalization_options.should == { :string_column => [:capitalize, :upcase], :integer_column => [:integer] }
|
@@ -41,108 +27,60 @@ describe Normatron do
|
|
41
27
|
TestModel.normalization_options.should == { :integer_column => [:integer] }
|
42
28
|
end
|
43
29
|
|
44
|
-
|
45
|
-
|
46
|
-
TestModel.normalize :string_column, :with => :capitalize
|
47
|
-
|
48
|
-
m = TestModel.create :string_column => "áb c, 1 2 3 DEF GHI i oz "
|
49
|
-
m.string_column.should == "Áb c, 1 2 3 def ghi i oz "
|
50
|
-
end
|
51
|
-
|
52
|
-
it :digits do
|
53
|
-
TestModel.normalize :string_column, :with => :digits
|
54
|
-
|
55
|
-
m = TestModel.create :string_column => " 1a 2b 3c 4d 5e 6f "
|
56
|
-
m.string_column.should == "123456"
|
57
|
-
end
|
58
|
-
|
59
|
-
it :downcase do
|
60
|
-
TestModel.normalize :string_column, :with => :downcase
|
61
|
-
|
62
|
-
m = TestModel.create :string_column => " a b c, 1 2 3 DEF GHI i oz "
|
63
|
-
m.string_column.should == " a b c, 1 2 3 def ghi i oz "
|
64
|
-
end
|
65
|
-
|
66
|
-
it :phone do
|
67
|
-
TestModel.normalize :string_column, :with => :phone
|
68
|
-
|
69
|
-
m = TestModel.create :string_column => "(99) 9999-9999"
|
70
|
-
m.string_column.should == "(99) 9999-9999"
|
71
|
-
|
72
|
-
m = TestModel.create :string_column => "9999999999"
|
73
|
-
m.string_column.should == "(99) 9999-9999"
|
74
|
-
|
75
|
-
m = TestModel.create :string_column => "999999999"
|
76
|
-
m.string_column.should == "999999999"
|
77
|
-
end
|
78
|
-
|
79
|
-
it :phrase do
|
80
|
-
TestModel.normalize :string_column, :with => :phrase
|
30
|
+
it "should apply single filter" do
|
31
|
+
TestModel.normalize :string_column, :with => :upcase
|
81
32
|
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
it :postal_code do
|
87
|
-
TestModel.normalize :string_column, :with => :postal_code
|
88
|
-
|
89
|
-
m = TestModel.create :string_column => "88888-888"
|
90
|
-
m.string_column.should == "88888-888"
|
91
|
-
|
92
|
-
m = TestModel.create :string_column => "88888888"
|
93
|
-
m.string_column.should == "88888-888"
|
94
|
-
|
95
|
-
m = TestModel.create :string_column => "8888888"
|
96
|
-
m.string_column.should == "8888888"
|
97
|
-
end
|
98
|
-
|
99
|
-
it :squish do
|
100
|
-
TestModel.normalize :string_column, :with => :squish
|
101
|
-
|
102
|
-
m = TestModel.create :string_column => " a b c, 1 2 3 DEF GHI i oz "
|
103
|
-
m.string_column.should == " a b c, 1 2 3 DEF GHI i oz "
|
104
|
-
end
|
105
|
-
|
106
|
-
it :strip do
|
107
|
-
TestModel.normalize :string_column, :with => :strip
|
33
|
+
m = TestModel.create :string_column => "master of puppets"
|
34
|
+
m.string_column.should == "MASTER OF PUPPETS"
|
35
|
+
end
|
108
36
|
|
109
|
-
|
110
|
-
|
111
|
-
end
|
37
|
+
it "should apply multiple filters" do
|
38
|
+
TestModel.normalize :string_column, :with => [:downcase, :squish, :nil]
|
112
39
|
|
113
|
-
|
114
|
-
|
40
|
+
m = TestModel.create :string_column => " YOU SHALL NOT PASS "
|
41
|
+
m.string_column.should == "you shall not pass"
|
115
42
|
|
116
|
-
|
117
|
-
|
118
|
-
|
43
|
+
m = TestModel.create :string_column => " "
|
44
|
+
m.string_column.should == nil
|
45
|
+
end
|
119
46
|
|
120
|
-
|
121
|
-
|
47
|
+
it "should stack filters for the same attribute" do
|
48
|
+
TestModel.normalize :string_column, :with => [:upcase, :squish]
|
49
|
+
TestModel.normalize :string_column, :with => :nil
|
50
|
+
TestModel.normalization_options.should == { :string_column => [:upcase, :squish, :nil] }
|
51
|
+
end
|
122
52
|
|
123
|
-
|
124
|
-
|
125
|
-
|
53
|
+
it "default filters should be :squish and :nillify" do
|
54
|
+
TestModel.normalize :string_column
|
55
|
+
TestModel.normalization_options.should == { :string_column => [:squish, :nillify] }
|
56
|
+
end
|
126
57
|
|
127
|
-
|
128
|
-
|
58
|
+
it "default filters should be :squish and :nillify" do
|
59
|
+
TestModel.normalize :string_column
|
60
|
+
TestModel.normalization_options.should == { :string_column => [:squish, :nillify] }
|
61
|
+
end
|
129
62
|
|
130
|
-
|
131
|
-
|
132
|
-
end
|
63
|
+
it "should filter multiple attributes" do
|
64
|
+
TestModel.normalize :string_column, :integer_column, :with => :nil
|
133
65
|
|
134
|
-
|
135
|
-
|
66
|
+
m = TestModel.create :string_column => " ", :integer_column => " "
|
67
|
+
m.string_column.should == nil
|
68
|
+
m.integer_column.should == nil
|
69
|
+
end
|
136
70
|
|
137
|
-
|
138
|
-
|
139
|
-
|
71
|
+
it "should raise error if a wrong option is set and the right isn't" do
|
72
|
+
lambda do
|
73
|
+
TestModel.normalize :string_column, :wrong_option => :upcase
|
74
|
+
end.should raise_error "Wrong normalization key in TestModel, use :with instead of wrong_option"
|
140
75
|
|
141
|
-
|
142
|
-
TestModel.normalize :string_column, :with => :
|
76
|
+
lambda do
|
77
|
+
TestModel.normalize :string_column, :wrong_option => :upcase, :with => :downcase
|
78
|
+
end.should_not raise_error
|
79
|
+
end
|
143
80
|
|
144
|
-
|
145
|
-
|
146
|
-
|
81
|
+
it "should raise error if wrong filter is called" do
|
82
|
+
lambda do
|
83
|
+
TestModel.normalize :string_column, :with => :wrong_filter
|
84
|
+
end.should raise_error "Normalization filter 'wrong_filter' doesn't exist"
|
147
85
|
end
|
148
86
|
end
|
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.0.
|
4
|
+
version: 0.0.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-08-
|
12
|
+
date: 2012-08-30 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -68,8 +68,8 @@ executables: []
|
|
68
68
|
extensions: []
|
69
69
|
extra_rdoc_files: []
|
70
70
|
files:
|
71
|
+
- lib/normatron/filters.rb
|
71
72
|
- lib/normatron/active_record.rb
|
72
|
-
- lib/normatron/conversors.rb
|
73
73
|
- lib/normatron/version.rb
|
74
74
|
- lib/normatron.rb
|
75
75
|
- MIT-LICENSE
|
@@ -78,6 +78,7 @@ files:
|
|
78
78
|
- spec/support/test_model.rb
|
79
79
|
- spec/normatron_spec.rb
|
80
80
|
- spec/schema.rb
|
81
|
+
- spec/filters_spec.rb
|
81
82
|
- spec/spec_helper.rb
|
82
83
|
homepage: https://github.com/fernandors87/normatron
|
83
84
|
licenses: []
|
@@ -107,4 +108,5 @@ test_files:
|
|
107
108
|
- spec/support/test_model.rb
|
108
109
|
- spec/normatron_spec.rb
|
109
110
|
- spec/schema.rb
|
111
|
+
- spec/filters_spec.rb
|
110
112
|
- spec/spec_helper.rb
|