has_normalized_attributes 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/has_normalized_attributes.rb +7 -3
- data/lib/version.rb +1 -1
- data/spec/db/schema.rb +1 -0
- data/spec/db/test.sqlite3 +0 -0
- data/spec/has_normalized_fields_spec.rb +8 -1
- metadata +1 -1
@@ -14,15 +14,19 @@ module HasNormalizedAttributes
|
|
14
14
|
#instance methods
|
15
15
|
def self.normalizations(*args)
|
16
16
|
args.each do |arg|
|
17
|
-
reg_exp = HasNormalizedAttributes.const_get(arg.upcase)
|
18
17
|
define_method "normalize_#{arg}" do
|
19
|
-
|
18
|
+
if arg == :strip
|
19
|
+
self ? self.strip! : self
|
20
|
+
else
|
21
|
+
reg_exp = HasNormalizedAttributes.const_get(arg.upcase)
|
22
|
+
self && is_a?(String) && match(reg_exp) ? gsub!(reg_exp,'') : self
|
23
|
+
end
|
20
24
|
end
|
21
25
|
end
|
22
26
|
end
|
23
27
|
|
24
28
|
#loading all methods dynamically
|
25
|
-
normalizations :phone, :zipcode, :ssn, :taxid, :dollar, :number, :percent, :spaces
|
29
|
+
normalizations :phone, :zipcode, :ssn, :taxid, :dollar, :number, :percent, :spaces, :strip
|
26
30
|
|
27
31
|
|
28
32
|
module ClassMethods
|
data/lib/version.rb
CHANGED
data/spec/db/schema.rb
CHANGED
data/spec/db/test.sqlite3
CHANGED
Binary file
|
@@ -3,7 +3,7 @@ require 'spec_helper'
|
|
3
3
|
class Resource < ActiveRecord::Base
|
4
4
|
has_normalized_attributes :phone_attr => :phone, :zipcode_attr => :zipcode, :ssn_attr => :ssn,
|
5
5
|
:dollar_attr => :dollar, :taxid_attr => :taxid, :number_attr => :number,
|
6
|
-
:percent_attr => :percent, :spaces_attr => :spaces
|
6
|
+
:percent_attr => :percent, :spaces_attr => :spaces, :strip_attr => :strip
|
7
7
|
end
|
8
8
|
|
9
9
|
describe "HasNormalizedAttributes" do
|
@@ -111,5 +111,12 @@ describe "HasNormalizedAttributes" do
|
|
111
111
|
it{@resource.spaces_attr = nil;@resource.spaces_attr.should == nil}
|
112
112
|
end
|
113
113
|
|
114
|
+
describe "#strip" do
|
115
|
+
it{@resource.strip_attr = "text "; @resource.strip_attr.should == "text"}
|
116
|
+
it{@resource.strip_attr = " text"; @resource.strip_attr.should == "text"}
|
117
|
+
it{@resource.strip_attr = " text "; @resource.strip_attr.should == "text"}
|
118
|
+
it{@resource.strip_attr = " some text"; @resource.strip_attr.should == "some text"}
|
119
|
+
it{@resource.strip_attr = " some text "; @resource.strip_attr.should == "some text"}
|
120
|
+
end
|
114
121
|
|
115
122
|
end
|