normalize_attributes 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ .DS_Store
2
+ pkg
data/Gemfile CHANGED
@@ -1,5 +1,3 @@
1
1
  source :rubygems
2
2
 
3
- gem "rspec", "2.0.0"
4
- gem "activerecord", "3.0.1"
5
- gem "sqlite3-ruby"
3
+ gemspec
data/Gemfile.lock CHANGED
@@ -1,6 +1,23 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ normalize_attributes (0.1.5)
5
+ activerecord (>= 3.0.0)
6
+
1
7
  GEM
2
8
  remote: http://rubygems.org/
3
9
  specs:
10
+ abstract (1.0.0)
11
+ actionpack (3.0.1)
12
+ activemodel (= 3.0.1)
13
+ activesupport (= 3.0.1)
14
+ builder (~> 2.1.2)
15
+ erubis (~> 2.6.6)
16
+ i18n (~> 0.4.1)
17
+ rack (~> 1.2.1)
18
+ rack-mount (~> 0.6.12)
19
+ rack-test (~> 0.5.4)
20
+ tzinfo (~> 0.3.23)
4
21
  activemodel (3.0.1)
5
22
  activesupport (= 3.0.1)
6
23
  builder (~> 2.1.2)
@@ -11,28 +28,50 @@ GEM
11
28
  arel (~> 1.0.0)
12
29
  tzinfo (~> 0.3.23)
13
30
  activesupport (3.0.1)
31
+ archive-tar-minitar (0.5.2)
14
32
  arel (1.0.1)
15
33
  activesupport (~> 3.0.0)
16
34
  builder (2.1.2)
35
+ columnize (0.3.2)
17
36
  diff-lcs (1.1.2)
18
- i18n (0.4.1)
19
- rspec (2.0.0)
20
- rspec-core (= 2.0.0)
21
- rspec-expectations (= 2.0.0)
22
- rspec-mocks (= 2.0.0)
23
- rspec-core (2.0.0)
24
- rspec-expectations (2.0.0)
25
- diff-lcs (>= 1.1.2)
26
- rspec-mocks (2.0.0)
27
- rspec-core (= 2.0.0)
28
- rspec-expectations (= 2.0.0)
29
- sqlite3-ruby (1.3.1)
37
+ erubis (2.6.6)
38
+ abstract (>= 1.0.0)
39
+ i18n (0.4.2)
40
+ linecache19 (0.5.11)
41
+ ruby_core_source (>= 0.1.4)
42
+ rack (1.2.1)
43
+ rack-mount (0.6.13)
44
+ rack (>= 1.0.0)
45
+ rack-test (0.5.6)
46
+ rack (>= 1.0)
47
+ rspec (2.1.0)
48
+ rspec-core (~> 2.1.0)
49
+ rspec-expectations (~> 2.1.0)
50
+ rspec-mocks (~> 2.1.0)
51
+ rspec-core (2.1.0)
52
+ rspec-expectations (2.1.0)
53
+ diff-lcs (~> 1.1.2)
54
+ rspec-mocks (2.1.0)
55
+ ruby-debug-base19 (0.11.24)
56
+ columnize (>= 0.3.1)
57
+ linecache19 (>= 0.5.11)
58
+ ruby_core_source (>= 0.1.4)
59
+ ruby-debug19 (0.11.6)
60
+ columnize (>= 0.3.1)
61
+ linecache19 (>= 0.5.11)
62
+ ruby-debug-base19 (>= 0.11.19)
63
+ ruby_core_source (0.1.4)
64
+ archive-tar-minitar (>= 0.5.2)
65
+ sqlite3-ruby (1.3.2)
30
66
  tzinfo (0.3.23)
31
67
 
32
68
  PLATFORMS
33
69
  ruby
34
70
 
35
71
  DEPENDENCIES
36
- activerecord (= 3.0.1)
37
- rspec (= 2.0.0)
72
+ actionpack
73
+ activerecord (>= 3.0.0)
74
+ normalize_attributes!
75
+ rspec (>= 2.0.0)
76
+ ruby-debug19
38
77
  sqlite3-ruby
data/README.rdoc CHANGED
@@ -11,39 +11,46 @@ To install:
11
11
  Then on your model:
12
12
 
13
13
  class User < ActiveRecord::Base
14
- normalize_attribute :email, :with => :downcase
14
+ normalize :email, :with => :downcase
15
15
  end
16
16
 
17
17
  The example above will normalize your <tt>:email</tt> attribute on the <tt>before_save</tt> callback.
18
18
 
19
19
  You can specify multiple attributes
20
20
 
21
- normalize_attributes :email, :username, :with => :downcase
21
+ normalize :email, :username, :with => :downcase
22
22
 
