normatron 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/normatron/active_record.rb +2 -1
- data/lib/normatron/conversors.rb +3 -3
- data/lib/normatron/version.rb +1 -1
- data/spec/normatron_spec.rb +16 -10
- metadata +1 -1
@@ -28,7 +28,7 @@ module Normatron
|
|
28
28
|
elsif options.has_key? :with
|
29
29
|
methods << options[:with]
|
30
30
|
else
|
31
|
-
raise "Wrong normalization option
|
31
|
+
raise "Wrong normalization option in #{self.name}, use :with instead."
|
32
32
|
end
|
33
33
|
|
34
34
|
# Make a prettier array
|
@@ -44,6 +44,7 @@ module Normatron
|
|
44
44
|
|
45
45
|
def normalize_attributes
|
46
46
|
options = self.class.standardize_options
|
47
|
+
return unless options
|
47
48
|
|
48
49
|
options.each do |attribute, methods|
|
49
50
|
value = send("#{attribute}_before_type_cast") || send(attribute)
|
data/lib/normatron/conversors.rb
CHANGED
@@ -5,7 +5,7 @@ module Normatron
|
|
5
5
|
module Conversors
|
6
6
|
|
7
7
|
@@MB_CHARS_METHODS = [:upcase, :downcase, :capitalize, :lstrip, :rstrip, :strip, :titlecase, :titleize]
|
8
|
-
@@SELF_METHODS = [:nillify, :nullify, :nil, :
|
8
|
+
@@SELF_METHODS = [:nillify, :nullify, :nil, :squish, :currency, :integer, :float, :postal_code, :phone, :digits, :phrase]
|
9
9
|
|
10
10
|
def convert(method, value)
|
11
11
|
if @@MB_CHARS_METHODS.include? method
|
@@ -27,7 +27,7 @@ module Normatron
|
|
27
27
|
alias :convert_nullify :convert_nillify
|
28
28
|
|
29
29
|
# Remove repeated spaces from the string.
|
30
|
-
def
|
30
|
+
def convert_squish(value)
|
31
31
|
value.mb_chars.to_s.gsub(/\p{Zs}+/u, ' ')
|
32
32
|
end
|
33
33
|
|
@@ -85,7 +85,7 @@ module Normatron
|
|
85
85
|
end
|
86
86
|
|
87
87
|
def convert_phrase(value)
|
88
|
-
convert(:
|
88
|
+
convert(:squish, convert(:strip, value))
|
89
89
|
end
|
90
90
|
end
|
91
91
|
end
|
data/lib/normatron/version.rb
CHANGED
data/spec/normatron_spec.rb
CHANGED
@@ -4,7 +4,15 @@ require "spec_helper"
|
|
4
4
|
|
5
5
|
describe Normatron do
|
6
6
|
before :each do
|
7
|
-
TestModel.standardize_options =
|
7
|
+
TestModel.standardize_options = nil
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should save without any normalize options" do
|
11
|
+
lambda do
|
12
|
+
a = TestModel.new
|
13
|
+
a.string_field = "a"
|
14
|
+
a.save!
|
15
|
+
end.should_not raise_error
|
8
16
|
end
|
9
17
|
|
10
18
|
describe "Conversors" do
|
@@ -62,6 +70,13 @@ describe Normatron do
|
|
62
70
|
m.string_field.should == "8888888"
|
63
71
|
end
|
64
72
|
|
73
|
+
it :squish do
|
74
|
+
TestModel.normalize :string_field, :with => :squish
|
75
|
+
|
76
|
+
m = TestModel.create :string_field => " a b c, 1 2 3 DEF GHI i oz "
|
77
|
+
m.string_field.should == " a b c, 1 2 3 DEF GHI i oz "
|
78
|
+
end
|
79
|
+
|
65
80
|
it :strip do
|
66
81
|
TestModel.normalize :string_field, :with => :strip
|
67
82
|
|
@@ -83,8 +98,6 @@ describe Normatron do
|
|
83
98
|
m.string_field.should == " A B C, 1 2 3 DEF GHI I OZ "
|
84
99
|
end
|
85
100
|
|
86
|
-
|
87
|
-
|
88
101
|
it :nillify do
|
89
102
|
TestModel.normalize :string_field, :with => :nillify
|
90
103
|
|
@@ -92,13 +105,6 @@ describe Normatron do
|
|
92
105
|
m.string_field.should == nil
|
93
106
|
end
|
94
107
|
|
95
|
-
it :trim do
|
96
|
-
TestModel.normalize :string_field, :with => :trim
|
97
|
-
|
98
|
-
m = TestModel.create :string_field => " a b c, 1 2 3 DEF GHI i oz "
|
99
|
-
m.string_field.should == " a b c, 1 2 3 DEF GHI i oz "
|
100
|
-
end
|
101
|
-
|
102
108
|
it :lstrip do
|
103
109
|
TestModel.normalize :string_field, :with => :lstrip
|
104
110
|
|