normatron 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -17,4 +17,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
17
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
18
  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
19
  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -1,29 +1,22 @@
1
1
  = Normatron
2
2
 
3
- Normatron is a attribute normalizer for ActiveRecord.
3
+ Normatron is an attribute normalizer for Ruby On Rails ActiveRecord objects.
4
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.
5
+ This gem inhibits the work of having to override attributes or create a specific method to perform the normalizations.
6
6
 
7
7
  = Installation
8
-
9
8
  Let the bundler install the gem by adding the following into your application gemfile:
10
-
11
9
  gem 'normatron'
12
10
 
13
11
  And then bundle it up:
14
-
15
12
  $ bundle install
16
13
 
17
14
  Or install it by yourself:
18
-
19
15
  $ gem install normatron
20
16
 
21
17
  = The problem
22
-
23
- I have a product model as the following:
24
-
18
+ Suppose you have a product model as the following:
25
19
  # ./db/migrate/20120101010000_create_products.rb
26
-
27
20
  class CreateProducts < ActiveRecord::Migration
28
21
  def change
29
22
  create_table :products do |t|
@@ -34,47 +27,19 @@ I have a product model as the following:
34
27
  end
35
28
 
36
29
  # ./app/models/products.rb
37
-
38
30
  class Product < ActiveRecord::Base
39
31
  attr_accessible :name, :price
40
32
  end
41
33
 
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:
34
+ And you want the 'name' attribute be uppercased before saving it into the database.
35
+ The most usual approach to do it includes:
36
+ * Override the 'name' setter and convert the value to an uppercased string.
37
+ * Use the 'before_validation' callback to run a method or block doing the task.
38
+ Both ways are ilenegant, boring, error prone and very expensive.
39
+ What led me to make this gem and offer a third way to solve the problem:
73
40
 
74
41
  = Usage
75
-
76
42
  # ./app/models/products.rb
77
-
78
43
  class Product < ActiveRecord::Base
79
44
  attr_accessible :name, :price
80
45
 
@@ -86,41 +51,14 @@ Which led me to use a third method:
86
51
  > p.name
87
52
  => " MEMORY CARD "
88
53
 
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
109
-
110
- class Product < ActiveRecord::Base
111
- attr_accessible :name, :description, :brand, :price
54
+ You can also assign multiple attributes to the same filter:
55
+ normalize :name, :brand, :with => :capitalize
112
56
 
113
- normalize :name, :description, :brand, :with => [:upcase, :squish, :nillify]
114
- end
57
+ And multiple filters to an unique attribute:
58
+ normalize :email, :with => [:downcase, :squish]
115
59
 
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
60
+ Or mixing both:
61
+ normalize :name, :description, :with => [:upcase, :squish, :blank]
124
62
 
125
63
  = Filters
126
64
  == :upcase and :downcase
@@ -209,13 +147,13 @@ Remove trailing and leading spaces and then changing remaining consecutive white
209
147
  > p.description
210
148
  => "My product is cheap!!!"
211
149
 
212
- == :nillify, :nullify or :nil
150
+ == :blank
213
151
  Convert a blank attribute into nil.
214
152
 
215
153
  class Person < ActiveRecord::Base
216
154
  attr_accessible :gender, :name
217
155
 
218
- normalize :gender, :with => :nil
156
+ normalize :gender, :with => :blank
219
157
  end
220
158
 
221
159
  $ rails console
@@ -225,7 +163,35 @@ Convert a blank attribute into nil.
225
163
  > p.name
226
164
  => " "
227
165
 
166
+ = Contributing
167
+ * Fork it, add your new features or bug fixes, make your tests and commit.
168
+ * Report any bug or unexpected behavior.
169
+ * Send me your feedback.
170
+ * Pay me a beer. =]
171
+
228
172
  = Special Thanks
229
173
  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.
