attribute_normalizer 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.textile CHANGED
@@ -4,38 +4,61 @@ p. I like to keep my Active Record models as strict as possible but I also like
4
4
 
5
5
  h2. Install as a Ruby gem
6
6
 
7
- TODO: gem-er-a-size this!
8
-
7
+ p. The "attribute_normalizer gem":http://gemcutter.org/gems/attribute_normalizer is hosted over at "Gemcutter":http://gemcutter.org
8
+
9
+ h3. Setup Gemcutter as a gem source if you have not already.
10
+
11
+ p. It’s fairly simple to set up Gemcutter. Before we start, however, it’s worth making sure that we’ve upgraded to the latest version of RubyGems, which can be done by running
12
+
13
+ <pre><code>sudo gem update --system</code></pre>
14
+
15
+ p. From the command line. Once RubyGems has updated itself we can then install the Gemcutter gem with
16
+
17
+ <pre><code>sudo gem install gemcutter</code></pre>
18
+
19
+ p. After Gemcutter has installed we’ll need to update our gem sources to include gemcutter.org. To do this we run *gem tumble*.
20
+
21
+ <pre><code>$ gem tumble
22
+ Thanks for using Gemcutter!
23
+ Your gem sources are now:
24
+ - http://gemcutter.org
25
+ - http://gems.rubyforge.org/
26
+ - http://gems.github.com
27
+ </code></pre>
28
+
29
+ h3. Install the Attribute Normalizer gem
30
+
31
+ <pre><code>sudo gem install attribute_normalizer</code></pre>
32
+
9
33
  h2. Install as a Ruby on Rails Plugin
10
34
 
11
35
  The traditional way.
12
36
 
13
37
  ./script/plugin install git://github.com/mdeering/attribute_normalizer.git
14
-
38
+
15
39
  or the old-school but still c00l way!
16
40
 
17
41
  piston import git://github.com/mdeering/attribute_normalizer.git vendor/plugins/attribute_normalizer
18
-
42
+
19
43
  or for all you hip gitsters.
20
44
 
21
45
  git submodule add git://github.com/mdeering/attribute_normalizer.git vendor/plugins/attribute_normalizer
22
46
  git submodule init
23
-
47
+
24
48
  h2. Usage
25
49
 
26
50
  This is eager loaded into Active Record. It is usable inside of other ruby classes outside of ActiveRecord by just including the module AttributeNormalizer.
27
51
 
28
52
  <pre><code>
29
53
  class Klass < ActiveRecord::Base
30
-
54
+
31
55
  # Can take an array of attributes if you want
32
56
  normalize_attributes :first_name, :last_name
33
-
57
+
34
58
  normalize_attributes :home_phone_number, :office_phone_number_ do |value|
35
- return nil unless value.is_a?(String)
36
- value.gsub(/\W/, '').gsub(/^1/, '')
59
+ value.is_a?(String) ? value.gsub(/\W/, '').gsub(/^1/, '') : nil
37
60
  end
38
-
61
+
39
62
  end
40
63
 
41
64
  object = Klass.new
@@ -43,7 +66,7 @@ object = Klass.new
43
66
  object.first_name = ''
44
67
  object.first_name # => nil
45
68
 
46
- # Whitespace is cleaned up
69
+ # Whitespace is cleaned up
47
70
  object.last_name = "\tDeering\n"
48
71
  object.last_name # => 'Deering'
49
72
 
@@ -58,4 +81,4 @@ Original module code and concept was taken from "Dan Kubb":http://github.com/dku
58
81
 
59
82
  h2. Copyright
60
83
 
61
- Copyright (c) 2009 "Michael Deering(Edmonton Ruby on Rails)":http://mdeering.com See MIT-LICENSE for details.
84
+ Copyright (c) 2009 "Michael Deering(Edmonton Ruby on Rails)":http://mdeering.com See MIT-LICENSE for details.
data/Rakefile CHANGED
@@ -13,11 +13,23 @@ begin
13
13
  Jeweler::Tasks.new do |s|
14
14
  s.author = AUTHOR
15
15
  s.email = EMAIL
16
- s.files = %w(MIT-LICENSE README.textile Rakefile) + Dir.glob("{rails,lib,spec}/**/*")
16
+ s.files = %w(install.rb install.txt MIT-LICENSE README.textile Rakefile) + Dir.glob("{rails,lib,spec}/**/*")
17
17
  s.homepage = HOMEPAGE
18
18
  s.name = GEM