23
23
  You can use a block
24
24
 
25
- normalize_attribute :name do |value|
25
+ normalize :name do |value|
26
26
  value.squish
27
27
  end
28
28
 
29
29
  You can combine both
30
30
 
31
- normalize_attribute :name, :with => :downcase do |value|
31
+ normalize :name, :with => :downcase do |value|
32
32
  value.squish
33
33
  end
34
34
 
35
35
  The <tt>squish</tt> method is the default normalizer for strings. All you need to is specify the attribute:
36
36
 
37
- normalize_attribute :content
37
+ normalize :content
38
38
 
39
39
  The <tt>compact</tt> method is the default normalizer for arrays (when using <tt>serialize</tt> method):
40
40
 
41
41
  class User < ActiveRecord::Base
42
42
  serialize :preferences, Array
43
- normalize_attribute :preferences
43
+ normalize :preferences
44
44
  end
45
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>.
46
+ The <tt>normalize</tt> method is aliased as <tt>normalize_attributes</tt>, <tt>normalize_attribute</tt>, <tt>normalize_attr</tt>, and <tt>normalize_attrs</tt>.
47
+
48
+ You can normalize the attribute before type casting; this is specially useful for normalizing
49
+ dates and numbers.
50
+
51
+ class Product
52
+ normalize(:price, :raw => true) {|v| Money.new(v).to_f}
53
+ end
47
54
 
48
55
  == Maintainer
49
56
 
data/Rakefile CHANGED
@@ -1,32 +1,5 @@
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
1
+ require "bundler"
2
+ Bundler::GemHelper.install_tasks
12
3
 
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
4
+ require "rspec/core/rake_task"
5
+ RSpec::Core::RakeTask.new
@@ -2,3 +2,5 @@ require "active_record"
2
2
  require "active_support/core_ext/string/filters"
3
3
  require "normalize_attributes/version"
4
4
  require "normalize_attributes/active_record"
5
+
6
+ ActiveRecord::Base.send :include, NormalizeAttributes::ActiveRecord
@@ -7,65 +7,72 @@ module NormalizeAttributes
7
7
  before_validation :normalize_attributes
8
8
 
9
9
  class << self
10
- attr_accessor :normalize_attributes_options
10
+ attr_accessor :normalize_options
11
11
  end
12
12
  end
13
13
  end
14
14
 
15
15
  module ClassMethods
16
- def normalize_attributes(*args, &block)
17
- self.normalize_attributes_options ||= {}
16
+ def normalize(*args, &block)
17
+ self.normalize_options ||= {}
18
18
  options = args.extract_options!
19
19
 
20
20
  args.each do |attr_name|
21
21
  attr_name = attr_name.to_sym
22
22
 
23
- normalize_attributes_options.tap do |o|
23
+ normalize_options.tap do |o|
24
24
  o[attr_name] ||= []
25
- o[attr_name] << options[:with] if options[:with]
26
- o[attr_name] << block if block_given?
25
+ o[attr_name] << [[block, options[:with]].flatten.compact, options.except(:with)]
27
26
  end
28
27
  end
29
28
  end
30
29
 
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
30
+ alias_method :normalize_attributes, :normalize
31
+ alias_method :normalize_attribute, :normalize
32
+ alias_method :normalize_attr, :normalize
33
+ alias_method :normalize_attrs, :normalize
35
34
  end
36
35
 
37
36
  module InstanceMethods
38
37
  private
39
38
  def normalize_attributes
40
- options = self.class.normalize_attributes_options || {}
39
+ return unless self.class.normalize_options
41
40
 
42
- options.each do |attr_name, normalizers|
43
- value = self.send("#{attr_name}_before_type_cast")
44
-
45
- if normalizers.empty?
46
- case value
47
- when String
48
- normalizers << :squish
49
- when Array
50
- normalizers << :compact
51
- end
41
+ self.class.normalize_options.each do |name, items|
42
+ items.each do |item|
43
+ apply_normalizers name, *item
52
44
  end
45
+ end
46
+ end
47
+
48
+ def apply_normalizers(name, normalizers, options)
49
+ if options[:raw] && respond_to?("#{name}_before_type_cast")
50
+ value = send("#{name}_before_type_cast")
51
+ else
52
+ value = send(name)
53
+ end
53
54
 
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
- else
60
- value = send(normalizer, value)
61
- end
55
+ if normalizers.empty?
56
+ case value
57
+ when String
58
+ normalizers << :squish
59
+ when Array
60
+ normalizers << :compact
62
61
  end
62
+ end
63
63
 