174
+ I just forked his gem and added some functionalities.<br>
175
+ He has a lot of interesting gems on his repo, please take a look.
176
+
177
+ = Licence
178
+ Copyright 2012 Fernando Rodrigues da Silva
179
+
180
+ Permission is hereby granted, free of charge, to any person obtaining
181
+ a copy of this software and associated documentation files (the
182
+ "Software"), to deal in the Software without restriction, including
183
+ without limitation the rights to use, copy, modify, merge, publish,
184
+ distribute, sublicense, and/or sell copies of the Software, and to
185
+ permit persons to whom the Software is furnished to do so, subject to
186
+ the following conditions:
187
+
188
+ The above copyright notice and this permission notice shall be
189
+ included in all copies or substantial portions of the Software.
190
+
191
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
192
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
193
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
194
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
195
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
196
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
197
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile CHANGED
@@ -31,5 +31,4 @@ Rake::TestTask.new(:test) do |t|
31
31
  t.verbose = false
32
32
  end
33
33
 
34
-
35
- task :default => :test
34
+ task :default => :test
@@ -1,4 +1,7 @@
1
- require "normatron/filters"
2
1
  require "normatron/active_record"
3
2
 
3
+ module Normatron
4
+ VERSION = "0.0.6"
5
+ end
6
+
4
7
  ActiveRecord::Base.send :include, Normatron::ActiveRecord
@@ -1,76 +1,51 @@
1
+ require "normatron/filters"
1
2
  require "active_record"
2
3
 
3
4
  module Normatron
4
5
  module ActiveRecord
5
-
6
6
  def self.included(base)
7
7
  base.instance_eval do
8
- extend ClassMethods
9
-
10
- before_validation :normalize_attributes
8
+ before_validation :apply_normalization
11
9
 
12
10
  class << self
13
- attr_accessor :normalization_options
14
- end
15
- end
16
- end
17
-
18
- module ClassMethods
19
-
20
- def normalize(*args)
21
- # Extract options
22
- @normalization_options ||= {}
23
- options = args.extract_options!
24
-
25
- # Set callbacks
26
- filters = []
27
- if options.empty? # Default standardization
28
- filters << [:squish, :nillify]
29
- elsif options.has_key? :with
30
- filters << options[:with]
31
- else
32
- raise "Wrong normalization key in #{self.name}, use :with instead of #{options.keys.first}"
33
- end
34
-
35
- # Make a prettier array
36
- filters = filters.flatten.compact
37
- filters.map! { |v| v = v.to_sym }
38
-
39
- # Check filters
40
- filters.each do |f|
41
- unless Filters::NAMES.include? f
42
- raise "Normalization filter '#{f}' doesn't exist"
11
+ attr_accessor :normalization_filters
12
+
13
+ def normalize(*args)
14
+ options = args.extract_options!
15
+ options[:with] ||= [:blank, :squish]
16
+
17
+ filters = [options[:with]].flatten.compact
18
+ filters.map!(&:to_sym)
19
+ intersect = filters & (Filters.filter_names + methods(false))
20
+
21
+ unless intersect == filters
22
+ raise "Normalization filters #{filters - intersect} doesn't exist"
23
+ end
24
+
25
+ @normalization_filters ||= {}
26
+ args.each do |raw_attribute|
27
+ attribute = raw_attribute.to_sym
28
+
29
+ if self.column_names.include? attribute.to_s
30
+ @normalization_filters[attribute] ||= []
31
+ @normalization_filters[attribute] += filters
32
+ else
33
+ raise "Attribute '#{attribute}' doesn't exist in #{self}"
34
+ end
35
+ end
43
36
  end
44
37
  end
45
-
46
- # Add normalization callbacks
47
- args.each do |attribute|
48
- # Check attributes
49
- unless self.column_names.include? attribute.to_s
50
- raise "Attribute '#{attribute}' doesn't exist in #{self.name}"
51
- end
52
-
53
- @normalization_options[attribute] ||= []
54
- @normalization_options[attribute] += filters
55
- end
56
38
  end
57
- alias :normatron :normalize
58
39
  end
40
+
41
+ def apply_normalization
42
+ return unless self.class.normalization_filters.is_a? Hash
59
43
 
60
- def normalize_attributes
61
- options = self.class.normalization_options
62
- return unless options
63
-
64
- options.each do |attribute, methods|
44
+ self.class.normalization_filters.each do |attribute, filters|
65
45
  value = send("#{attribute}_before_type_cast") || send(attribute)