19
19
  s.require_path = 'lib'
20
20
  s.summary = SUMMARY
21
+ s.post_install_message = %q[
22
+ =============================
23
+ Attribute Normalizer News:
24
+
25
+ I am looking for feedback on the roadmap for this gem. Please visit
26
+ http://github.com/mdeering/attribute_normalizer/blob/master/ROADMAP.textile
27
+ and send your comments, suggestions, and pull requests.
28
+
29
+ Cheers,
30
+ Michael Deering http://mdeering.com
31
+ =============================
32
+ ]
21
33
  end
22
34
  Jeweler::GemcutterTasks.new
23
35
  rescue LoadError
data/install.rb ADDED
@@ -0,0 +1 @@
1
+ puts IO.read(File.join(File.dirname(__FILE__), 'install.txt'))
data/install.txt ADDED
@@ -0,0 +1,10 @@
1
+ -----------------------------------------------------------------------
2
+ Attribute Normalizer News:
3
+
4
+ I am looking for feedback on the roadmap for this gem. Please visit
5
+ http://github.com/mdeering/attribute_normalizer/blob/master/ROADMAP.textile
6
+ and send your comments, suggestions, and pull requests.
7
+
8
+ Cheers,
9
+ Michael Deering http://mdeering.com
10
+ -----------------------------------------------------------------------
@@ -22,18 +22,24 @@ module AttributeNormalizer
22
22
 
23
23
  src = <<-end_src
24
24
  def #{attribute}
