has_normalized_attributes 0.0.8 → 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/has_normalized_attributes.rb +53 -48
- data/lib/version.rb +1 -1
- 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: a479787ffc5dfe1a582733208d00010519996423
|
4
|
+
data.tar.gz: 85ea06aec0c4cf71a84833c61af8bb425ea6e8c6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e631a8404fddbedc845f6f3e8f47ee21d1f69e7d3ee61d076b8ee868034bac601f340dce4807ab25fa38142f2c0b2a5b0531e25fd40a21ffcee321eb223cf9a5
|
7
|
+
data.tar.gz: 564da1e595406546904f1ccd251bd71d75bf414dbdd0e6aed8765ed73673dce13270165545e21cfb4b927888f5dc21186abc61d4e97980c99114b99804e796b4
|
@@ -1,25 +1,5 @@
|
|
1
1
|
module HasNormalizedAttributes
|
2
|
-
|
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
|
-
result = super()
|
14
|
-
result.is_a?(String) ? result.sub(/\A\((.*)\)\Z/, '-\1') : result
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
normalizations :number, :dollar
|
20
|
-
}
|
21
|
-
|
22
|
-
#CONSTANT
|
2
|
+
#CONSTANTS - Do not mix these into ActiveRecord!!!
|
23
3
|
ZIPCODE = /[-.\s)(,]/
|
24
4
|
PHONE = /[-.\s)(,]|(^0)/
|
25
5
|
SSN = /[-.\s)(,]/
|
@@ -29,49 +9,74 @@ module HasNormalizedAttributes
|
|
29
9
|
PERCENT = /[%,\s]/
|
30
10
|
SPACES = /\s/
|
31
11
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
12
|
+
module CoreExtensions
|
13
|
+
extend ActiveSupport::Concern
|
14
|
+
|
15
|
+
# Prepending a dynamically defined module to add functionality to the current normalize_ methods.
|
16
|
+
# This is similar to alias_method_chain, but accomplished in a cleaner way using inheritance.
|
17
|
+
prepend Module.new {
|
18
|
+
def self.normalizations(*args)
|
19
|
+
args.each do |arg|
|
20
|
+
# Convert outer parentheses into a negative (-) sign on the result of the super method.
|
21
|
+
# E.g. `normalize_dollar` will first call the original `normalize_dollar` method (super)
|
22
|
+
# and then use the result from that to check for parentheses.
|
23
|
+
define_method "normalize_#{ arg }" do
|
24
|
+
result = super()
|
25
|
+
result.is_a?(String) ? result.sub(/\A\((.*)\)\Z/, '-\1') : result
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
normalizations :number, :dollar
|
31
|
+
}
|
32
|
+
|
33
|
+
#instance methods
|
34
|
+
def self.normalizations(*args)
|
35
|
+
args.each do |arg|
|
36
|
+
define_method "normalize_#{arg}" do
|
37
|
+
if arg == :strip
|
38
|
+
self ? self.strip : self
|
39
|
+
else
|
40
|
+
reg_exp = HasNormalizedAttributes.const_get(arg.upcase)
|
41
|
+
self && is_a?(String) && match(reg_exp) ? gsub(reg_exp,'') : self
|
42
|
+
end
|
41
43
|
end
|
42
44
|
end
|
43
45
|
end
|
44
|
-
end
|
45
46
|
|
46
|
-
|
47
|
-
|
47
|
+
#loading all methods dynamically
|
48
|
+
normalizations :phone, :zipcode, :ssn, :taxid, :dollar, :number, :percent, :spaces, :strip
|
49
|
+
end
|
48
50
|
|
51
|
+
module ActiveRecord
|
52
|
+
extend ActiveSupport::Concern
|
49
53
|
|
50
|
-
|
51
|
-
|
54
|
+
module ClassMethods
|
55
|
+
def has_normalized_attributes(args = {})
|
52
56
|
|
53
|
-
|
54
|
-
|
55
|
-
|
57
|
+
if args.blank? || !args.is_a?(Hash)
|
58
|
+
raise ArgumentError, 'Must define the fields you want to be normalize with has_normalized_attributes :field_one => "phone", :field_two => "zipcode"'
|
59
|
+
end
|
56
60
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
61
|
+
args.each do |field, normalization_type|
|
62
|
+
define_method "#{field.to_s}=" do |value|
|
63
|
+
if value.present?
|
64
|
+
normalized_value = value.send("normalize_#{normalization_type.downcase}".to_sym)
|
65
|
+
else
|
66
|
+
normalized_value = value
|
67
|
+
end
|
68
|
+
super normalized_value
|
63
69
|
end
|
64
|
-
super normalized_value
|
65
70
|
end
|
66
|
-
end
|
67
71
|
|
72
|
+
end
|
68
73
|
end
|
69
74
|
end
|
70
75
|
end
|
71
76
|
|
72
77
|
#extend these classes - Numeric is a parent class for all of Ruby's numeric types.
|
73
78
|
[String, Numeric, NilClass].each do |klass|
|
74
|
-
klass.send(:include, HasNormalizedAttributes)
|
79
|
+
klass.send(:include, HasNormalizedAttributes::CoreExtensions)
|
75
80
|
end
|
76
81
|
#include activerecord
|
77
|
-
ActiveRecord::Base.send :include, HasNormalizedAttributes
|
82
|
+
ActiveRecord::Base.send :include, HasNormalizedAttributes::ActiveRecord
|
data/lib/version.rb
CHANGED