has_normalized_attributes 0.0.2

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.
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ *.gem
2
+ .bundle
3
+ .rvmrc
4
+ Gemfile.lock
5
+ pkg/*
6
+ log/*.log
7
+ *.tmproj
8
+ tmp
9
+ log
10
+ .gitconfig
11
+ bin/*
12
+ .rbenv-version
13
+ .rbenv-gemsets
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
+ == HasNormalizedAttributes {<img src="https://secure.travis-ci.org/kylejginavan/has_normalized_attributes.png"/>}[http://travis-ci.org/kylejginavan/has_normalized_attributes]
2
+
3
+ == DESCRIPTION
4
+
5
+ has_normalized_attributes 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_attributes'
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_attributes}
7
+ s.version = HasNormalizedAttributes::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_attributes"
12
+ s.summary = %q{Ruby on Rails gem for normalize data prior to save}
13
+ s.description = %q{has_normalized_attributes 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_attributes"
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,53 @@
1
+ module HasNormalizedAttributes
2
+ extend ActiveSupport::Concern
3
+
4
+ #CONSTANT
5
+ ZIPCODE = /[-. )(,]/
6
+ PHONE = /[-. )(,]|(^0)/
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 = HasNormalizedAttributes.const_get(arg.upcase)
18
+ define_method "normalize_#{arg}" do
19
+ self && is_a?(String) && match(reg_exp) ? gsub!(reg_exp,'') : self
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
+ define_method "#{field.to_s}=" do |value|
37
+ if value.present?
38
+ value.send("normalize_#{normalization_type.downcase}".to_sym)
39
+ end
40
+ super value
41
+ end
42
+ end
43
+
44
+ end
45
+ end
46
+ end
47
+
48
+ #extend these classes
49
+ [String, Fixnum, Float, NilClass].each do |klass|
50
+ klass.send(:include, HasNormalizedAttributes)
51
+ end
52
+ #include activerecord
53
+ ActiveRecord::Base.send :include, HasNormalizedAttributes
data/lib/version.rb ADDED
@@ -0,0 +1,3 @@
1
+ module HasNormalizedAttributes
2
+ VERSION = "0.0.2"
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,115 @@
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 "HasNormalizedAttributes" 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
+ end
27
+
28
+ describe "#phone" do
29
+ it{@resource.phone_attr = "11-111"; @resource.phone_attr.should == "11111"}
30
+ it{@resource.phone_attr = "1111111111"; @resource.phone_attr.should == "1111111111"}
31
+ it{@resource.phone_attr = "111 111 1111"; @resource.phone_attr.should == "1111111111"}
32
+ it{@resource.phone_attr = " 111 111 1111 "; @resource.phone_attr.should == "1111111111"}
33
+ it{@resource.phone_attr = "111.111.1111"; @resource.phone_attr.should == "1111111111"}
34
+ it{@resource.phone_attr = "(111)111-1111"; @resource.phone_attr.should == "1111111111"}
35
+ it{@resource.phone_attr = "(111)1111111"; @resource.phone_attr.should == "1111111111"}
36
+ it{@resource.phone_attr = " 111-111.1111 "; @resource.phone_attr.should == "1111111111"}
37
+ it{@resource.phone_attr = "0111-111.1111 "; @resource.phone_attr.should == "1111111111"}
38
+ it{@resource.phone_attr = "1011-111.1111 "; @resource.phone_attr.should == "10111111111"}
39
+ it{@resource.phone_attr = "111-0222.333 "; @resource.phone_attr.should == "1110222333"}
40
+ it{@resource.phone_attr = ""; @resource.phone_attr.should == ""}
41
+ it{@resource.phone_attr = nil; @resource.phone_attr.should == nil}
42
+ end
43
+
44
+ describe "#zipcode" do
45
+ it{@resource.zipcode_attr = "11111"; @resource.zipcode_attr.should == "11111"}
46
+ it{@resource.zipcode_attr = " 11 1-11 "; @resource.zipcode_attr.should == "11111"}
47
+ it{@resource.zipcode_attr = " 111-11 ("; @resource.zipcode_attr.should == "11111"}
48
+ it{@resource.zipcode_attr = "11,1-11"; @resource.zipcode_attr.should == "11111"}
49
+ it{@resource.zipcode_attr = "11.111"; @resource.zipcode_attr.should == "11111"}
50
+ it{@resource.zipcode_attr = "111111111"; @resource.zipcode_attr.should == "111111111"}
51
+ it{@resource.zipcode_attr = "(11111) 1111"; @resource.zipcode_attr.should == "111111111"}
52
+ it{@resource.zipcode_attr = "11111) -1111"; @resource.zipcode_attr.should == "111111111"}
53
+ it{@resource.zipcode_attr = "11111.1111"; @resource.zipcode_attr.should == "111111111"}
54
+ it{@resource.zipcode_attr = "11111 --1111"; @resource.zipcode_attr.should == "111111111"}
55
+ it{@resource.zipcode_attr = " 11111,1111 "; @resource.zipcode_attr.should == "111111111"}
56
+ it{@resource.zipcode_attr = ""; @resource.zipcode_attr.should == ""}
57
+ it{@resource.zipcode_attr = nil; @resource.zipcode_attr.should == nil}
58
+ it{@resource.zipcode_attr = "(111)"; @resource.zipcode_attr.should == "111"}
59
+ it{@resource.zipcode_attr = "11111"; @resource.zipcode_attr.should == "11111"}
60
+ it{@resource.zipcode_attr = 111.11; @resource.zipcode_attr.should == 111.11}
61
+ end
62
+
63
+ describe "#ssn" do
64
+ it{@resource.ssn_attr = "111 111 1111"; @resource.ssn_attr.should == "1111111111"}
65
+ it{@resource.ssn_attr = " 111 111 1111 "; @resource.ssn_attr.should == "1111111111"}
66
+ it{@resource.ssn_attr = "111.111.1111"; @resource.ssn_attr.should == "1111111111"}
67
+ it{@resource.ssn_attr = "(111)111-1111"; @resource.ssn_attr.should == "1111111111"}
68
+ it{@resource.ssn_attr = "(111)1111111"; @resource.ssn_attr.should == "1111111111"}
69
+ it{@resource.ssn_attr = " 111-111.1111 "; @resource.ssn_attr.should == "1111111111"}
70
+ it{@resource.ssn_attr = ""; @resource.ssn_attr.should == ""}
71
+ it{@resource.ssn_attr = nil; @resource.ssn_attr.should == nil}
72
+ end
73
+
74
+ describe "#taxid" do
75
+ it{@resource.taxid_attr = "12-345"; @resource.taxid_attr.should == "12345"}
76
+ it{@resource.taxid_attr = "12.345"; @resource.taxid_attr.should == "12345"}
77
+ it{@resource.taxid_attr = "1-2345"; @resource.taxid_attr.should == "12345"}
78
+ it{@resource.taxid_attr = "1,2345"; @resource.taxid_attr.should == "12345"}
79
+ it{@resource.taxid_attr = ""; @resource.taxid_attr.should == ""}
80
+ it{@resource.taxid_attr = nil; @resource.taxid_attr.should == nil}
81
+ end
82
+
83
+ describe "#dollar" do
84
+ it{@resource.dollar_attr = "$111111";@resource.dollar_attr.should == "111111"}
85
+ it{@resource.dollar_attr = "111,111";@resource.dollar_attr.should == "111111"}
86
+ it{@resource.dollar_attr = "111 111 ";@resource.dollar_attr.should == "111111"}
87
+ it{@resource.dollar_attr = "$111, 111 ";@resource.dollar_attr.should == "111111"}
88
+ it{@resource.dollar_attr = "";@resource.dollar_attr.should == ""}
89
+ it{@resource.dollar_attr = nil;@resource.dollar_attr.should == nil}
90
+ end
91
+
92
+ describe "#number" do
93
+ it{@resource.number_attr = "1,23";@resource.number_attr.should == "123"}
94
+ it{@resource.number_attr = "1 23 ";@resource.number_attr.should == "123"}
95
+ it{@resource.number_attr = "";@resource.number_attr.should == ""}
96
+ it{@resource.number_attr = nil;@resource.number_attr.should == nil}
97
+ end
98
+
99
+ describe "#percent" do
100
+ it{@resource.percent_attr = " 1 1 ";@resource.percent_attr.should == "11"}
101
+ it{@resource.percent_attr = "11%";@resource.percent_attr.should == "11"}
102
+ it{@resource.percent_attr = "%11";@resource.percent_attr.should == "11"}
103
+ it{@resource.percent_attr = "1 1 % ";@resource.percent_attr.should == "11"}
104
+ it{@resource.percent_attr = "";@resource.percent_attr.should == ""}
105
+ it{@resource.percent_attr = nil;@resource.percent_attr.should == nil}
106
+ end
107
+
108
+ describe "#spaces" do
109
+ it{@resource.spaces_attr = "5 0";@resource.spaces_attr.should == "50"}
110
+ it{@resource.spaces_attr = "";@resource.spaces_attr.should == ""}
111
+ it{@resource.spaces_attr = nil;@resource.spaces_attr.should == nil}
112
+ end
113
+
114
+
115
+ 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_attributes'
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,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: has_normalized_attributes
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.2
6
+ platform: ruby
7
+ authors:
8
+ - Kyle Ginavan
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2010-05-18 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ prerelease: false
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ type: :development
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: activerecord
28
+ prerelease: false
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: 3.1.0
35
+ type: :development
36
+ version_requirements: *id002
37
+ - !ruby/object:Gem::Dependency
38
+ name: sqlite3
39
+ prerelease: false
40
+ requirement: &id003 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ type: :development
47
+ version_requirements: *id003
48
+ - !ruby/object:Gem::Dependency
49
+ name: database_cleaner
50
+ prerelease: false
51
+ requirement: &id004 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ type: :development
58
+ version_requirements: *id004
59
+ description: |-
60
+ has_normalized_attributes is a Ruby on Rails gem that lets you normalize user data for an improved user experience.
61
+ It takes the messy user inputed data and normalizes it into a nice clean standard format.
62
+ email: kylejginavan@gmail.com
63
+ executables: []
64
+
65
+ extensions: []
66
+
67
+ extra_rdoc_files: []
68
+
69
+ files:
70
+ - .gitignore
71
+ - .rspec
72
+ - .travis.yml
73
+ - Gemfile
74
+ - README.rdoc
75
+ - Rakefile
76
+ - has_normalized_attributes.gemspec
77
+ - lib/has_normalized_attributes.rb
78
+ - lib/version.rb
79
+ - spec/db/database.yml
80
+ - spec/db/schema.rb
81
+ - spec/db/test.sqlite3
82
+ - spec/has_normalized_fields_spec.rb
83
+ - spec/spec_helper.rb
84
+ homepage: https://github.com/kylejginavan/has_normalized_attributes
85
+ licenses: []
86
+
87
+ post_install_message:
88
+ rdoc_options: []
89
+
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: "0"
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: "0"
104
+ requirements: []
105
+
106
+ rubyforge_project: has_normalized_attributes
107
+ rubygems_version: 1.8.15
108
+ signing_key:
109
+ specification_version: 3
110
+ summary: Ruby on Rails gem for normalize data prior to save
111
+ test_files:
112
+ - spec/db/database.yml
113
+ - spec/db/schema.rb
114
+ - spec/db/test.sqlite3
115
+ - spec/has_normalized_fields_spec.rb
116
+ - spec/spec_helper.rb