attribute_normalizer 0.3.1 → 1.0.0.pre1

Sign up to get free protection for your applications and to get access to all the features.
data/README.textile CHANGED
@@ -93,6 +93,12 @@ AttributeNormalizer.configure do |config|
93
93
  # You can change this if you would like as follows:
94
94
  # config.default_normalizers = :strip, :blank
95
95
 
96
+ # You can enable the attribute normalizers automatically if the specified attributes exist in your column_names. It will use
97
+ # the default normalizers for each attribute (e.g. config.default_normalizers)
98
+ # config.default_attributes = :name, :title
99
+
100
+ # Also, You can add an specific attribute to default_attributes using one or more normalizers:
101
+ # config.add_default_attribute :name, :with => :truncate
96
102
  end
97
103
  </code></pre>
98
104
 
@@ -46,5 +46,16 @@ module AttributeNormalizer
46
46
  end
47
47
  end
48
48
  alias :normalize_attribute :normalize_attributes
49
+
50
+ def normalize_default_attributes
51
+ AttributeNormalizer.configuration.default_attributes.each do |attribute_name, options|
52
+ normalize_attribute(attribute_name, options) if self.column_names.include?(attribute_name)
53
+ end
54
+ end
55
+
56
+ def inherited(subclass)
57
+ super
58
+ subclass.normalize_default_attributes
59
+ end
49
60
  end
50
61
  end
@@ -20,18 +20,29 @@ module AttributeNormalizer
20
20
 
21
21
 
22
22
  class Configuration
23
- attr_accessor :default_normalizers, :normalizers
23
+ attr_accessor :default_normalizers, :normalizers, :default_attributes
24
24
 
25
25
  def default_normalizers=(normalizers)
26
26
  @default_normalizers = normalizers.is_a?(Array) ? normalizers : [ normalizers ]
27
27
  end
28
28
 
29
+ def default_attributes=(attributes)
30
+ [attributes].flatten.each do |attribute|
31
+ add_default_attribute(attribute, :with => default_normalizers)
32
+ end
33
+ end
34
+
35
+ def add_default_attribute(attribute, options={})
36
+ @default_attributes[attribute.to_s] = { :with => default_normalizers }.merge(options)
37
+ end
38
+
29
39
  def initialize
30
40
  @normalizers = {}
31
41
  @normalizers[ :blank ] = AttributeNormalizer::Normalizers::BlankNormalizer
32
42
  @normalizers[ :phone ] = AttributeNormalizer::Normalizers::PhoneNormalizer
33
43
  @normalizers[ :strip ] = AttributeNormalizer::Normalizers::StripNormalizer
34
44
  @default_normalizers = [ :strip, :blank ]
45
+ @default_attributes = {}
35
46
  end
36
47
 
37
48
 
@@ -0,0 +1,45 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ describe Article do
4
+ it { should normalize_attribute(:title).from(' Social Life at the Edge of Chaos ').to('Social Life at the Edge of Chaos') }
5
+ it { should normalize_attribute(:authors).from(' Octavio Miramontes and Pedro Miramontes ').to('Octavio Miramontes and Pedro Miramontes') }
6
+
7
+ context 'normalization should not interfear with other hooks and aliases on the attribute assignment' do
8
+ before do
9
+ @article = Article.create!(:title => 'Original Title')
10
+ end
11
+
12
+ it 'should still reflect that the attribute has been changed through the call to super' do
13
+ lambda { @article.title = 'New Title' }.should change(@article, :title_changed?).from(false).to(true)
14
+ end
15
+ end
16
+
17
+ context 'when another instance of the same saved record has been changed' do
18
+ before do
19
+ @article = Article.create!(:title => 'Original Title')
20
+ @article2 = Article.find(@article.id)
21
+ @article2.update_attributes(:title => 'New Title')
22
+ end
23
+
24
+ it "should reflect the change when the record is reloaded" do
25
+ lambda { @article.reload }.should change(@article, :title).from('Original Title').to('New Title')
26
+ end
27
+ end
28
+
29
+ context 'normalization should work with multiple attributes at the same time' do
30
+ before do
31
+ @article = Article.new(:title => ' Bad Title ', :authors => ' Bad Authors ')
32
+ end
33
+
34
+ it "should apply normalizations to both attributes" do
35
+ @article.title.should == 'Bad Title'
36
+ @article.authors.should == 'Bad Authors'
37
+ end
38
+ end
39
+
40
+ context 'with the default normalizer changed' do
41
+ @article = Article.new :authors => 'testing the default normalizer'
42
+ @article.authors.should == 'testing the default normalizer'
43
+ end
44
+
45
+ end
@@ -20,4 +20,13 @@ ActiveRecord::Schema.define do
20
20
  t.string :summary