66
46
 
67
- methods.each do |method|
68
- value = Filters.do_filter(method, value) unless value.nil?
69
-
70
- if value == :no_method
71
- raise ArgumentError, "Method :#{method} cannot be resolved.
72
- Check options for #{attribute} in #{klass}", caller
73
- end
47
+ filters.each do |filter|
48
+ value = Filters.apply(filter, value)
74
49
  end
75
50
 
76
51
  write_attribute attribute, value
@@ -1,42 +1,33 @@
1
- require "i18n"
2
1
  require "active_support/all"
3
2
 
4
3
  module Normatron
5
4
  module Filters
5
+ class << self
6
+ def apply(filter, value)
7
+ return value unless value.is_a? String
8
+
9
+ case filter
10
+ when :upcase, :downcase, :capitalize, :titlecase, :titleize
11
+ value.mb_chars.send(filter).to_s
12
+ when :squish, :lstrip, :rstrip, :strip
13
+ value.send(filter)
14
+ when :blank
15
+ send("to_#{filter}", value)
16
+ end
17
+ end
6
18
 
7
- private
8
-
9
- MB_CHARS_METHODS = [:upcase, :downcase, :capitalize, :lstrip, :rstrip, :strip, :titlecase, :titleize]
10
- SELF_METHODS = [:nillify, :nullify, :nil, :squish, :currency, :integer, :float]
11
-
12
- public
13
-
14
- NAMES = MB_CHARS_METHODS + SELF_METHODS
15
-
16
- def self.do_filter(method, value)
17
- if MB_CHARS_METHODS.include? method
18
- value.mb_chars.send(method).to_s
19
- elsif SELF_METHODS.include? method
20
- send("filter_#{method}", value)
21
- else
22
- :no_method
19
+ def filter_names
20
+ [:upcase, :downcase, :capitalize, :titlecase, :titleize, :squish, :lstrip, :rstrip, :strip, :blank]
23
21
  end
24
- end
25
22
 
26
- # Return nil if value is blank or else value itself.
27
- def self.filter_nillify(value)
28
- value.blank? ? nil : value
29
- end
30
- class << self
31
- alias :filter_nil :filter_nillify
32
- alias :filter_nullify :filter_nillify
33
- end
23
+ private
34
24
 
35
- # Remove repeated spaces from the string.
36
- def self.filter_squish(value)
37
- value.squish
25
+ def to_blank(value)
26
+ value.blank? ? nil : value
27
+ end
38
28
  end
39
29
 
30
+ =begin
40
31
  def self.filter_currency(value)
41
32
  filter_number value, :currency
42
33
  end
@@ -75,5 +66,6 @@ module Normatron
75
66
 
76
67
  res
77
68
  end
69
+ =end
78
70
  end
79
71
  end