64
- write_attribute attr_name, value
64
+ normalizers.each do |normalizer|
65
+ if normalizer.respond_to?(:call)
66
+ value = normalizer.call(value)
67
+ elsif value.respond_to?(normalizer)
68
+ value = value.send(normalizer)
69
+ elsif respond_to?(normalizer)
70
+ value = send(normalizer, value)
71
+ end
65
72
  end
73
+
74
+ write_attribute name, value
66
75
  end
67
76
  end
68
77
  end
69
78
  end
70
-
71
- ActiveRecord::Base.send :include, NormalizeAttributes::ActiveRecord
@@ -2,7 +2,7 @@ module NormalizeAttributes
2
2
  module Version
3
3
  MAJOR = 0
4
4
  MINOR = 1
5
- PATCH = 5
5
+ PATCH = 6
6
6
  STRING = "#{MAJOR}.#{MINOR}.#{PATCH}"
7
7
  end
8
8
  end
@@ -1,59 +1,25 @@
1
- # Generated by jeweler
2
- # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
1
  # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "normalize_attributes/version"
5
4
 
6
5
  Gem::Specification.new do |s|
7
- s.name = %q{normalize_attributes}
8
- s.version = "0.1.5"
6
+ s.name = "normalize_attributes"
7
+ s.version = NormalizeAttributes::Version::STRING
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Nando Vieira"]
10
+ s.email = ["fnando.vieira@gmail.com"]
11
+ s.homepage = "http://rubygems.org/gems/normalize_attributes"
12
+ s.summary = "Normalize ActiveRecord attributes"
13
+ s.description = "Normalize ActiveRecord attributes"
9
14
 
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-18}
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"]
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
35
18
  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
19
 
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
20
+ s.add_dependency "activerecord", ">= 3.0.0"
21
+ s.add_development_dependency "rspec", ">= 2.0.0"
22
+ s.add_development_dependency "sqlite3-ruby"
23
+ s.add_development_dependency "actionpack"
24
+ s.add_development_dependency "ruby-debug19"
58
25
  end
59
-
@@ -2,46 +2,47 @@ require "spec_helper"
2
2
 
3
3
  describe "Normalize Attributes" do
4
4
  before do
5
- User.normalize_attributes_options = {}
5
+ User.normalize_options = {}
6
6
  end
7
7
 
8
8
  it "should respond to aliases" do
9
9
  User.should respond_to(:normalize)
10
- User.should respond_to(:normalize_attrs)
11
10
  User.should respond_to(:normalize_attr)
11
+ User.should respond_to(:normalize_attrs)
12
12
  User.should respond_to(:normalize_attribute)
13
+ User.should respond_to(:normalize_attributes)
13
14
  end
14
15
 
15
16
  it "should apply single normalization method" do
16
- User.normalize_attribute :email, :with => :downcase
17
+ User.normalize :email, :with => :downcase
17
18
  user = User.create(:email => "JOHN@DOE.COM")
18
19
 
19
20
  user.email.should == "john@doe.com"
20
21
  end
21
22
 
22
23
  it "should apply multiple normalization methods" do
23
- User.normalize_attribute :email, :with => [:downcase, :reverse]
24
+ User.normalize :email, :with => [:downcase, :reverse]
24
25
  user = User.create(:email => "JOHN@DOE.COM")
25
26
 
26
27
  user.email.should == "moc.eod@nhoj"
27
28
  end
28
29
 
29
30
  it "should apply proc" do
30
- User.normalize_attribute(:email) {|v| v.downcase }
31
+ User.normalize(:email) {|v| v.downcase }
31
32
  user = User.create(:email => "JOHN@DOE.COM")
32
33
 
33
34
  user.email.should == "john@doe.com"
34
35
  end
35
36
 
36
37
  it "should apply instance method" do
37
- User.normalize_attribute(:username, :with => :normalize_username)
38
+ User.normalize(:username, :with => :normalize_username)
38
39
  user = User.create(:username => "JOHNDOE")
39
40
 
40
41
  user.username.should == "johndoe"
41
42
  end
42
43
 
43
44
  it "should use value before type casting" do
44
- User.normalize_attribute(:age) do |v|
45
+ User.normalize(:age, :raw => true) do |v|
45
46
  v.should == "1.2"
46
47
  v.to_f * 10
47
48
  end
@@ -50,15 +51,19 @@ describe "Normalize Attributes" do
50
51
  user.age.should == 12
51
52
  end
52
53
 
54
+ it "should apply normalizers to accessor" do
55
+ # User.normalize()
56
+ end
57
+
53
58
  it "should combine both method names and procs as normalization methods" do
54
- User.normalize_attribute(:email, :with => :downcase) {|v| v.reverse }
59
+ User.normalize(:email, :with => :downcase) {|v| v.reverse }
55
60
  user = User.create(:email => "JOHN@DOE.COM")
56
61
 
