normalize_attributes 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
@@ -40,7 +40,7 @@ module NormalizeAttributes
|
|
40
40
|
options = self.class.normalize_attributes_options || {}
|
41
41
|
|
42
42
|
options.each do |attr_name, normalizers|
|
43
|
-
value = self.send(attr_name)
|
43
|
+
value = self.send("#{attr_name}_before_type_cast")
|
44
44
|
|
45
45
|
if normalizers.empty?
|
46
46
|
case value
|
@@ -56,6 +56,8 @@ module NormalizeAttributes
|
|
56
56
|
value = normalizer.call(value)
|
57
57
|
elsif value.respond_to?(normalizer)
|
58
58
|
value = value.send(normalizer)
|
59
|
+
else
|
60
|
+
value = send(normalizer, value)
|
59
61
|
end
|
60
62
|
end
|
61
63
|
|
@@ -33,6 +33,23 @@ describe "Normalize Attributes" do
|
|
33
33
|
user.email.should == "john@doe.com"
|
34
34
|
end
|
35
35
|
|
36
|
+
it "should apply instance method" do
|
37
|
+
User.normalize_attribute(:username, :with => :normalize_username)
|
38
|
+
user = User.create(:username => "JOHNDOE")
|
39
|
+
|
40
|
+
user.username.should == "johndoe"
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should use value before type casting" do
|
44
|
+
User.normalize_attribute(:age) do |v|
|
45
|
+
v.should == "1.2"
|
46
|
+
v.to_f * 10
|
47
|
+
end
|
48
|
+
|
49
|
+
user = User.create(:age => "1.2")
|
50
|
+
user.age.should == 12
|
51
|
+
end
|
52
|
+
|
36
53
|
it "should combine both method names and procs as normalization methods" do
|
37
54
|
User.normalize_attribute(:email, :with => :downcase) {|v| v.reverse }
|
38
55
|
user = User.create(:email => "JOHN@DOE.COM")
|
data/spec/schema.rb
CHANGED
data/spec/support/user.rb
CHANGED