@@ -0,0 +1,137 @@
1
+ require "spec_helper"
2
+
3
+ describe Normatron::ActiveRecord do
4
+ before :each do
5
+ TestModel.normalization_filters = nil
6
+ end
7
+
8
+ it "filters should be accessible" do
9
+ TestModel.normalize :string_column, :with => [:capitalize, :upcase]
10
+ TestModel.normalize :integer_column, :with => :blank
11
+ TestModel.normalization_filters.should == { :string_column => [:capitalize, :upcase],
12
+ :integer_column => [:blank] }
13
+ TestModel.normalization_filters.delete :string_column
14
+ TestModel.normalization_filters.should == { :integer_column => [:blank] }
15
+ end
16
+
17
+ it "should save without normalization" do
18
+ lambda do
19
+ instance = TestModel.new :string_column => "Any string"
20
+ instance.save!
21
+ end.should_not raise_error
22
+ end
23
+
24
+ it "should skip normalization when filters aren't a Hash" do
25
+ TestModel.normalization_filters = true
26
+ lambda do
27
+ instance = TestModel.new :string_column => "Any string"
28
+ instance.valid?
29
+ end.should_not raise_error
30
+
31
+ TestModel.normalization_filters = 1
32
+ lambda do
33
+ instance = TestModel.new :string_column => "Any string"
34
+ instance.valid?
35
+ end.should_not raise_error
36
+
37
+ TestModel.normalization_filters = false
38
+ lambda do
39
+ instance = TestModel.new :string_column => "Any string"
40
+ instance.valid?
41
+ end.should_not raise_error
42
+ end
43
+
44
+ it "should apply default filter to single attribute" do
45
+ TestModel.normalize :string_column
46
+ TestModel.normalization_filters.should == { :string_column => [:blank, :squish] }
47
+ end
48
+
49
+ it "should apply default filter to multiple attributes" do
50
+ TestModel.normalize :string_column, :integer_column
51
+ TestModel.normalization_filters.should == { :string_column => [:blank, :squish],
52
+ :integer_column => [:blank, :squish] }
53
+ end
54
+
55
+ it "should apply single filter to single attribute" do
56
+ TestModel.normalize :string_column, :with => :upcase
57
+ TestModel.normalization_filters.should == { :string_column => [:upcase] }
58
+ end
59
+
60
+ it "should apply single filter to multiple attributes" do
61
+ TestModel.normalize :string_column, :integer_column, :with => :blank
62
+ TestModel.normalization_filters.should == { :string_column => [:blank],
63
+ :integer_column => [:blank] }
64
+ end
65
+
66
+ it "should apply multiple filters to single attribute" do
67
+ TestModel.normalize :string_column, :with => [:blank, :upcase]
68
+ TestModel.normalization_filters.should == { :string_column => [:blank, :upcase] }
69
+ end
70
+
71
+ it "should apply multiple filters to multiple attributes" do
72
+ TestModel.normalize :string_column, :integer_column, :with => [:squish, :blank]
73
+ TestModel.normalization_filters.should == { :string_column => [:squish, :blank],
74
+ :integer_column => [:squish, :blank] }
75
+ end
76
+
77
+ it "should stack filters for the same attribute" do
78
+ TestModel.normalize :string_column, :with => [:upcase, :squish]
79
+ TestModel.normalize :string_column, :integer_column, :with => :blank
80
+ TestModel.normalize :string_column, :integer_column, :with => [:downcase, :strip]
81
+ TestModel.normalization_filters.should == { :string_column => [:upcase, :squish, :blank, :downcase, :strip],
82
+ :integer_column => [:blank, :downcase, :strip] }
83
+ end
84
+
85
+ it "should apply attributes as string" do
86
+ TestModel.normalize "string_column", :with => :upcase
87
+
88
+ instance = TestModel.new :string_column => "Any string"
89
+ instance.valid?
90
+ instance.string_column.should == "ANY STRING"
91
+
92
+ TestModel.normalization_filters.should == { :string_column => [:upcase] }
93
+ end
94
+
95
+ it "should apply filters as one word string" do
96
+ TestModel.normalize :string_column, :with => "upcase"
97
+
98
+ TestModel.normalization_filters.should == { :string_column => [:upcase] }
99
+
100
+ instance = TestModel.new :string_column => "Any string"
101
+ instance.valid?
102
+ instance.string_column.should == "ANY STRING"
103
+ end
104
+
105
+ it "should raise error when a wrong filter is used" do
106
+ lambda do
107
+ TestModel.normalize :string_column, :with => :wrong_filter
108
+ end.should raise_error "Normalization filters [:wrong_filter] doesn't exist"
109
+ end
110
+
111
+ it "should raise error when a wrong attribute is used" do
112
+ lambda do
113
+ TestModel.normalize :wrong_column, :with => :upcase
114
+ end.should raise_error "Attribute 'wrong_column' doesn't exist in TestModel"
115
+ end
116
+
117
+ pending "should allow use an instance method as a filter" do
118
+ lambda do
119
+ TestModel.normalize :string_column, :with => :my_custom_filter_method
120
+ end.should_not raise_error "Normalization filters [:my_custom_filter_method] doesn't exist"
121
+
122
+ TestModel.normalization_filters.should == { :string_column => [:my_custom_filter_method] }
123
+ end
124
+
125
+ pending "should apply an instance method used as a filter" do
126
+ TestModel.normalize :string_column, :with => :my_custom_filter_method
127
+
128
+ m = TestModel.new :string_column => "My life for Aiur"
129
+ m.string_column.should == "M-y- -l-i-f-e- -f-o-r- -A-i-u-r"
130
+ end
131
+
132
+ pending "should allow change default callback"
133
+ pending "should allow get value after type cast"
134
+ pending "should allow use blocks"
135
+ pending "should allow build custom filters"
136
+ pending "should allow use a instance method as a filter"
137
+ end
@@ -5,73 +5,214 @@ require "spec_helper"
5
5
  describe Normatron::Filters do
