trimify 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +15 -0
- data/Gemfile.lock +49 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +28 -0
- data/Rakefile +50 -0
- data/VERSION +1 -0
- data/lib/trimify.rb +50 -0
- data/spec/fixtures/category.rb +4 -0
- data/spec/fixtures/no_table.rb +3 -0
- data/spec/fixtures/post.rb +3 -0
- data/spec/fixtures/schema.rb +19 -0
- data/spec/fixtures/specialized_user.rb +2 -0
- data/spec/fixtures/user.rb +4 -0
- data/spec/spec_helper.rb +15 -0
- data/spec/trimify_spec.rb +58 -0
- metadata +172 -0
data/Gemfile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
|
6
|
+
# Add dependencies to develop your gem here.
|
7
|
+
# Include everything needed to run rake, tests, features, etc.
|
8
|
+
group :development do
|
9
|
+
gem "rspec", "~> 2.6.0"
|
10
|
+
gem "bundler", "~> 1.0.0"
|
11
|
+
gem "jeweler", "~> 1.6.2"
|
12
|
+
gem "rcov", ">= 0"
|
13
|
+
gem "sqlite3", "~>1.3.0"
|
14
|
+
gem "rails", "~>2.3.0"
|
15
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
actionmailer (2.3.9)
|
5
|
+
actionpack (= 2.3.9)
|
6
|
+
actionpack (2.3.9)
|
7
|
+
activesupport (= 2.3.9)
|
8
|
+
rack (~> 1.1.0)
|
9
|
+
activerecord (2.3.9)
|
10
|
+
activesupport (= 2.3.9)
|
11
|
+
activeresource (2.3.9)
|
12
|
+
activesupport (= 2.3.9)
|
13
|
+
activesupport (2.3.9)
|
14
|
+
diff-lcs (1.1.2)
|
15
|
+
git (1.2.5)
|
16
|
+
jeweler (1.6.2)
|
17
|
+
bundler (~> 1.0)
|
18
|
+
git (>= 1.2.5)
|
19
|
+
rake
|
20
|
+
rack (1.1.2)
|
21
|
+
rails (2.3.9)
|
22
|
+
actionmailer (= 2.3.9)
|
23
|
+
actionpack (= 2.3.9)
|
24
|
+
activerecord (= 2.3.9)
|
25
|
+
activeresource (= 2.3.9)
|
26
|
+
activesupport (= 2.3.9)
|
27
|
+
rake (>= 0.8.3)
|
28
|
+
rake (0.8.7)
|
29
|
+
rcov (0.9.9)
|
30
|
+
rspec (2.6.0)
|
31
|
+
rspec-core (~> 2.6.0)
|
32
|
+
rspec-expectations (~> 2.6.0)
|
33
|
+
rspec-mocks (~> 2.6.0)
|
34
|
+
rspec-core (2.6.4)
|
35
|
+
rspec-expectations (2.6.0)
|
36
|
+
diff-lcs (~> 1.1.2)
|
37
|
+
rspec-mocks (2.6.0)
|
38
|
+
sqlite3 (1.3.3)
|
39
|
+
|
40
|
+
PLATFORMS
|
41
|
+
ruby
|
42
|
+
|
43
|
+
DEPENDENCIES
|
44
|
+
bundler (~> 1.0.0)
|
45
|
+
jeweler (~> 1.6.2)
|
46
|
+
rails (~> 2.3.0)
|
47
|
+
rcov
|
48
|
+
rspec (~> 2.6.0)
|
49
|
+
sqlite3 (~> 1.3.0)
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Behrang Saeedzadeh
|
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.rdoc
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
=Trimify
|
2
|
+
|
3
|
+
Trimify is an ActiveRecord Plugin that allows you to easily trim leading and trailing white spaces from string attributes and optionally turn blank strings to nil.
|
4
|
+
Only attributes you specify will be checked and converted. Using trimify on non-string attributes has no effect.
|
5
|
+
|
6
|
+
==Installation
|
7
|
+
|
8
|
+
gem install trimify
|
9
|
+
|
10
|
+
==Example
|
11
|
+
|
12
|
+
class User < ActiveRecord::Base
|
13
|
+
trimify :first_name, :middle_name
|
14
|
+
trimify :last_name, :nilify => false # default value of :nilify is true
|
15
|
+
end
|
16
|
+
|
17
|
+
user = User.create(:first_name => " Shompet ", :middle_name = "", :last_name => "")
|
18
|
+
user.first_name # => "Shompet"
|
19
|
+
user.middle_name # => nil
|
20
|
+
user.last_name # => ""
|
21
|
+
|
22
|
+
==Acknowledgments
|
23
|
+
|
24
|
+
Trimify is inspired by the Nilify plugin written by Tobias Schmidt.
|
25
|
+
|
26
|
+
== Copyright
|
27
|
+
|
28
|
+
Copyright (c) 2011 Behrang Saeedzadeh, released under the MIT license.
|
data/Rakefile
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "trimify"
|
18
|
+
gem.homepage = "http://github.com/behrangsa/trimify"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{A gem for trimming strings in active record models.}
|
21
|
+
gem.description = %Q{A gem for trimming strings and optionally turning blank strings to nil}
|
22
|
+
gem.email = "behrangsa@gmail.com"
|
23
|
+
gem.authors = ["Behrang Saeedzadeh"]
|
24
|
+
gem.version = File.read('VERSION').chomp
|
25
|
+
# dependencies defined in Gemfile
|
26
|
+
end
|
27
|
+
Jeweler::RubygemsDotOrgTasks.new
|
28
|
+
|
29
|
+
require 'rspec/core'
|
30
|
+
require 'rspec/core/rake_task'
|
31
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
32
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
33
|
+
end
|
34
|
+
|
35
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
36
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
37
|
+
spec.rcov = true
|
38
|
+
end
|
39
|
+
|
40
|
+
task :default => :spec
|
41
|
+
|
42
|
+
require 'rake/rdoctask'
|
43
|
+
Rake::RDocTask.new do |rdoc|
|
44
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
45
|
+
|
46
|
+
rdoc.rdoc_dir = 'rdoc'
|
47
|
+
rdoc.title = "trimify #{version}"
|
48
|
+
rdoc.rdoc_files.include('README*')
|
49
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
50
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/lib/trimify.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
module Behi
|
2
|
+
module Trimify
|
3
|
+
|
4
|
+
def self.included(base)
|
5
|
+
base.extend ClassMethods
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
def trimify(*args)
|
10
|
+
return if args.empty?
|
11
|
+
return unless table_exists?
|
12
|
+
|
13
|
+
options = { :nilify => true }
|
14
|
+
options.merge!(args.pop) if args.last.kind_of? Hash
|
15
|
+
|
16
|
+
unless self.included_modules.include?(Trimify::InstanceMethods)
|
17
|
+
include Trimify::InstanceMethods
|
18
|
+
write_inheritable_attribute :trimify_attributes, {}
|
19
|
+
class_inheritable_reader :trimify_attributes
|
20
|
+
end
|
21
|
+
|
22
|
+
attributes = args.map(&:to_sym)
|
23
|
+
col_options = {}
|
24
|
+
columns = content_columns.reject { |c| c.type != :string }.map { |c| c.name.to_sym }.select { |c| args.include?(c) }
|
25
|
+
columns.each { |col| col_options[col] = options }
|
26
|
+
write_inheritable_hash :trimify_attributes, col_options
|
27
|
+
|
28
|
+
class_eval "before_validation :trimify"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
module InstanceMethods
|
33
|
+
def trimify
|
34
|
+
self.class.trimify_attributes.each do |attribute, options|
|
35
|
+
value = read_attribute(attribute)
|
36
|
+
if value.is_a?(String)
|
37
|
+
value.strip!
|
38
|
+
value = nil if (value.blank? && options[:nilify] == true)
|
39
|
+
write_attribute(attribute, value)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
class ActiveRecord::Base
|
49
|
+
include Behi::Trimify
|
50
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
ActiveRecord::Schema.define do
|
2
|
+
|
3
|
+
create_table :users, :force => true do |t|
|
4
|
+
t.string :first_name, :not_null, :type
|
5
|
+
t.string :middle_name, :not_null, :type
|
6
|
+
t.string :last_name, :not_null, :type
|
7
|
+
t.integer :salary
|
8
|
+
t.datetime :birthday
|
9
|
+
end
|
10
|
+
|
11
|
+
create_table :posts, :force => true do |t|
|
12
|
+
t.string :name
|
13
|
+
end
|
14
|
+
|
15
|
+
create_table :categories, :force => true do |t|
|
16
|
+
t.string :title, :description
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require "rspec"
|
3
|
+
require "sqlite3"
|
4
|
+
require "active_record"
|
5
|
+
|
6
|
+
# load nilify
|
7
|
+
require File.dirname(__FILE__) + '/../lib/trimify'
|
8
|
+
|
9
|
+
# establish database connection, define schema and load model
|
10
|
+
ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => ':memory:'
|
11
|
+
ActiveRecord::Migration.verbose = false
|
12
|
+
|
13
|
+
%w(schema category post user specialized_user).each do |file|
|
14
|
+
require File.dirname(__FILE__) + "/fixtures/#{file}"
|
15
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
|
3
|
+
describe Behi::Trimify do
|
4
|
+
|
5
|
+
it "should add a class method to activerecord" do
|
6
|
+
User.methods.should include("trimify")
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should allow multiple trimify calls" do
|
10
|
+
Category.trimify_attributes.should == { :title => { :nilify => true }, :description => { :nilify => true } }
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should bequest trimify attributes" do
|
14
|
+
SpecializedUser.trimify_attributes.should == {
|
15
|
+
:first_name => { :nilify => true },
|
16
|
+
:middle_name => { :nilify => true },
|
17
|
+
:last_name => { :nilify => false }
|
18
|
+
}
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should not raise an exception for a non existing table" do
|
22
|
+
expect { require "fixtures/no_table" }.to_not raise_error(ActiveRecord::StatementInvalid)
|
23
|
+
end
|
24
|
+
|
25
|
+
describe "when trimifying possible attributes" do
|
26
|
+
before(:each) do
|
27
|
+
@user = User.new(:first_name => " Shompet ", :middle_name => " ", :last_name => "", :salary => 0, :birthday => Time.at(0))
|
28
|
+
@user.valid?
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should trim leading and trailing white spaces" do
|
32
|
+
@user.first_name.should == "Shompet"
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should nilify strings that are trimmed down to a blank string" do
|
36
|
+
@user.middle_name.should be_nil
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should not nilify strings that are trimmed down to a blank string if :nilify is set to false" do
|
40
|
+
@user.last_name.should == ""
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should not nilify not specified values" do
|
44
|
+
user = User.new(:not_null => "")
|
45
|
+
user.valid?
|
46
|
+
user.not_null.should == ""
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
describe "when calling nilify without attributes" do
|
51
|
+
it "should do nothing" do
|
52
|
+
post = Post.new(:name => "")
|
53
|
+
post.valid?
|
54
|
+
post.name.should == ""
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
metadata
ADDED
@@ -0,0 +1,172 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: trimify
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Behrang Saeedzadeh
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-06-08 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rspec
|
22
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
23
|
+
none: false
|
24
|
+
requirements:
|
25
|
+
- - ~>
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
hash: 23
|
28
|
+
segments:
|
29
|
+
- 2
|
30
|
+
- 6
|
31
|
+
- 0
|
32
|
+
version: 2.6.0
|
33
|
+
prerelease: false
|
34
|
+
type: :development
|
35
|
+
requirement: *id001
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: bundler
|
38
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ~>
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
hash: 23
|
44
|
+
segments:
|
45
|
+
- 1
|
46
|
+
- 0
|
47
|
+
- 0
|
48
|
+
version: 1.0.0
|
49
|
+
prerelease: false
|
50
|
+
type: :development
|
51
|
+
requirement: *id002
|
52
|
+
- !ruby/object:Gem::Dependency
|
53
|
+
name: jeweler
|
54
|
+
version_requirements: &id003 !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ~>
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
hash: 11
|
60
|
+
segments:
|
61
|
+
- 1
|
62
|
+
- 6
|
63
|
+
- 2
|
64
|
+
version: 1.6.2
|
65
|
+
prerelease: false
|
66
|
+
type: :development
|
67
|
+
requirement: *id003
|
68
|
+
- !ruby/object:Gem::Dependency
|
69
|
+
name: rcov
|
70
|
+
version_requirements: &id004 !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
hash: 3
|
76
|
+
segments:
|
77
|
+
- 0
|
78
|
+
version: "0"
|
79
|
+
prerelease: false
|
80
|
+
type: :development
|
81
|
+
requirement: *id004
|
82
|
+
- !ruby/object:Gem::Dependency
|
83
|
+
name: sqlite3
|
84
|
+
version_requirements: &id005 !ruby/object:Gem::Requirement
|
85
|
+
none: false
|
86
|
+
requirements:
|
87
|
+
- - ~>
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
hash: 27
|
90
|
+
segments:
|
91
|
+
- 1
|
92
|
+
- 3
|
93
|
+
- 0
|
94
|
+
version: 1.3.0
|
95
|
+
prerelease: false
|
96
|
+
type: :development
|
97
|
+
requirement: *id005
|
98
|
+
- !ruby/object:Gem::Dependency
|
99
|
+
name: rails
|
100
|
+
version_requirements: &id006 !ruby/object:Gem::Requirement
|
101
|
+
none: false
|
102
|
+
requirements:
|
103
|
+
- - ~>
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
hash: 3
|
106
|
+
segments:
|
107
|
+
- 2
|
108
|
+
- 3
|
109
|
+
- 0
|
110
|
+
version: 2.3.0
|
111
|
+
prerelease: false
|
112
|
+
type: :development
|
113
|
+
requirement: *id006
|
114
|
+
description: A gem for trimming strings and optionally turning blank strings to nil
|
115
|
+
email: behrangsa@gmail.com
|
116
|
+
executables: []
|
117
|
+
|
118
|
+
extensions: []
|
119
|
+
|
120
|
+
extra_rdoc_files:
|
121
|
+
- README.rdoc
|
122
|
+
files:
|
123
|
+
- Gemfile
|
124
|
+
- Gemfile.lock
|
125
|
+
- MIT-LICENSE
|
126
|
+
- README.rdoc
|
127
|
+
- Rakefile
|
128
|
+
- VERSION
|
129
|
+
- lib/trimify.rb
|
130
|
+
- spec/fixtures/category.rb
|
131
|
+
- spec/fixtures/no_table.rb
|
132
|
+
- spec/fixtures/post.rb
|
133
|
+
- spec/fixtures/schema.rb
|
134
|
+
- spec/fixtures/specialized_user.rb
|
135
|
+
- spec/fixtures/user.rb
|
136
|
+
- spec/spec_helper.rb
|
137
|
+
- spec/trimify_spec.rb
|
138
|
+
homepage: http://github.com/behrangsa/trimify
|
139
|
+
licenses:
|
140
|
+
- MIT
|
141
|
+
post_install_message:
|
142
|
+
rdoc_options: []
|
143
|
+
|
144
|
+
require_paths:
|
145
|
+
- lib
|
146
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
147
|
+
none: false
|
148
|
+
requirements:
|
149
|
+
- - ">="
|
150
|
+
- !ruby/object:Gem::Version
|
151
|
+
hash: 3
|
152
|
+
segments:
|
153
|
+
- 0
|
154
|
+
version: "0"
|
155
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
156
|
+
none: false
|
157
|
+
requirements:
|
158
|
+
- - ">="
|
159
|
+
- !ruby/object:Gem::Version
|
160
|
+
hash: 3
|
161
|
+
segments:
|
162
|
+
- 0
|
163
|
+
version: "0"
|
164
|
+
requirements: []
|
165
|
+
|
166
|
+
rubyforge_project:
|
167
|
+
rubygems_version: 1.8.5
|
168
|
+
signing_key:
|
169
|
+
specification_version: 3
|
170
|
+
summary: A gem for trimming strings in active record models.
|
171
|
+
test_files: []
|
172
|
+
|