21
21
  t.string :title
22
22
  end
23
+
24
+ create_table :journals, :force => true do |t|
25
+ t.string :name
26
+ end
27
+
28
+ create_table :articles, :force => true do |t|
29
+ t.string :title
30
+ t.string :authors
31
+ end
23
32
  end
@@ -0,0 +1,12 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ describe Journal do
4
+
5
+ context 'Testing the built in normalizers' do
6
+ # default normalization [ :strip, :blank ]
7
+ it { should normalize_attribute(:name) }
8
+ it { should normalize_attribute(:name).from(' Physical Review ').to('Physical Review') }
9
+ it { should normalize_attribute(:name).from(' ').to(nil) }
10
+ end
11
+
12
+ end
@@ -0,0 +1,2 @@
1
+ class Article < ActiveRecord::Base
2
+ end
@@ -0,0 +1,2 @@
1
+ class Journal < ActiveRecord::Base
2
+ end
data/spec/test_helper.rb CHANGED
@@ -29,6 +29,8 @@ AttributeNormalizer.configure do |config|
29
29
  end
30
30
 
31
31
  config.default_normalizers = :strip, :special_normalizer, :blank
32
+ config.default_attributes = :name, :title
33
+ config.add_default_attribute :authors, :with => [:strip, :blank]
32
34
 
33
35
  end
34
36
 
@@ -36,6 +38,8 @@ end
36
38
  require 'connection_and_schema'
37
39
  require 'models/book'
38
40
  require 'models/author'
41
+ require 'models/journal'
42
+ require 'models/article'
39
43
 
40
44
 
41
45
  Spec::Runner.configure do |config|
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: attribute_normalizer
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
5
- prerelease: false
4
+ prerelease: true
6
5
  segments:
7
- - 0
8
- - 3
9
6
  - 1
10
- version: 0.3.1
7
+ - 0
8
+ - 0
9
+ - pre1
10
+ version: 1.0.0.pre1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Michael Deering
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-08-19 00:00:00 -06:00
18
+ date: 2010-11-15 00:00:00 -06:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -40,12 +40,16 @@ files:
40
40
  - lib/attribute_normalizer/normalizers/strip_normalizer.rb
41
41
  - lib/attribute_normalizer/rspec_matcher.rb
42
42
  - rails/init.rb
43
+ - spec/article_spec.rb
43
44
  - spec/attribute_normalizer_spec.rb
44
45
  - spec/author_spec.rb
45
46
  - spec/book_spec.rb
46
47
  - spec/connection_and_schema.rb
48
+ - spec/journal_spec.rb
49
+ - spec/models/article.rb
47
50
  - spec/models/author.rb
48
51
  - spec/models/book.rb
52
+ - spec/models/journal.rb
49
53
  - spec/test_helper.rb
50
54
  has_rdoc: true
51
55
  homepage: http://github.com/mdeering/attribute_normalizer
@@ -63,19 +67,19 @@ required_ruby_version: !ruby/object:Gem::Requirement
63
67
  requirements:
64
68
  - - ">="
65
69
  - !ruby/object:Gem::Version
66
- hash: 3
67
70
  segments:
68
71
  - 0
69
72
  version: "0"
70
73
  required_rubygems_version: !ruby/object:Gem::Requirement
71
74
  none: false
72
75
  requirements:
73
- - - ">="
76
+ - - ">"
74
77
  - !ruby/object:Gem::Version
75
- hash: 3
76
78
  segments:
77
- - 0
78
- version: "0"
79
+ - 1
80
+ - 3
81
+ - 1
82
+ version: 1.3.1
79
83
  requirements: []
80
84
 
81
85
  rubyforge_project:
@@ -84,10 +88,14 @@ signing_key:
84
88
  specification_version: 3
85
89
  summary: Attribute normalizer that excepts code blocks.
86
90
  test_files:
91
+ - spec/article_spec.rb
87
92
  - spec/attribute_normalizer_spec.rb
88
93
  - spec/author_spec.rb
89
94
  - spec/book_spec.rb
90
95
  - spec/connection_and_schema.rb
96
+ - spec/journal_spec.rb
97
+ - spec/models/article.rb
91
98
  - spec/models/author.rb
92
99
  - spec/models/book.rb
100
+ - spec/models/journal.rb
93
101
  - spec/test_helper.rb