25
- @#{attribute} ||= self.class.send(:normalize_#{attribute}, self[:#{attribute}]) unless self[:#{attribute}].nil?
25
+ value = super
26
+ value.nil? ? value : self.class.send(:normalize_#{attribute}, value)
26
27
  end
27
28
 
28
29
  def #{attribute}=(#{attribute})
29
- @#{attribute} = self[:#{attribute}] = self.class.send(:normalize_#{attribute}, #{attribute})
30
+ super(self.class.send(:normalize_#{attribute}, #{attribute}))
30
31
  end
31
32
  end_src
32
33
 
33
34
  module_eval src, __FILE__, __LINE__
34
-
35
+
35
36
  end
36
-
37
+
37
38
  end
39
+
40
+ alias :normalize_attribute :normalize_attributes
41
+
38
42
  end
39
43
  end
44
+
45
+ ActiveRecord::Base.send(:include, AttributeNormalizer)
data/rails/init.rb CHANGED
@@ -1,2 +1 @@
1
1
  require File.join(File.dirname(__FILE__), *%w[.. lib attribute_normalizer])
2
- ActiveRecord::Base.send(:include, AttributeNormalizer)
@@ -2,44 +2,44 @@ require File.dirname(__FILE__) + '/test_helper'
2
2
  require 'attribute_normalizer'
3
3
 
4
4
  describe AttributeNormalizer do
5
-
5
+
6
6
  it 'should add the class method Class#normalize_attributes when included' do
7
-
7
+
8
8
  klass = Class.new do
9
9
  include AttributeNormalizer
10
10
  end
11
-
11
+
12
12
  klass.respond_to?(:normalize_attributes).should be_true
13
13
  end
14
-
14
+
15
15
  end
16
16
 
17
17
  describe '#normalize_attributes without a block' do
18
-
19
- before do
18
+
19
+ before do
20
20
 
21
21
  class Klass
22
22
  attr_accessor :attribute
23
23
  include AttributeNormalizer
24
24
  normalize_attributes :attribute
25
25
  end
26
-
26
+
27
27
  end
28
28
 
29
29
  {
30
- ' spaces in front and back ' => 'spaces in front and back',
30
+ ' spaces in front and back ' => 'spaces in front and back',
31
31
  "\twe hate tabs!\t" => 'we hate tabs!'
32
32
  }.each do |key, value|
33
33
  it "should normalize '#{key}' to '#{value}'" do
34
34
  Klass.send(:normalize_attribute, key).should == value
35
35
  end
36
36
  end
37
-
37
+
38
38
  end
39
39
 
40
40
  describe '#normalize_attributes with a block' do
41
-
42
- before do
41
+
42
+ before do
43
43
 
44
44
  class Klass
45
45
  attr_accessor :attribute
@@ -51,13 +51,13 @@ describe '#normalize_attributes with a block' do
51
51
  value
52
52
  end
53
53
  end
54
-
54
+
55
55
  @object = Klass.new
56
-
56
+
57
57
  end
58
58
 
59
59
  {
60
- "\tMichael Deering" => 'MICHAEL DEERING',
60
+ "\tMichael Deering" => 'MICHAEL DEERING',
61
61
  2 => 4,
62
62
  2.0 => 1.0
63
63
  }.each do |key, value|
@@ -65,30 +65,59 @@ describe '#normalize_attributes with a block' do
65
65
  Klass.send(:normalize_attribute, key).should == value
66
66
  end
67
67
  end
68
-
68
+
69
69
  end
70
70
 
71
71
  describe 'with an instance' do
72
-
72
+
73
73
  before do
74
- class Klass
75
- attr_accessor :attribute
76
- include AttributeNormalizer
77
- normalize_attributes :attribute
74
+ User.class_eval do
75
+ normalize_attributes :name
78
76
  end
79
-
80
- @object = Klass.new
77
+ @user = User.new
78
+ end
79
+
80
+ {
81
+ ' spaces in front and back ' => 'spaces in front and back',
82
+ "\twe hate tabs!\t" => 'we hate tabs!'
83
+ }.each do |key, value|
84
+ it "should normalize '#{key}' to '#{value}'" do
85
+ @user.name = key
86
+ @user.name.should == value
87
+ end
88
+ end
89
+
90
+ context 'when another instance of the same saved record has been changed' do
91
+
92
+ before do
93
+ @user = User.create!(:name => 'Jimi Hendrix')
94
+ @user2 = User.find(@user.id)
95
+ @user2.update_attributes(:name => 'Thom Yorke')
96
+ end
97
+
98
+ it "should reflect the change when the record is reloaded" do
99
+ lambda { @user.reload }.should change(@user, :name).from('Jimi Hendrix').to('Thom Yorke')
100
+ end
101
+ end
102
+
103
+ end
104
+
105
+ describe 'normalize_attribute is aliased to normalize_attributes' do
106
+ before do
107
+ User.class_eval do
108
+ normalize_attribute :name
109
+ end
110
+ @user = User.new
81
111
  end
82
-
112
+
83
113
  {
84
- ' spaces in front and back ' => 'spaces in front and back',
114
+ ' spaces in front and back ' => 'spaces in front and back',
85
115
  "\twe hate tabs!\t" => 'we hate tabs!'
86
116
  }.each do |key, value|
87
117
  it "should normalize '#{key}' to '#{value}'" do
88
- @object.send('attribute=', key)
89
- @object.send(:attribute).should == value
118
+ @user.name = key
119
+ @user.name.should == value
90
120
  end
91
121
  end
92
-
93
-
122
+
94
123
  end
data/spec/test_helper.rb CHANGED
@@ -1,9 +1,19 @@
1
1
  require 'rubygems'
2
2
  require 'spec'
3
- require 'activesupport'
4
- require 'activerecord'
3
+ require 'active_support'
4
+ require 'active_record'
5
+
6
+ ActiveRecord::Base.establish_connection({ :database => ":memory:", :adapter => 'sqlite3', :timeout => 500 })
7
+
8
+ ActiveRecord::Schema.define do
9
+ create_table :users, :force => true do |t|
10
+ t.string :name
11
+ end
12
+ end
13
+
14
+ class User < ActiveRecord::Base; end
5
15
 
6
16
  Spec::Runner.configure do |config|
7
17
  end
8
18
 
9
- $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib')
19
+ $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib')
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: attribute_normalizer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Deering
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-01-01 00:00:00 -07:00
12
+ date: 2010-01-02 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -25,6 +25,8 @@ files:
25
25
  - MIT-LICENSE
26
26
  - README.textile
27
27
  - Rakefile
28
+ - install.rb
29
+ - install.txt
28
30
  - lib/attribute_normalizer.rb
29
31
  - rails/init.rb
30
32
  - spec/attribute_normalizer_spec.rb
@@ -33,7 +35,7 @@ has_rdoc: true
33
35
  homepage: http://github.com/mdeering/attribute_normalizer
34
36
  licenses: []
35
37
 
36
- post_install_message:
38
+ post_install_message: "\n =============================\n Attribute Normalizer News:\n\n I am looking for feedback on the roadmap for this gem. Please visit\n http://github.com/mdeering/attribute_normalizer/blob/master/ROADMAP.textile\n and send your comments, suggestions, and pull requests.\n\n Cheers,\n Michael Deering http://mdeering.com\n ============================= \n "
37
39
  rdoc_options:
38
40
  - --charset=UTF-8
39
41
  require_paths: