has_normalized_fields 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ *.gem
2
+ .bundle
3
+ .rvmrc
4
+ Gemfile.lock
5
+ pkg/*
6
+ log/*.log
7
+ *.tmproj
8
+ tmp
9
+ log
10
+ .gitconfig
11
+ bin/*
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --colour
2
+ --format nested
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ rvm:
2
+ - 1.9.3
3
+ - 1.9.2
4
+
5
+ script: rake spec
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in send_grid.gemspec
4
+ gemspec
data/README.rdoc ADDED
@@ -0,0 +1,52 @@
1
+ == HasNormalizedFields {<img src="https://secure.travis-ci.org/kylejginavan/has_normalized_fields.png"/>}[http://travis-ci.org/kylejginavan/has_normalized_fields]
2
+
3
+ == DESCRIPTION
4
+
5
+ has_normalized_fields is a Ruby on Rails gem that lets you normalize user data for an improved user experience.
6
+ It takes the messy user inputed data and normalizes it into a nice clean standard format.
7
+
8
+ == INSTALLATION & SETUP:
9
+
10
+ add in your Gemfile:
11
+
12
+ gem 'has_normalized_fields'
13
+
14
+ in your model:
15
+
16
+ has_normalized_attributes :field => :normalized_type
17
+
18
+ for example:
19
+
20
+ class CoolStuff < ActiveRecord::Base
21
+ has_normalized_attributes :phone_attr => :phone, :zipcode_attr => :zipcode, :ssn_attr => :ssn,
22
+ :dollar_attr => :dollar, :taxid_attr => :taxid, :number_attr => :number,
23
+ :percent_attr => :percent, :spaces_attr => :spaces
24
+ end
25
+
26
+ == Normalizations availables
27
+ :phone, :zipcode, :ssn, :dollar, :taxid, :number, :percent, :spaces
28
+
29
+
30
+ == COPYRIGHT
31
+
32
+ Copyright (c) 2011 Kyle Ginavan
33
+
34
+ Permission is hereby granted, free of charge, to any person obtaining
35
+ a copy of this software and associated documentation files (the
36
+ "Software"), to deal in the Software without restriction, including
37
+ without limitation the rights to use, copy, modify, merge, publish,
38
+ distribute, sublicense, and/or sell copies of the Software, and to
39
+ permit persons to whom the Software is furnished to do so, subject to
40
+ the following conditions:
41
+
42
+ The above copyright notice and this permission notice shall be
43
+ included in all copies or substantial portions of the Software.
44
+
45
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
46
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
47
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
48
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
49
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
50
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
51
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
52
+ See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require 'bundler'
2
+ require 'rspec/core/rake_task'
3
+ Bundler::GemHelper.install_tasks
4
+ RSpec::Core::RakeTask.new(:spec)
5
+ task :default => :spec
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = %q{has_normalized_fields}
7
+ s.version = HasNormalizedFields::VERSION
8
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
9
+ s.authors = ["Kyle Ginavan"]
10
+ s.date = %q{2010-05-18}
11
+ s.homepage = "https://github.com/kylejginavan/has_normalized_fields"
12
+ s.summary = %q{Ruby on Rails gem for normalize data prior to save}
13
+ s.description = %q{has_normalized_fields is a Ruby on Rails gem that lets you normalize user data for an improved user experience.
14
+ It takes the messy user inputed data and normalizes it into a nice clean standard format.}
15
+ s.email = %q{kylejginavan@gmail.com}
16
+ s.rubyforge_project = "has_normalized_fields"
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ s.add_development_dependency "rspec"
22
+ s.add_development_dependency("activerecord", ['>= 3.1.0'])
23
+ s.add_development_dependency("sqlite3")
24
+ s.add_development_dependency('database_cleaner')
25
+ end
26
+
@@ -0,0 +1,50 @@
1
+ module HasNormalizedFields
2
+ extend ActiveSupport::Concern
3
+
4
+ #CONSTANT
5
+ ZIPCODE = /[-. )(,]/
6
+ PHONE = /[-. )(,]/
7
+ SSN = /[-. )(,]/
8
+ TAXID = /[-. )(,]/
9
+ DOLLAR = /[$, ]/
10
+ NUMBER = /[, ]/
11
+ PERCENT = /[% ]/
12
+ SPACES = /\s/
13
+
14
+ #instance methods
15
+ def self.normalizations(*args)
16
+ args.each do |arg|
17
+ reg_exp = HasNormalizedFields.const_get(arg.upcase)
18
+ define_method "normalize_#{arg}" do |value|
19
+ value && value.is_a?(String) && value.match(reg_exp) ? value.gsub!(reg_exp,'') : value
20
+ end
21
+ end
22
+ end
23
+
24
+ #loading all methods dynamically
25
+ normalizations :phone, :zipcode, :ssn, :taxid, :dollar, :number, :percent, :spaces
26
+
27
+
28
+ module ClassMethods
29
+ def has_normalized_attributes(args = {})
30
+
31
+ if args.blank? || !args.is_a?(Hash)
32
+ raise ArgumentError, 'Must define the fields you want to be normalize with has_normalized_attributes :field_one => "phone", :field_two => "zipcode"'
33
+ end
34
+
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
+ define_method "#{field.to_s}=" do |value|
38
+ if value.present?
39
+ send "normalize_#{normalization_type.downcase}".to_sym, value.to_s
40
+ end
41
+ super value
42
+ end
43
+ end
44
+
45
+ end
46
+ end
47
+ end
48
+
49
+ #include activerecord
50
+ ActiveRecord::Base.send :include, HasNormalizedFields
data/lib/version.rb ADDED
@@ -0,0 +1,3 @@
1
+ module HasNormalizedFields
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,3 @@
1
+ sqlite3:
2
+ adapter: sqlite3
3
+ database: spec/db/test.sqlite3
data/spec/db/schema.rb ADDED
@@ -0,0 +1,14 @@
1
+ ActiveRecord::Schema.define(:version => 0) do
2
+
3
+ create_table "resources", :force => true do |t|
4
+ t.string "phone_attr"
5
+ t.string "ssn_attr"
6
+ t.string "zipcode_attr"
7
+ t.string "taxid_attr"
8
+ t.string "dollar_attr"
9
+ t.string "number_attr"
10
+ t.string "percent_attr"
11
+ t.string "spaces_attr"
12
+ end
13
+
14
+ end
Binary file
@@ -0,0 +1,125 @@
1
+ require 'spec_helper'
2
+
3
+ class Resource < ActiveRecord::Base
4
+ has_normalized_attributes :phone_attr => :phone, :zipcode_attr => :zipcode, :ssn_attr => :ssn,
5
+ :dollar_attr => :dollar, :taxid_attr => :taxid, :number_attr => :number,
6
+ :percent_attr => :percent, :spaces_attr => :spaces
7
+ end
8
+
9
+ describe "HasNormalizedFields" do
10
+ before(:each) do
11
+ @resource = Resource.new
12
+ end
13
+
14
+ describe "class method" do
15
+ it "should work" do
16
+ expect {
17
+ Resource.send(:has_normalized_attributes, {:phone_attr => :phone})
18
+ }.to_not raise_error
19
+ end
20
+
21
+ it "should raise an error if no fields are passed in" do
22
+ expect {
23
+ Resource.send(:has_normalized_attributes)
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
+ 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
+ end
40
+
41
+ describe "#phone" do
42
+ it{@resource.phone_attr = "11-111"; @resource.phone_attr.should == "11111"}
43
+ it{@resource.phone_attr = "1111111111"; @resource.phone_attr.should == "1111111111"}
44
+ it{@resource.phone_attr = "111 111 1111"; @resource.phone_attr.should == "1111111111"}
45
+ it{@resource.phone_attr = " 111 111 1111 "; @resource.phone_attr.should == "1111111111"}
46
+ it{@resource.phone_attr = "111.111.1111"; @resource.phone_attr.should == "1111111111"}
47
+ it{@resource.phone_attr = "(111)111-1111"; @resource.phone_attr.should == "1111111111"}
48
+ it{@resource.phone_attr = "(111)1111111"; @resource.phone_attr.should == "1111111111"}
49
+ it{@resource.phone_attr = " 111-111.1111 "; @resource.phone_attr.should == "1111111111"}
50
+ it{@resource.phone_attr = ""; @resource.phone_attr.should == ""}
51
+ it{@resource.phone_attr = nil; @resource.phone_attr.should == nil}
52
+ end
53
+
54
+ describe "#zipcode" do
55
+ it{@resource.zipcode_attr = "11111"; @resource.zipcode_attr.should == "11111"}
56
+ it{@resource.zipcode_attr = " 11 1-11 "; @resource.zipcode_attr.should == "11111"}
57
+ it{@resource.zipcode_attr = " 111-11 ("; @resource.zipcode_attr.should == "11111"}
58
+ it{@resource.zipcode_attr = "11,1-11"; @resource.zipcode_attr.should == "11111"}
59
+ it{@resource.zipcode_attr = "11.111"; @resource.zipcode_attr.should == "11111"}
60
+ it{@resource.zipcode_attr = "111111111"; @resource.zipcode_attr.should == "111111111"}
61
+ it{@resource.zipcode_attr = "(11111) 1111"; @resource.zipcode_attr.should == "111111111"}
62
+ it{@resource.zipcode_attr = "11111) -1111"; @resource.zipcode_attr.should == "111111111"}
63
+ it{@resource.zipcode_attr = "11111.1111"; @resource.zipcode_attr.should == "111111111"}
64
+ it{@resource.zipcode_attr = "11111 --1111"; @resource.zipcode_attr.should == "111111111"}
65
+ it{@resource.zipcode_attr = " 11111,1111 "; @resource.zipcode_attr.should == "111111111"}
66
+ it{@resource.zipcode_attr = ""; @resource.zipcode_attr.should == ""}
67
+ it{@resource.zipcode_attr = nil; @resource.zipcode_attr.should == nil}
68
+ it{@resource.zipcode_attr = "(111)"; @resource.zipcode_attr.should == "111"}
69
+ it{@resource.zipcode_attr = "11111"; @resource.zipcode_attr.should == "11111"}
70
+ it{@resource.zipcode_attr = 111.11; @resource.zipcode_attr.should == 111.11}
71
+ end
72
+
73
+ describe "#ssn" do
74
+ it{@resource.ssn_attr = "111 111 1111"; @resource.ssn_attr.should == "1111111111"}
75
+ it{@resource.ssn_attr = " 111 111 1111 "; @resource.ssn_attr.should == "1111111111"}
76
+ it{@resource.ssn_attr = "111.111.1111"; @resource.ssn_attr.should == "1111111111"}
77
+ it{@resource.ssn_attr = "(111)111-1111"; @resource.ssn_attr.should == "1111111111"}
78
+ it{@resource.ssn_attr = "(111)1111111"; @resource.ssn_attr.should == "1111111111"}
79
+ it{@resource.ssn_attr = " 111-111.1111 "; @resource.ssn_attr.should == "1111111111"}
80
+ it{@resource.ssn_attr = ""; @resource.ssn_attr.should == ""}
81
+ it{@resource.ssn_attr = nil; @resource.ssn_attr.should == nil}
82
+ end
83
+
84
+ describe "#taxid" do
85
+ it{@resource.taxid_attr = "12-345"; @resource.taxid_attr.should == "12345"}
86
+ it{@resource.taxid_attr = "12.345"; @resource.taxid_attr.should == "12345"}
87
+ it{@resource.taxid_attr = "1-2345"; @resource.taxid_attr.should == "12345"}
88
+ it{@resource.taxid_attr = "1,2345"; @resource.taxid_attr.should == "12345"}
89
+ it{@resource.taxid_attr = ""; @resource.taxid_attr.should == ""}
90
+ it{@resource.taxid_attr = nil; @resource.taxid_attr.should == nil}
91
+ end
92
+
93
+ describe "#dollar" do
94
+ it{@resource.dollar_attr = "$111111";@resource.dollar_attr.should == "111111"}
95
+ it{@resource.dollar_attr = "111,111";@resource.dollar_attr.should == "111111"}
96
+ it{@resource.dollar_attr = "111 111 ";@resource.dollar_attr.should == "111111"}
97
+ it{@resource.dollar_attr = "$111, 111 ";@resource.dollar_attr.should == "111111"}
98
+ it{@resource.dollar_attr = "";@resource.dollar_attr.should == ""}
99
+ it{@resource.dollar_attr = nil;@resource.dollar_attr.should == nil}
100
+ end
101
+
102
+ describe "#number" do
103
+ it{@resource.number_attr = "1,23";@resource.number_attr.should == "123"}
104
+ it{@resource.number_attr = "1 23 ";@resource.number_attr.should == "123"}
105
+ it{@resource.number_attr = "";@resource.number_attr.should == ""}
106
+ it{@resource.number_attr = nil;@resource.number_attr.should == nil}
107
+ end
108
+
109
+ describe "#percent" do
110
+ it{@resource.percent_attr = " 1 1 ";@resource.percent_attr.should == "11"}
111
+ it{@resource.percent_attr = "11%";@resource.percent_attr.should == "11"}
112
+ it{@resource.percent_attr = "%11";@resource.percent_attr.should == "11"}
113
+ it{@resource.percent_attr = "1 1 % ";@resource.percent_attr.should == "11"}
114
+ it{@resource.percent_attr = "";@resource.percent_attr.should == ""}
115
+ it{@resource.percent_attr = nil;@resource.percent_attr.should == nil}
116
+ end
117
+
118
+ describe "#spaces" do
119
+ it{@resource.spaces_attr = "5 0";@resource.spaces_attr.should == "50"}
120
+ it{@resource.spaces_attr = "";@resource.spaces_attr.should == ""}
121
+ it{@resource.spaces_attr = nil;@resource.spaces_attr.should == nil}
122
+ end
123
+
124
+
125
+ end
@@ -0,0 +1,35 @@
1
+ require "active_support"
2
+ require "active_record"
3
+ require "database_cleaner"
4
+
5
+ ENV['debug'] = 'test' unless ENV['debug']
6
+
7
+ # Establish DB Connection
8
+ config = YAML::load(IO.read(File.join(File.dirname(__FILE__), 'db', 'database.yml')))
9
+ ActiveRecord::Base.configurations = {'test' => config[ENV['DB'] || 'sqlite3']}
10
+ ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['test'])
11
+
12
+ # Load Test Schema into the Database
13
+ load(File.dirname(__FILE__) + "/db/schema.rb")
14
+
15
+ # Load in our code
16
+ $LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib"
17
+
18
+ require 'has_normalized_fields'
19
+
20
+ RSpec.configure do |config|
21
+
22
+ config.before(:suite) do
23
+ DatabaseCleaner.strategy = :transaction
24
+ DatabaseCleaner.clean_with(:truncation)
25
+ end
26
+
27
+ config.before(:each) do
28
+ DatabaseCleaner.start
29
+ end
30
+
31
+ config.after(:each) do
32
+ DatabaseCleaner.clean
33
+ end
34
+
35
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: has_normalized_fields
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Kyle Ginavan
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2010-05-18 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &70354463185680 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70354463185680
25
+ - !ruby/object:Gem::Dependency
26
+ name: activerecord
27
+ requirement: &70354463184960 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: 3.1.0
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70354463184960
36
+ - !ruby/object:Gem::Dependency
37
+ name: sqlite3
38
+ requirement: &70354463184460 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70354463184460
47
+ - !ruby/object:Gem::Dependency
48
+ name: database_cleaner
49
+ requirement: &70354463183900 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70354463183900
58
+ description: ! 'has_normalized_fields is a Ruby on Rails gem that lets you normalize
59
+ user data for an improved user experience.
60
+
61
+ It takes the messy user inputed data and normalizes it into a nice clean standard
62
+ format.'
63
+ email: kylejginavan@gmail.com
64
+ executables: []
65
+ extensions: []
66
+ extra_rdoc_files: []
67
+ files:
68
+ - .gitignore
69
+ - .rspec
70
+ - .travis.yml
71
+ - Gemfile
72
+ - README.rdoc
73
+ - Rakefile
74
+ - has_normalized_fields.gemspec
75
+ - lib/has_normalized_fields.rb
76
+ - lib/version.rb
77
+ - spec/db/database.yml
78
+ - spec/db/schema.rb
79
+ - spec/db/test.sqlite3
80
+ - spec/has_normalized_fields_spec.rb
81
+ - spec/spec_helper.rb
82
+ homepage: https://github.com/kylejginavan/has_normalized_fields
83
+ licenses: []
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ! '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubyforge_project: has_normalized_fields
102
+ rubygems_version: 1.8.15
103
+ signing_key:
104
+ specification_version: 3
105
+ summary: Ruby on Rails gem for normalize data prior to save
106
+ test_files:
107
+ - spec/db/database.yml
108
+ - spec/db/schema.rb
109
+ - spec/db/test.sqlite3
110
+ - spec/has_normalized_fields_spec.rb
111
+ - spec/spec_helper.rb