57
62
  user.email.should == "moc.eod@nhoj"
58
63
  end
59
64
 
60
65
  it "should normalize multiple attributes" do
61
- User.normalize_attributes :email, :username, :with => :downcase
66
+ User.normalize :email, :username, :with => :downcase
62
67
  user = User.create(:email => "JOHN@DOE.COM", :username => "JOHN")
63
68
 
64
69
  user.email.should == "john@doe.com"
@@ -66,7 +71,7 @@ describe "Normalize Attributes" do
66
71
  end
67
72
 
68
73
  it "should not apply on associations" do
69
- User.normalize_attributes :email, :with => :downcase
74
+ User.normalize :email, :with => :downcase
70
75
  user = User.create(:email => "JOHN@DOE.COM", :username => "JOHN")
71
76
 
72
77
  expect {
@@ -75,24 +80,33 @@ describe "Normalize Attributes" do
75
80
  end
76
81
 
77
82
  it "should apply default filter on strings" do
78
- User.normalize_attributes :email
83
+ User.normalize :email
79
84
  user = User.create(:email => " \n\t john@doe.com \t\t\n\r\n", :username => "john")
80
85
 
81
86
  user.email.should == "john@doe.com"
82
87
  end
83
88
 
84
89
  it "should apply default filter on arrays" do
85
- User.normalize_attributes :preferences
90
+ User.normalize :preferences
86
91
  user = User.create(:preferences => [nil, :games, :music])
87
92
  user.preferences.should == [:games, :music]
88
93
  end
89
94
 
90
95
  it "should not apply default filter on unknown objects" do
91
- User.normalize_attributes :username
96
+ User.normalize :username
92
97
 
93
98
  expect {
94
99
  user = User.create(:username => 100)
95
100
  user.username.should == 100
96
101
  }.to_not raise_error
97
102
  end
103
+
104
+ it "should not apply filter when object do not respond to normalizer" do
105
+ User.normalize :username, :with => :missing
106
+
107
+ expect {
108
+ user = User.create(:username => nil)
109
+ user.save
110
+ }.to_not raise_error
111
+ end
98
112
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 5
9
- version: 0.1.5
8
+ - 6
9
+ version: 0.1.6
10
10
  platform: ruby
11
11
  authors:
12
12
  - Nando Vieira
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-10-18 00:00:00 -02:00
17
+ date: 2010-11-07 00:00:00 -02:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -26,19 +26,77 @@ dependencies:
26
26
  - - ">="
27
27
  - !ruby/object:Gem::Version
28
28
  segments:
29
+ - 3
29
30
  - 0
30
- version: "0"
31
+ - 0
32
+ version: 3.0.0
31
33
  type: :runtime
32
34
  version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: rspec
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ segments:
44
+ - 2
45
+ - 0
46
+ - 0
47
+ version: 2.0.0
48
+ type: :development
49
+ version_requirements: *id002
50
+ - !ruby/object:Gem::Dependency
51
+ name: sqlite3-ruby
52
+ prerelease: false
53
+ requirement: &id003 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ type: :development
62
+ version_requirements: *id003
63
+ - !ruby/object:Gem::Dependency
64
+ name: actionpack
65
+ prerelease: false
66
+ requirement: &id004 !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ segments:
72
+ - 0
73
+ version: "0"
74
+ type: :development
75
+ version_requirements: *id004
76
+ - !ruby/object:Gem::Dependency
77
+ name: ruby-debug19
78
+ prerelease: false
79
+ requirement: &id005 !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ segments:
85
+ - 0
86
+ version: "0"
87
+ type: :development
88
+ version_requirements: *id005
33
89
  description: Normalize ActiveRecord attributes
34
- email: fnando.vieira@gmail.com
90
+ email:
91
+ - fnando.vieira@gmail.com
35
92
  executables: []
36
93
 
37
94
  extensions: []
38
95
 
39
- extra_rdoc_files:
40
- - README.rdoc
96
+ extra_rdoc_files: []
97
+
41
98
  files:
99
+ - .gitignore
42
100
  - Gemfile
43
101
  - Gemfile.lock
44
102
  - README.rdoc
@@ -53,12 +111,12 @@ files:
53
111
  - spec/support/token.rb
54
112
  - spec/support/user.rb
55
113
  has_rdoc: true
56
- homepage: http://github.com/fnando/normalize_attributes
114
+ homepage: http://rubygems.org/gems/normalize_attributes
57
115
  licenses: []
58
116
 
59
117
  post_install_message:
60
- rdoc_options:
61
- - --charset=UTF-8
118
+ rdoc_options: []
119
+
62
120
  require_paths:
63
121
  - lib
64
122
  required_ruby_version: !ruby/object:Gem::Requirement