has_normalized_fields 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/lib/has_normalized_fields.rb +8 -5
- data/lib/version.rb +1 -1
- data/spec/db/test.sqlite3 +0 -0
- data/spec/has_normalized_fields_spec.rb +0 -13
- metadata +9 -9
data/.gitignore
CHANGED
@@ -8,15 +8,15 @@ module HasNormalizedFields
|
|
8
8
|
TAXID = /[-. )(,]/
|
9
9
|
DOLLAR = /[$, ]/
|
10
10
|
NUMBER = /[, ]/
|
11
|
-
PERCENT = /[
|
11
|
+
PERCENT = /[%, ]/
|
12
12
|
SPACES = /\s/
|
13
13
|
|
14
14
|
#instance methods
|
15
15
|
def self.normalizations(*args)
|
16
16
|
args.each do |arg|
|
17
17
|
reg_exp = HasNormalizedFields.const_get(arg.upcase)
|
18
|
-
define_method "normalize_#{arg}" do
|
19
|
-
|
18
|
+
define_method "normalize_#{arg}" do
|
19
|
+
self && is_a?(String) && match(reg_exp) ? gsub!(reg_exp,'') : self
|
20
20
|
end
|
21
21
|
end
|
22
22
|
end
|
@@ -33,10 +33,9 @@ module HasNormalizedFields
|
|
33
33
|
end
|
34
34
|
|
35
35
|
args.each do |field, normalization_type|
|
36
|
-
raise ArgumentError, "attribute #{field} no belongs to current class" unless columns.map(&:name).include?(field.to_s)
|
37
36
|
define_method "#{field.to_s}=" do |value|
|
38
37
|
if value.present?
|
39
|
-
send
|
38
|
+
value.send("normalize_#{normalization_type.downcase}".to_sym)
|
40
39
|
end
|
41
40
|
super value
|
42
41
|
end
|
@@ -46,5 +45,9 @@ module HasNormalizedFields
|
|
46
45
|
end
|
47
46
|
end
|
48
47
|
|
48
|
+
#extend these classes
|
49
|
+
[String, Fixnum, Float, NilClass].each do |klass|
|
50
|
+
klass.send(:include, HasNormalizedFields)
|
51
|
+
end
|
49
52
|
#include activerecord
|
50
53
|
ActiveRecord::Base.send :include, HasNormalizedFields
|
data/lib/version.rb
CHANGED
data/spec/db/test.sqlite3
CHANGED
Binary file
|
@@ -23,19 +23,6 @@ describe "HasNormalizedFields" do
|
|
23
23
|
Resource.send(:has_normalized_attributes)
|
24
24
|
}.to raise_error(ArgumentError, 'Must define the fields you want to be normalize with has_normalized_attributes :field_one => "phone", :field_two => "zipcode"')
|
25
25
|
end
|
26
|
-
|
27
|
-
it "should raise an error if has fields that no belongs to the current class" do
|
28
|
-
expect {
|
29
|
-
Resource.send(:has_normalized_attributes, {:food => :phone})
|
30
|
-
}.to raise_error(ArgumentError, 'attribute food no belongs to current class')
|
31
|
-
end
|
32
|
-
|
33
|
-
it "should raise an error if has fields that no belongs to the current class" do
|
34
|
-
expect {
|
35
|
-
Resource.send(:has_normalized_attributes, :food)
|
36
|
-
}.to raise_error(ArgumentError, 'Must define the fields you want to be normalize with has_normalized_attributes :field_one => "phone", :field_two => "zipcode"')
|
37
|
-
end
|
38
|
-
|
39
26
|
end
|
40
27
|
|
41
28
|
describe "#phone" do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: has_normalized_fields
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2010-05-18 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &70363055060680 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70363055060680
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: activerecord
|
27
|
-
requirement: &
|
27
|
+
requirement: &70363055060000 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: 3.1.0
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70363055060000
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: sqlite3
|
38
|
-
requirement: &
|
38
|
+
requirement: &70363055059580 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70363055059580
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: database_cleaner
|
49
|
-
requirement: &
|
49
|
+
requirement: &70363055059000 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,7 +54,7 @@ dependencies:
|
|
54
54
|
version: '0'
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *70363055059000
|
58
58
|
description: ! 'has_normalized_fields is a Ruby on Rails gem that lets you normalize
|
59
59
|
user data for an improved user experience.
|
60
60
|
|