normalize_attributes 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source :rubygems
2
+
3
+ gem "rspec", "2.0.0.rc"
4
+ gem "activerecord", "3.0.0"
5
+ gem "sqlite3-ruby"
data/Gemfile.lock ADDED
@@ -0,0 +1,38 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activemodel (3.0.0)
5
+ activesupport (= 3.0.0)
6
+ builder (~> 2.1.2)
7
+ i18n (~> 0.4.1)
8
+ activerecord (3.0.0)
9
+ activemodel (= 3.0.0)
10
+ activesupport (= 3.0.0)
11
+ arel (~> 1.0.0)
12
+ tzinfo (~> 0.3.23)
13
+ activesupport (3.0.0)
14
+ arel (1.0.1)
15
+ activesupport (~> 3.0.0)
16
+ builder (2.1.2)
17
+ diff-lcs (1.1.2)
18
+ i18n (0.4.1)
19
+ rspec (2.0.0.rc)
20
+ rspec-core (= 2.0.0.rc)
21
+ rspec-expectations (= 2.0.0.rc)
22
+ rspec-mocks (= 2.0.0.rc)
23
+ rspec-core (2.0.0.rc)
24
+ rspec-expectations (2.0.0.rc)
25
+ diff-lcs (>= 1.1.2)
26
+ rspec-mocks (2.0.0.rc)
27
+ rspec-core (= 2.0.0.rc)
28
+ rspec-expectations (= 2.0.0.rc)
29
+ sqlite3-ruby (1.3.1)
30
+ tzinfo (0.3.23)
31
+
32
+ PLATFORMS
33
+ ruby
34
+
35
+ DEPENDENCIES
36
+ activerecord (= 3.0.0)
37
+ rspec (= 2.0.0.rc)
38
+ sqlite3-ruby
data/README.rdoc ADDED
@@ -0,0 +1,73 @@
1
+ = Normalize Attributes
2
+
3
+ Sometimes you want to normalize data before saving it to the database like down casing e-mails, removing spaces and so on. This Rails plugin allows you to do so in a simple way.
4
+
5
+ == Usage
6
+
7
+ To install:
8
+
9
+ gem install normalize_attributes
10
+
11
+ Then on your model:
12
+
13
+ class User < ActiveRecord::Base
14
+ normalize_attribute :email, :with => :downcase
15
+ end
16
+
17
+ The example above will normalize your <tt>:email</tt> attribute on the <tt>before_save</tt> callback.
18
+
19
+ You can specify multiple attributes
20
+
21
+ normalize_attributes :email, :username, :with => :downcase
22
+
23
+ You can use a block
24
+
25
+ normalize_attribute :name do |value|
26
+ value.squish
27
+ end
28
+
29
+ You can combine both
30
+
31
+ normalize_attribute :name, :with => :downcase do |value|
32
+ value.squish
33
+ end
34
+
35
+ The <tt>squish</tt> method is the default normalizer for strings. All you need to is specify the attribute:
36
+
37
+ normalize_attribute :content
38
+
39
+ The <tt>compact</tt> method is the default normalizer for arrays (when using <tt>serialize</tt> method):
40
+
41
+ class User < ActiveRecord::Base
42
+ serialize :preferences, Array
43
+ normalize_attribute :preferences
44
+ end
45
+
46
+ The <tt>normalize_attributes</tt> method is aliased as <tt>normalize</tt>, <tt>normalize_attribute</tt>, <tt>normalize_attr</tt>, and <tt>normalize_attrs</tt>.
47
+
48
+ == Maintainer
49
+
50
+ * Nando Vieira (http://nandovieira.com.br)
51
+
52
+ == License:
53
+
54
+ (The MIT License)
55
+
56
+ Permission is hereby granted, free of charge, to any person obtaining
57
+ a copy of this software and associated documentation files (the
58
+ 'Software'), to deal in the Software without restriction, including
59
+ without limitation the rights to use, copy, modify, merge, publish,
60
+ distribute, sublicense, and/or sell copies of the Software, and to
61
+ permit persons to whom the Software is furnished to do so, subject to
62
+ the following conditions:
63
+
64
+ The above copyright notice and this permission notice shall be
65
+ included in all copies or substantial portions of the Software.
66
+
67
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
68
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
69
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
70
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
71
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
72
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
73
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,32 @@
1
+ require "rake"
2
+ require "rspec/core/rake_task"
3
+ require "./lib/normalize_attributes/version"
4
+
5
+ desc "Default: run specs."
6
+ task :default => :spec
7
+
8
+ desc "Run the specs"
9
+ RSpec::Core::RakeTask.new do |t|
10
+ t.ruby_opts = %w[-Ilib -Ispec]
11
+ end
12
+
13
+ begin
14
+ require "jeweler"
15
+
16
+ JEWEL = Jeweler::Tasks.new do |gem|
17
+ gem.name = "normalize_attributes"
18
+ gem.version = NormalizeAttributes::Version::STRING
19
+ gem.summary = "Normalize ActiveRecord attributes"
20
+ gem.description = "Normalize ActiveRecord attributes"
21
+ gem.authors = ["Nando Vieira"]
22
+ gem.email = "fnando.vieira@gmail.com"
23
+ gem.homepage = "http://github.com/fnando/normalize_attributes"
24
+ gem.has_rdoc = false
25
+ gem.add_dependency "activerecord"
26
+ gem.files = FileList["{Gemfile,Gemfile.lock,Rakefile,MIT-LICENSE,normalize_attributes.gemspec,README.rdoc}", "{lib,spec,templates}/**/*"]
27
+ end
28
+
29
+ Jeweler::GemcutterTasks.new
30
+ rescue LoadError => e
31
+ puts "You don't Jeweler installed, so you won't be able to build gems."
32
+ end
@@ -0,0 +1,4 @@
1
+ require "active_record"
2
+ require "active_support/core_ext/string/filters"
3
+ require "normalize_attributes/version"
4
+ require "normalize_attributes/active_record"
@@ -0,0 +1,69 @@
1
+ module NormalizeAttributes
2
+ module ActiveRecord
3
+ def self.included(base)
4
+ base.instance_eval do
5
+ extend ClassMethods
6
+ include InstanceMethods
7
+ before_save :normalize_attributes
8
+
9
+ class << self
10
+ attr_accessor :normalize_attributes_options
11
+ end
12
+ end
13
+ end
14
+
15
+ module ClassMethods
16
+ def normalize_attributes(*args, &block)
17
+ self.normalize_attributes_options ||= {}
18
+ options = args.extract_options!
19
+
20
+ args.each do |attr_name|
21
+ attr_name = attr_name.to_sym
22
+
23
+ normalize_attributes_options.tap do |o|
24
+ o[attr_name] ||= []
25
+ o[attr_name] << options[:with] if options[:with]
26
+ o[attr_name] << block if block_given?
27
+ end
28
+ end
29
+ end
30
+
31
+ alias_method :normalize, :normalize_attributes
32
+ alias_method :normalize_attribute, :normalize_attributes
33
+ alias_method :normalize_attr, :normalize_attributes
34
+ alias_method :normalize_attrs, :normalize_attributes
35
+ end
36
+
37
+ module InstanceMethods
38
+ private
39
+ def normalize_attributes
40
+ options = self.class.normalize_attributes_options || {}
41
+
42
+ options.each do |attr_name, normalizers|
43
+ value = self.send(attr_name)
44
+
45
+ if normalizers.empty?
46
+ case value
47
+ when String
48
+ normalizers << :squish
49
+ when Array
50
+ normalizers << :compact
51
+ end
52
+ end
53
+
54
+ [normalizers].flatten.each do |normalizer|
55
+ if normalizer.respond_to?(:call)
56
+ value = normalizer.call(value)
57
+ elsif value.respond_to?(normalizer)
58
+ value = value.send(normalizer)
59
+ end
60
+ end
61
+
62
+ write_attribute attr_name, value
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
68
+
69
+ ActiveRecord::Base.send :include, NormalizeAttributes::ActiveRecord
@@ -0,0 +1,8 @@
1
+ module NormalizeAttributes
2
+ module Version
3
+ MAJOR = 0
4
+ MINOR = 1
5
+ PATCH = 3
6
+ STRING = "#{MAJOR}.#{MINOR}.#{PATCH}"
7
+ end
8
+ end
@@ -0,0 +1,59 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{normalize_attributes}
8
+ s.version = "0.1.3"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Nando Vieira"]
12
+ s.date = %q{2010-10-07}
13
+ s.description = %q{Normalize ActiveRecord attributes}
14
+ s.email = %q{fnando.vieira@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "README.rdoc"
17
+ ]
18
+ s.files = [
19
+ "Gemfile",
20
+ "Gemfile.lock",
21
+ "README.rdoc",
22
+ "Rakefile",
23
+ "lib/normalize_attributes.rb",
24
+ "lib/normalize_attributes/active_record.rb",
25
+ "lib/normalize_attributes/version.rb",
26
+ "normalize_attributes.gemspec",
27
+ "spec/normalize_attributes_spec.rb",
28
+ "spec/schema.rb",
29
+ "spec/spec_helper.rb",
30
+ "spec/support/token.rb",
31
+ "spec/support/user.rb"
32
+ ]
33
+ s.homepage = %q{http://github.com/fnando/normalize_attributes}
34
+ s.rdoc_options = ["--charset=UTF-8"]
35
+ s.require_paths = ["lib"]
36
+ s.rubygems_version = %q{1.3.7}
37
+ s.summary = %q{Normalize ActiveRecord attributes}
38
+ s.test_files = [
39
+ "spec/normalize_attributes_spec.rb",
40
+ "spec/schema.rb",
41
+ "spec/spec_helper.rb",
42
+ "spec/support/token.rb",
43
+ "spec/support/user.rb"
44
+ ]
45
+
46
+ if s.respond_to? :specification_version then
47
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
48
+ s.specification_version = 3
49
+
50
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
51
+ s.add_runtime_dependency(%q<activerecord>, [">= 0"])
52
+ else
53
+ s.add_dependency(%q<activerecord>, [">= 0"])
54
+ end
55
+ else
56
+ s.add_dependency(%q<activerecord>, [">= 0"])
57
+ end
58
+ end
59
+
@@ -0,0 +1,81 @@
1
+ require "spec_helper"
2
+
3
+ describe "Normalize Attributes" do
4
+ before do
5
+ User.normalize_attributes_options = {}
6
+ end
7
+
8
+ it "should respond to aliases" do
9
+ User.should respond_to(:normalize)
10
+ User.should respond_to(:normalize_attrs)
11
+ User.should respond_to(:normalize_attr)
12
+ User.should respond_to(:normalize_attribute)
13
+ end
14
+
15
+ it "should apply single normalization method" do
16
+ User.normalize_attribute :email, :with => :downcase
17
+ user = User.create(:email => "JOHN@DOE.COM")
18
+
19
+ user.email.should == "john@doe.com"
20
+ end
21
+
22
+ it "should apply multiple normalization methods" do
23
+ User.normalize_attribute :email, :with => [:downcase, :reverse]
24
+ user = User.create(:email => "JOHN@DOE.COM")
25
+
26
+ user.email.should == "moc.eod@nhoj"
27
+ end
28
+
29
+ it "should apply proc" do
30
+ User.normalize_attribute(:email) {|v| v.downcase }
31
+ user = User.create(:email => "JOHN@DOE.COM")
32
+
33
+ user.email.should == "john@doe.com"
34
+ end
35
+
36
+ it "should combine both method names and procs as normalization methods" do
37
+ User.normalize_attribute(:email, :with => :downcase) {|v| v.reverse }
38
+ user = User.create(:email => "JOHN@DOE.COM")
39
+
40
+ user.email.should == "moc.eod@nhoj"
41
+ end
42
+
43
+ it "should normalize multiple attributes" do
44
+ User.normalize_attributes :email, :username, :with => :downcase
45
+ user = User.create(:email => "JOHN@DOE.COM", :username => "JOHN")
46
+
47
+ user.email.should == "john@doe.com"
48
+ user.username.should == "john"
49
+ end
50
+
51
+ it "should not apply on associations" do
52
+ User.normalize_attributes :email, :with => :downcase
53
+ user = User.create(:email => "JOHN@DOE.COM", :username => "JOHN")
54
+
55
+ expect {
56
+ user.tokens.create!
57
+ }.to_not raise_error
58
+ end
59
+
60
+ it "should apply default filter on strings" do
61
+ User.normalize_attributes :email
62
+ user = User.create(:email => " \n\t john@doe.com \t\t\n\r\n", :username => "john")
63
+
64
+ user.email.should == "john@doe.com"
65
+ end
66
+
67
+ it "should apply default filter on arrays" do
68
+ User.normalize_attributes :preferences
69
+ user = User.create(:preferences => [nil, :games, :music])
70
+ user.preferences.should == [:games, :music]
71
+ end
72
+
73
+ it "should not apply default filter on unknown objects" do
74
+ User.normalize_attributes :username
75
+
76
+ expect {
77
+ user = User.create(:username => 100)
78
+ user.username.should == 100
79
+ }.to_not raise_error
80
+ end
81
+ end
data/spec/schema.rb ADDED
@@ -0,0 +1,11 @@
1
+ ActiveRecord::Schema.define(:version => 0) do
2
+ create_table :users do |t|
3
+ t.string :email, :username
4
+ t.text :preferences
5
+ end
6
+
7
+ create_table :tokens do |t|
8
+ t.string :name
9
+ t.references :user
10
+ end
11
+ end
@@ -0,0 +1,9 @@
1
+ require "rspec"
2
+ require "normalize_attributes"
3
+
4
+ ActiveRecord::Base.establish_connection :adapter => "sqlite3", :database => ":memory:"
5
+
6
+ # Load database schema
7
+ load "schema.rb"
8
+
9
+ Dir[File.dirname(__FILE__) + "/support/**/*.rb"].each {|f| require f}
@@ -0,0 +1,3 @@
1
+ class Token < ActiveRecord::Base
2
+ belongs_to :user
3
+ end
@@ -0,0 +1,4 @@
1
+ class User < ActiveRecord::Base
2
+ has_many :tokens
3
+ serialize :preferences
4
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: normalize_attributes
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 3
9
+ version: 0.1.3
10
+ platform: ruby
11
+ authors:
12
+ - Nando Vieira
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-10-07 00:00:00 -03:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: activerecord
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ type: :runtime
32
+ version_requirements: *id001
33
+ description: Normalize ActiveRecord attributes
34
+ email: fnando.vieira@gmail.com
35
+ executables: []
36
+
37
+ extensions: []
38
+
39
+ extra_rdoc_files:
40
+ - README.rdoc
41
+ files:
42
+ - Gemfile
43
+ - Gemfile.lock
44
+ - README.rdoc
45
+ - Rakefile
46
+ - lib/normalize_attributes.rb
47
+ - lib/normalize_attributes/active_record.rb
48
+ - lib/normalize_attributes/version.rb
49
+ - normalize_attributes.gemspec
50
+ - spec/normalize_attributes_spec.rb
51
+ - spec/schema.rb
52
+ - spec/spec_helper.rb
53
+ - spec/support/token.rb
54
+ - spec/support/user.rb
55
+ has_rdoc: true
56
+ homepage: http://github.com/fnando/normalize_attributes
57
+ licenses: []
58
+
59
+ post_install_message:
60
+ rdoc_options:
61
+ - --charset=UTF-8
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ segments:
70
+ - 0
71
+ version: "0"
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ segments:
78
+ - 0
79
+ version: "0"
80
+ requirements: []
81
+
82
+ rubyforge_project:
83
+ rubygems_version: 1.3.7
84
+ signing_key:
85
+ specification_version: 3
86
+ summary: Normalize ActiveRecord attributes
87
+ test_files:
88
+ - spec/normalize_attributes_spec.rb
89
+ - spec/schema.rb
90
+ - spec/spec_helper.rb
91
+ - spec/support/token.rb
92
+ - spec/support/user.rb