has_normalized_attributes 0.0.8 → 0.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 91dc221ce7007d6265c70324eb766a71b7d094a0
4
- data.tar.gz: f3d16b28f11791f70d98356f950b2fe346c8db2f
3
+ metadata.gz: a479787ffc5dfe1a582733208d00010519996423
4
+ data.tar.gz: 85ea06aec0c4cf71a84833c61af8bb425ea6e8c6
5
5
  SHA512:
6
- metadata.gz: d1fe3bacd965a7ef3880ccb64d38081413c5ccba78d3448a8babffd0be09219992d58f8dc619ab11d0cef93df74e5af3e3fa42257bd38946465adac08c7dbfc3
7
- data.tar.gz: 211acf70d9871c0d78bdaa4c053d347970370256580831a84a3df63a7588016788d016475774057855a8a3afb0d9b1b458e3a02fff198eb3c5e8f25964822b95
6
+ metadata.gz: e631a8404fddbedc845f6f3e8f47ee21d1f69e7d3ee61d076b8ee868034bac601f340dce4807ab25fa38142f2c0b2a5b0531e25fd40a21ffcee321eb223cf9a5
7
+ data.tar.gz: 564da1e595406546904f1ccd251bd71d75bf414dbdd0e6aed8765ed73673dce13270165545e21cfb4b927888f5dc21186abc61d4e97980c99114b99804e796b4
@@ -1,25 +1,5 @@
1
1
  module HasNormalizedAttributes
2
- extend ActiveSupport::Concern
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
- #instance methods
33
- def self.normalizations(*args)
34
- args.each do |arg|
35
- define_method "normalize_#{arg}" do
36
- if arg == :strip
37
- self ? self.strip : self
38
- else
39
- reg_exp = HasNormalizedAttributes.const_get(arg.upcase)
40
- self && is_a?(String) && match(reg_exp) ? gsub(reg_exp,'') : self
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
- #loading all methods dynamically
47
- normalizations :phone, :zipcode, :ssn, :taxid, :dollar, :number, :percent, :spaces, :strip
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
- module ClassMethods
51
- def has_normalized_attributes(args = {})
54
+ module ClassMethods
55
+ def has_normalized_attributes(args = {})
52
56
 
53
- if args.blank? || !args.is_a?(Hash)
54
- raise ArgumentError, 'Must define the fields you want to be normalize with has_normalized_attributes :field_one => "phone", :field_two => "zipcode"'
55
- end
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
- args.each do |field, normalization_type|
58
- define_method "#{field.to_s}=" do |value|
59
- if value.present?
60
- normalized_value = value.send("normalize_#{normalization_type.downcase}".to_sym)
61
- else
62
- normalized_value = value
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
@@ -1,3 +1,3 @@
1
1
  module HasNormalizedAttributes
2
- VERSION = "0.0.8"
2
+ VERSION = "0.0.9"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: has_normalized_attributes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kyle Ginavan