6
6
 
7
7
  before :each do
8
- TestModel.normalization_options = nil
8
+ TestModel.normalization_filters = nil
9
+ end
10
+
11
+ it :blank do
12
+ value = nil
13
+ expected = nil
14
+ Normatron::Filters.apply(:blank, value).should == expected
15
+
16
+ value = " abc \f DEF \n 123 áÈç \t !*& \r 4gü "
17
+ expected = " abc \f DEF \n 123 áÈç \t !*& \r 4gü "
18
+ Normatron::Filters.apply(:blank, value).should == expected
19
+
20
+ value = " "
21
+ expected = nil
22
+ Normatron::Filters.apply(:blank, value).should == expected
23
+
24
+ value = ""
25
+ expected = nil
26
+ Normatron::Filters.apply(:blank, value).should == expected
9
27
  end
10
28
 
11
29
  it :capitalize do
12
- TestModel.normalize :string_column, :with => :capitalize
30
+ value = nil
31
+ expected = nil
32
+ Normatron::Filters.apply(:capitalize, value).should == expected
13
33
 
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ü "
34
+ value = " abc \f DEF \n 123 áÈç \t !*& \r 4gü "
35
+ expected = " abc \f def \n 123 áèç \t !*& \r 4gü "
36
+ Normatron::Filters.apply(:capitalize, value).should == expected
16
37
 
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ü"
38
+ value = "abc \f DEF \n 123 áÈç \t !*& \r 4gü"
39
+ expected = "Abc \f def \n 123 áèç \t !*& \r 4gü"
40
+ Normatron::Filters.apply(:capitalize, value).should == expected
20
41
  end
21
42
 
22
43
  it :downcase do
23
- TestModel.normalize :string_column, :with => :downcase
44
+ value = nil
45
+ expected = nil
46
+ Normatron::Filters.apply(:downcase, value).should == expected
24
47
 
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ü "
48
+ value = " abc \f DEF \n 123 áÈç \t !*& \r 4gü "
49
+ expected = " abc \f def \n 123 áèç \t !*& \r 4gü "
50
+ Normatron::Filters.apply(:downcase, value).should == expected
51
+ end
52
+
53
+ it :lstrip do
54
+ value = nil
55
+ expected = nil
56
+ Normatron::Filters.apply(:lstrip, value).should == expected
57
+
58
+ value = " abc \f DEF \n 123 áÈç \t !*& \r 4gü "
59
+ expected = "abc \f DEF \n 123 áÈç \t !*& \r 4gü "
60
+ Normatron::Filters.apply(:lstrip, value).should == expected
61
+ end
62
+
63
+ it :rstrip do
64
+ value = nil
65
+ expected = nil
66
+ Normatron::Filters.apply(:rstrip, value).should == expected
67
+
68
+ value = " abc \f DEF \n 123 áÈç \t !*& \r 4gü "
69
+ expected = " abc \f DEF \n 123 áÈç \t !*& \r 4gü"
70
+ Normatron::Filters.apply(:rstrip, value).should == expected
27
71
  end
28
72
 
29
73
  it :squish do
30
- TestModel.normalize :string_column, :with => :squish
74
+ value = nil
75
+ expected = nil
76
+ Normatron::Filters.apply(:squish, value).should == expected
31
77
 
32
- m = TestModel.create :string_column => " abc \f DEF \n 123 áÈç \t !*& \r 4gü "
33
- m.string_column.should == "abc DEF 123 áÈç !*& 4gü"
78
+ value = " abc \f DEF \n 123 áÈç \t !*& \r 4gü "
79
+ expected = "abc DEF 123 áÈç !*& 4gü"
80
+ Normatron::Filters.apply(:squish, value).should == expected
34
81
  end
