has_normalized_attributes 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +1 -1
- data/lib/has_normalized_attributes.rb +19 -0
- data/lib/version.rb +1 -1
- data/spec/db/test.sqlite3 +0 -0
- data/spec/has_normalized_fields_spec.rb +8 -1
- data/spec/spec_helper.rb +1 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f1fda4bcf138c5f344683454c81f5f487bfce890
|
4
|
+
data.tar.gz: 0ec6839731b7710722eb7a3bca7417381736bf96
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: da7e3cc3b96846545ef0d557297187d6ceaa7d78ac79962ee95f6bd20d66d9e6a6067a61ad7a36b4fabc67f3cb41a32545d0a890733d554bad00b8ac87e18712
|
7
|
+
data.tar.gz: 844bbd5d4aa88e0d8c2bdca18942110be00943143f89b11079f4cd2493d42eb4cafde5c7b9528c9b033397c53b688f83a617306b16a36bbaca3835a6c53549c5
|
data/.rspec
CHANGED
@@ -1,2 +1,2 @@
|
|
1
1
|
--colour
|
2
|
-
--format
|
2
|
+
--format documentation
|
@@ -1,6 +1,25 @@
|
|
1
1
|
module HasNormalizedAttributes
|
2
2
|
extend ActiveSupport::Concern
|
3
3
|
|
4
|
+
# Prepending a dynamically defined module to add functionality to the current normalize_ methods.
|
5
|
+
# This is similar to alias_method_chain, but accomplished in a cleaner way using inheritance.
|
6
|
+
prepend Module.new {
|
7
|
+
def self.normalizations(*args)
|
8
|
+
args.each do |arg|
|
9
|
+
# Convert outer parentheses into a negative (-) sign on the result of the super method.
|
10
|
+
# E.g. `normalize_dollar` will first call the original `normalize_dollar` method (super)
|
11
|
+
# and then use the result from that to check for parentheses.
|
12
|
+
define_method "normalize_#{ arg }" do
|
13
|
+
super().tap do |result|
|
14
|
+
result.sub! /\A\((.*)\)\Z/, '-\1' if result.respond_to?(:sub!)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
normalizations :number, :dollar
|
21
|
+
}
|
22
|
+
|
4
23
|
#CONSTANT
|
5
24
|
ZIPCODE = /[-. )(,]/
|
6
25
|
PHONE = /[-. )(,]|(^0)/
|
data/lib/version.rb
CHANGED
data/spec/db/test.sqlite3
CHANGED
Binary file
|
@@ -57,7 +57,7 @@ describe "HasNormalizedAttributes" do
|
|
57
57
|
it{@resource.zipcode_attr = nil; @resource.zipcode_attr.should == nil}
|
58
58
|
it{@resource.zipcode_attr = "(111)"; @resource.zipcode_attr.should == "111"}
|
59
59
|
it{@resource.zipcode_attr = "11111"; @resource.zipcode_attr.should == "11111"}
|
60
|
-
|
60
|
+
skip("Possible inconsistency between ActiveRecord and SQLite") {@resource.zipcode_attr = 111.11; @resource.zipcode_attr.should == 111.11} # It appears that there is inconsistency with the return value from a string field such as zipcode_attr.
|
61
61
|
end
|
62
62
|
|
63
63
|
describe "#ssn" do
|
@@ -87,6 +87,10 @@ describe "HasNormalizedAttributes" do
|
|
87
87
|
it{@resource.dollar_attr = "$111, 111 ";@resource.dollar_attr.should == "111111"}
|
88
88
|
it{@resource.dollar_attr = "";@resource.dollar_attr.should == ""}
|
89
89
|
it{@resource.dollar_attr = nil;@resource.dollar_attr.should == nil}
|
90
|
+
it{@resource.dollar_attr = 111111.51;@resource.dollar_attr.should == "111111.51"}
|
91
|
+
it{@resource.dollar_attr = "(321.45)";@resource.dollar_attr.should == "-321.45"}
|
92
|
+
it{@resource.dollar_attr = "$(321.45)";@resource.dollar_attr.should == "-321.45"}
|
93
|
+
it{@resource.dollar_attr = "($321.45)";@resource.dollar_attr.should == "-321.45"}
|
90
94
|
end
|
91
95
|
|
92
96
|
describe "#number" do
|
@@ -94,6 +98,9 @@ describe "HasNormalizedAttributes" do
|
|
94
98
|
it{@resource.number_attr = "1 23 ";@resource.number_attr.should == "123"}
|
95
99
|
it{@resource.number_attr = "";@resource.number_attr.should == ""}
|
96
100
|
it{@resource.number_attr = nil;@resource.number_attr.should == nil}
|
101
|
+
it{@resource.dollar_attr = 111111.51;@resource.dollar_attr.should == "111111.51"}
|
102
|
+
it{@resource.dollar_attr = "(321.45)";@resource.dollar_attr.should == "-321.45"}
|
103
|
+
it{@resource.dollar_attr = "-321.45";@resource.dollar_attr.should == "-321.45"}
|
97
104
|
end
|
98
105
|
|
99
106
|
describe "#percent" do
|
data/spec/spec_helper.rb
CHANGED