attribute_normalizer 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Michael Deering
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.textile ADDED
@@ -0,0 +1,61 @@
1
+ h1. Attribute Normalizer
2
+
3
+ p. I like to keep my Active Record models as strict as possible but I also like the further layer of protection/restriction setting database columns to not allow NULL adds. Normalizing to nil helps enforce this better by not letting '' slip through the cracks and I can still prevent those who insist on direct DB access from entering in shitty data as much as possible.
4
+
5
+ h2. Install as a Ruby gem
6
+
7
+ TODO: gem-er-a-size this!
8
+
9
+ h2. Install as a Ruby on Rails Plugin
10
+
11
+ The traditional way.
12
+
13
+ ./script/plugin install git://github.com/mdeering/attribute_normalizer.git
14
+
15
+ or the old-school but still c00l way!
16
+
17
+ piston import git://github.com/mdeering/attribute_normalizer.git vendor/plugins/attribute_normalizer
18
+
19
+ or for all you hip gitsters.
20
+
21
+ git submodule add git://github.com/mdeering/attribute_normalizer.git vendor/plugins/attribute_normalizer
22
+ git submodule init
23
+
24
+ h2. Usage
25
+
26
+ 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
+
28
+ <pre><code>
29
+ class Klass < ActiveRecord::Base
30
+
31
+ # Can take an array of attributes if you want
32
+ normalize_attributes :first_name, :last_name
33
+
34
+ normalize_attributes :home_phone_number, :office_phone_number_ do |value|
35
+ return nil unless value.is_a?(String)
36
+ value.gsub(/\W/, '').gsub(/^1/, '')
37
+ end
38
+
39
+ end
40
+
41
+ object = Klass.new
42
+ # Blank normalizes to nil
43
+ object.first_name = ''
44
+ object.first_name # => nil
45
+
46
+ # Whitespace is cleaned up
47
+ object.last_name = "\tDeering\n"
48
+ object.last_name # => 'Deering'
49
+
50
+ # Your given block will be executed to normalize
51
+ object.home_phone_number = '+1 (555) 123.4567'
52
+ object.home_phone_number # => '5551234567'
53
+ </code></pre>
54
+
55
+ h2. Credits
56
+
57
+ Original module code and concept was taken from "Dan Kubb":http://github.com/dkubb during a project we worked on together. I found that I was consistently using this across all my projects so I wanted to plugin-er-size and gem this up for easy reuse.
58
+
59
+ h2. Copyright
60
+
61
+ Copyright (c) 2009 "Michael Deering(Edmonton Ruby on Rails)":http://mdeering.com See MIT-LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,50 @@
1
+ require 'rake'
2
+ require 'rake/rdoctask'
3
+ require 'spec/rake/spectask'
4
+
5
+ begin
6
+ AUTHOR = "Michael Deering"
7
+ EMAIL = "mdeering@mdeering.com"
8
+ GEM = "attribute_normalizer"
9
+ HOMEPAGE = "http://github.com/mdeering/attribute_normalizer"
10
+ SUMMARY = "Active Record attribute normalizer that excepts code blocks."
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |s|
14
+ s.author = AUTHOR
15
+ s.email = EMAIL
16
+ s.files = %w(MIT-LICENSE README.textile Rakefile) + Dir.glob("{rails,lib,spec}/**/*")
17
+ s.homepage = HOMEPAGE
18
+ s.name = GEM
19
+ s.require_path = 'lib'
20
+ s.summary = SUMMARY
21
+ end
22
+ Jeweler::GemcutterTasks.new
23
+ rescue LoadError
24
+ puts "Jeweler, or one of its dependencies, is not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
25
+ end
26
+
27
+ desc 'Default: spec tests.'
28
+ task :default => :spec
29
+
30
+ desc 'Test the attribute_normalizer plugin.'
31
+ Spec::Rake::SpecTask.new('spec') do |t|
32
+ t.spec_files = FileList['spec/**/*_spec.rb']
33
+ t.spec_opts = ["-c"]
34
+ end
35
+
36
+ desc "Run all examples with RCov"
37
+ Spec::Rake::SpecTask.new('examples_with_rcov') do |t|
38
+ t.spec_files = FileList['spec/**/*_spec.rb']
39
+ t.rcov = true
40
+ t.rcov_opts = ['--exclude', '/opt,spec,Library']
41
+ end
42
+
43
+ desc 'Generate documentation for the attribute_normalizer plugin.'
44
+ Rake::RDocTask.new(:rdoc) do |rdoc|
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = 'AttributeNormalizer'
47
+ rdoc.options << '--line-numbers' << '--inline-source'
48
+ rdoc.rdoc_files.include('README.textile')
49
+ rdoc.rdoc_files.include('lib/**/*.rb')
50
+ end
@@ -0,0 +1,39 @@
1
+ module AttributeNormalizer
2
+
3
+ def self.included(base)
4
+ base.extend ClassMethods
5
+ end
6
+
7
+ module ClassMethods
8
+
9
+ def normalize_attributes(*attributes, &block)
10
+
11
+ attributes.each do |attribute|
12
+
13
+ klass = class << self; self end
14
+
15
+ klass.send :define_method, "normalize_#{attribute}" do |value|
16
+ value = value.strip if value.is_a?(String)
17
+ normalized = block_given? && !value.blank? ? yield(value) : value
18
+ normalized.nil? || (normalized.is_a?(String) && normalized == '') ? nil : normalized
19
+ end
20
+
21
+ klass.send :private, "normalize_#{attribute}"
22
+
23
+ src = <<-end_src
24
+ def #{attribute}
25
+ @#{attribute} ||= self.class.send(:normalize_#{attribute}, self[:#{attribute}]) unless self[:#{attribute}].nil?
26
+ end
27
+
28
+ def #{attribute}=(#{attribute})
29
+ @#{attribute} = self[:#{attribute}] = self.class.send(:normalize_#{attribute}, #{attribute})
30
+ end
31
+ end_src
32
+
33
+ module_eval src, __FILE__, __LINE__
34
+
35
+ end
36
+
37
+ end
38
+ end
39
+ end
data/rails/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ require File.join(File.dirname(__FILE__), *%w[.. lib attribute_normalizer])
2
+ ActiveRecord::Base.send(:include, AttributeNormalizer)
@@ -0,0 +1,94 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+ require 'attribute_normalizer'
3
+
4
+ describe AttributeNormalizer do
5
+
6
+ it 'should add the class method Class#normalize_attributes when included' do
7
+
8
+ klass = Class.new do
9
+ include AttributeNormalizer
10
+ end
11
+
12
+ klass.respond_to?(:normalize_attributes).should be_true
13
+ end
14
+
15
+ end
16
+
17
+ describe '#normalize_attributes without a block' do
18
+
19
+ before do
20
+
21
+ class Klass
22
+ attr_accessor :attribute
23
+ include AttributeNormalizer
24
+ normalize_attributes :attribute
25
+ end
26
+
27
+ end
28
+
29
+ {
30
+ ' spaces in front and back ' => 'spaces in front and back',
31
+ "\twe hate tabs!\t" => 'we hate tabs!'
32
+ }.each do |key, value|
33
+ it "should normalize '#{key}' to '#{value}'" do
34
+ Klass.send(:normalize_attribute, key).should == value
35
+ end
36
+ end
37
+
38
+ end
39
+
40
+ describe '#normalize_attributes with a block' do
41
+
42
+ before do
43
+
44
+ class Klass
45
+ attr_accessor :attribute
46
+ include AttributeNormalizer
47
+ normalize_attributes :attribute do |value|
48
+ value = value.strip.upcase if value.is_a?(String)
49
+ value = value * 2 if value.is_a?(Fixnum)
50
+ value = value * 0.5 if value.is_a?(Float)
51
+ value
52
+ end
53
+ end
54
+
55
+ @object = Klass.new
56
+
57
+ end
58
+
59
+ {
60
+ "\tMichael Deering" => 'MICHAEL DEERING',
61
+ 2 => 4,
62
+ 2.0 => 1.0
63
+ }.each do |key, value|
64
+ it "should normalize '#{key}' to '#{value}'" do
65
+ Klass.send(:normalize_attribute, key).should == value
66
+ end
67
+ end
68
+
69
+ end
70
+
71
+ describe 'with an instance' do
72
+
73
+ before do
74
+ class Klass
75
+ attr_accessor :attribute
76
+ include AttributeNormalizer
77
+ normalize_attributes :attribute
78
+ end
79
+
80
+ @object = Klass.new
81
+ end
82
+
83
+ {
84
+ ' spaces in front and back ' => 'spaces in front and back',
85
+ "\twe hate tabs!\t" => 'we hate tabs!'
86
+ }.each do |key, value|
87
+ it "should normalize '#{key}' to '#{value}'" do
88
+ @object.send('attribute=', key)
89
+ @object.send(:attribute).should == value
90
+ end
91
+ end
92
+
93
+
94
+ end
@@ -0,0 +1,9 @@
1
+ require 'rubygems'
2
+ require 'spec'
3
+ require 'activesupport'
4
+ require 'activerecord'
5
+
6
+ Spec::Runner.configure do |config|
7
+ end
8
+
9
+ $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib')
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: attribute_normalizer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Michael Deering
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-01-01 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: mdeering@mdeering.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.textile
24
+ files:
25
+ - MIT-LICENSE
26
+ - README.textile
27
+ - Rakefile
28
+ - lib/attribute_normalizer.rb
29
+ - rails/init.rb
30
+ - spec/attribute_normalizer_spec.rb
31
+ - spec/test_helper.rb
32
+ has_rdoc: true
33
+ homepage: http://github.com/mdeering/attribute_normalizer
34
+ licenses: []
35
+
36
+ post_install_message:
37
+ rdoc_options:
38
+ - --charset=UTF-8
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ version:
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: "0"
52
+ version:
53
+ requirements: []
54
+
55
+ rubyforge_project:
56
+ rubygems_version: 1.3.5
57
+ signing_key:
58
+ specification_version: 3
59
+ summary: Active Record attribute normalizer that excepts code blocks.
60
+ test_files:
61
+ - spec/attribute_normalizer_spec.rb
62
+ - spec/test_helper.rb