35
82
 
36
83
  it :strip do
37
- TestModel.normalize :string_column, :with => :strip
84
+ value = nil
85
+ expected = nil
86
+ Normatron::Filters.apply(:strip, value).should == expected
38
87
 
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ü"
88
+ value = " abc \f DEF \n 123 áÈç \t !*& \r 4gü "
89
+ expected = "abc \f DEF \n 123 áÈç \t !*& \r 4gü"
90
+ Normatron::Filters.apply(:strip, value).should == expected
41
91
  end
42
92
 
43
93
  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ü "
94
+ value = nil
95
+ expected = nil
96
+ Normatron::Filters.apply(:titlecase, value).should == expected
97
+ Normatron::Filters.apply(:titleize, value).should == expected
98
+
99
+ value = " abc \f DEF \n 123 áÈç \t !*& \r 4gü "
100
+ expected = " Abc \f Def \n 123 Áèç \t !*& \r 4gü "
101
+ Normatron::Filters.apply(:titlecase, value).should == expected
102
+ Normatron::Filters.apply(:titleize, value).should == expected
48
103
  end
49
104
 
50
105
  it :upcase do
51
- TestModel.normalize :string_column, :with => :upcase
106
+ value = nil
107
+ expected = nil
108
+ Normatron::Filters.apply(:upcase, value).should == expected
52
109
 
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Ü "
110
+ value = " abc \f DEF \n 123 áÈç \t !*& \r 4gü "
111
+ expected = " ABC \f DEF \n 123 ÁÈÇ \t !*& \r 4GÜ "
112
+ Normatron::Filters.apply(:upcase, value).should == expected
55
113
  end
56
114
 
57
- it :nillify do
58
- TestModel.normalize :string_column, :with => :nillify
115
+ pending "singularize"
116
+ pending "pluralize"
117
+ pending "parameterize"
118
+ pending "humanize"
119
+ pending "upper_camelcase"
120
+ pending "lower_camelcase"
121
+ pending "classify"
122
+ pending "dasherize"
123
+ pending "deconstantize"
124
+ pending "demodulize"
125
+ pending "html_safe"
126
+ pending "constantize"
127
+ pending "tableize"
128
+ pending "underscore"
129
+ =begin
130
+ it :singularize do
131
+ value = nil
132
+ expected = nil
133
+ Normatron::Filters.apply(:singularize, value).should == expected
134
+
135
+ value = "products"
136
+ expected = "product"
137
+ Normatron::Filters.apply(:singularize, value).should == expected
138
+ end
59
139
 
60
- m = TestModel.create :string_column => " "
61
- m.string_column.should == nil
140
+ it :pluralize do
141
+ value = nil
142
+ expected = nil
143
+ Normatron::Filters.apply(:pluralize, value).should == expected
144
+
145
+ value = "product"
146
+ expected = "products"
147
+ Normatron::Filters.apply(:pluralize, value).should == expected
62
148
  end
63
149
 
64
- it :lstrip do
65
- TestModel.normalize :string_column, :with => :lstrip
150
+ it :parameterize do
151
+ value = nil
152
+ expected = nil
153
+ Normatron::Filters.apply(:parameterize, value).should == expected
66
154
 
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ü "
155
+ value = " abc \f DEF \n 123 áÈç \t !*& \r 4gü "
156
+ expected = "abc-def-123-aec-4gu"
157
+ Normatron::Filters.apply(:parameterize, value).should == expected
69
158
  end
70
159
 
71
- it :rstrip do
72
- TestModel.normalize :string_column, :with => :rstrip
160
+ it :humanize do
161
+ value = nil
162
+ expected = nil
163
+ Normatron::Filters.apply(:humanize, value).should == expected
164
+
165
+ value = "éou_shall_not_pass"
166
+ expected = "Éou_shall_not_pass"
167
+ Normatron::Filters.apply(:humanize, value).should == expected
168
+ end
169
+
170
+ it :upper_camelcase do
171
+ value = nil
172
+ expected = nil
173
+ Normatron::Filters.apply(:camelize, value).should == expected
174
+ Normatron::Filters.apply(:camelcase, value).should == expected
175
+ Normatron::Filters.apply(:upper_camelize, value).should == expected
176
+ Normatron::Filters.apply(:upper_camelcase, value).should == expected
177
+
178
+ value = "active_record/érrors"
179
+ expected = "ActiveRecord::Érrors"
180
+ Normatron::Filters.apply(:camelize, value).should == expected
181
+ Normatron::Filters.apply(:camelcase, value).should == expected
182
+ Normatron::Filters.apply(:upper_camelize, value).should == expected
183
+ Normatron::Filters.apply(:upper_camelcase, value).should == expected
184
+ end
185
+
186
+ it :lower_camelcase do
187
+ value = nil
188
+ expected = nil
189
+ Normatron::Filters.apply(:lower_camelize, value).should == expected
190
+ Normatron::Filters.apply(:lower_camelcase, value).should == expected
191
+
192
+ value = "active_record/érrors"
193
+ expected = "activeRecord::Érrors"
194
+ Normatron::Filters.apply(:lower_camelize, value).should == expected
195
+ Normatron::Filters.apply(:lower_camelcase, value).should == expected
196
+ end
197
+
198
+ it :classify do
199
+ value = nil
200
+ expected = nil
201
+ Normatron::Filters.apply(:classify, value).should == expected
202
+
203
+ value = "áctive_record/errors"
204
+ expected = "ÁctiveRecord::Errors"
205
+ Normatron::Filters.apply(:classify, value).should == expected
206
+ end
207
+
208
+ it :dasherize do
209
+ value = nil
210
+ expected = nil
211
+ Normatron::Filters.apply(:dasherize, value).should == expected
73
212
 
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ü"
213
+ value = "active_record/errors"
214
+ expected = "active-record/errors"
215
+ Normatron::Filters.apply(:dasherize, value).should == expected
76
216
  end
217
+ =end
77
218
  end
@@ -3,6 +3,6 @@ require "normatron"
3
3
 
4
4
  ActiveRecord::Base.establish_connection :adapter => "sqlite3", :database => ":memory:"
5
5
 
6
- load "schema.rb"
7
6
 
8
- Dir[File.dirname(__FILE__) + "/support/**/*.rb"].each {|f| require f}
7
+
8
+ Dir[File.dirname(__FILE__) + "/support/**/*.rb"].each { |f| require f }
File without changes
@@ -1,3 +1,7 @@
1
1
  class TestModel < ActiveRecord::Base
2
2
  attr_accessible :string_column, :integer_column
3
+
4
+ def my_custom_filter_method(value)
5
+ value.split(//) * "-"
6
+ end
3
7
  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.5
4
+ version: 0.0.6
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-30 00:00:00.000000000 Z
12
+ date: 2012-09-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -34,7 +34,7 @@ dependencies:
34
34
  requirements:
35
35
  - - ! '>='
36
36
  - !ruby/object:Gem::Version
37
- version: '0'
37
+ version: 1.3.0
38
38
  type: :development
39
39
  prerelease: false
40
40
  version_requirements: !ruby/object:Gem::Requirement
@@ -42,7 +42,7 @@ dependencies:
42
42
  requirements:
43
43
  - - ! '>='
44
44
  - !ruby/object:Gem::Version
45
- version: '0'
45
+ version: 1.3.0
46
46
  - !ruby/object:Gem::Dependency
47
47
  name: rspec
48
48
  requirement: !ruby/object:Gem::Requirement
@@ -50,7 +50,7 @@ dependencies:
50
50
  requirements:
51
51
  - - ! '>='
52
52
  - !ruby/object:Gem::Version
53
- version: 2.0.0
53
+ version: 2.10.0
54
54
  type: :development
55
55
  prerelease: false
56
56
  version_requirements: !ruby/object:Gem::Requirement
@@ -58,26 +58,26 @@ dependencies:
58
58
  requirements:
59
59
  - - ! '>='
60
60
  - !ruby/object:Gem::Version
61
- version: 2.0.0
62
- description: ! " Normatron is a Rails plugin that helps normalize ActiveRecord
63
- attributes.\n Operations such as lowercase an e-mail address or format a phone
64
- number before model validations,\n can be made easily using this plugin.\n"
61
+ version: 2.10.0
62
+ description: ! " Normatron is an attribute normalizer for ActiveRecord objects.\n
63
+ \ With it you can convert attributes to the desired format before saving them
64
+ in the database.\n This gem inhibits the work of having to override attributes
65
+ or create a specific method to perform the normalizations.\n"
65
66
  email:
66
- - fernandors87@hotmail.com
67
+ - fernandors87@gmail.com
67
68
  executables: []
68
69
  extensions: []
69
70
  extra_rdoc_files: []
70
71
  files:
71
72
  - lib/normatron/filters.rb
72
73
  - lib/normatron/active_record.rb
73
- - lib/normatron/version.rb
74
74
  - lib/normatron.rb
75
75
  - MIT-LICENSE
76
76
  - Rakefile
77
77
  - README.rdoc
78
78
  - spec/support/test_model.rb
79
- - spec/normatron_spec.rb
80
- - spec/schema.rb
79
+ - spec/support/schema.rb
80
+ - spec/active_record_spec.rb
81
81
  - spec/filters_spec.rb
82
82
  - spec/spec_helper.rb
83
83
  homepage: https://github.com/fernandors87/normatron
@@ -103,10 +103,10 @@ rubyforge_project:
103
103
  rubygems_version: 1.8.24
104
104
  signing_key:
105
105
  specification_version: 3
106
- summary: Handle normalization for ActiveRecord attributes
106
+ summary: Normalize attributes for ActiveRecord objects.
107
107
  test_files:
108
108
  - spec/support/test_model.rb
109
- - spec/normatron_spec.rb
110
- - spec/schema.rb
109
+ - spec/support/schema.rb
110
+ - spec/active_record_spec.rb
111
111
  - spec/filters_spec.rb
112
112
  - spec/spec_helper.rb
@@ -1,3 +0,0 @@
1
- module Normatron
2
- VERSION = "0.0.5"
3
- end
@@ -1,86 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require "spec_helper"
4
-
5
- describe Normatron do
6
- before :each do
7
- TestModel.normalization_options = nil
8
- end
9
-
10
- it "should respond to aliases" do
11
- TestModel.should respond_to :normatron
12
- end
13
-
14
- it "should save without normalization option" do
15
- lambda do
16
- instance = TestModel.new
17
- instance.string_column = "Any string"
18
- instance.save!
19
- end.should_not raise_error
20
- end
21
-
22
- it "filters should be accessible" do
23
- TestModel.normalize :string_column, :with => [:capitalize, :upcase]
24
- TestModel.normalize :integer_column, :with => :integer
25
- TestModel.normalization_options.should == { :string_column => [:capitalize, :upcase], :integer_column => [:integer] }
26
- TestModel.normalization_options.delete :string_column
27
- TestModel.normalization_options.should == { :integer_column => [:integer] }
28
- end
29
-
30
- it "should apply single filter" do
31
- TestModel.normalize :string_column, :with => :upcase
32
-
33
- m = TestModel.create :string_column => "master of puppets"
34
- m.string_column.should == "MASTER OF PUPPETS"
35
- end
36
-
37
- it "should apply multiple filters" do
38
- TestModel.normalize :string_column, :with => [:downcase, :squish, :nil]
39
-
40
- m = TestModel.create :string_column => " YOU SHALL NOT PASS "
41
- m.string_column.should == "you shall not pass"
42
-
43
- m = TestModel.create :string_column => " "
44
- m.string_column.should == nil
45
- end
46
-
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
52
-
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
57
-
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
62
-
63
- it "should filter multiple attributes" do
64
- TestModel.normalize :string_column, :integer_column, :with => :nil
65
-
66
- m = TestModel.create :string_column => " ", :integer_column => " "
67
- m.string_column.should == nil
68
- m.integer_column.should == nil
69
- end
70
-
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"
75
-
76
- lambda do
77
- TestModel.normalize :string_column, :wrong_option => :upcase, :with => :downcase
78
- end.should_not raise_error
79
- end
80
-
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"
85
- end
86
- end