vacuum_cleaner 0.1.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +72 -2
- data/Rakefile +19 -14
- data/init.rb +1 -4
- data/lib/vacuum_cleaner/normalizations/url.rb +4 -0
- data/lib/vacuum_cleaner/normalizations.rb +3 -3
- data/lib/vacuum_cleaner/normalizer.rb +19 -12
- data/lib/vacuum_cleaner.rb +3 -1
- data/rails/init.rb +1 -1
- data/test/unit/vacuum_cleaner/normalizations_test.rb +6 -6
- metadata +18 -8
- data/vacuum_cleaner.gemspec +0 -69
data/README.md
CHANGED
@@ -1,6 +1,76 @@
|
|
1
1
|
Vacuum Cleaner
|
2
2
|
==============
|
3
3
|
|
4
|
-
A Ruby (and Rails) attribute normalization gem.
|
4
|
+
A Ruby (and Rails) attribute normalization gem, which is supposedly [semver](http://semver.org/)-compliant.
|
5
5
|
|
6
|
-
|
6
|
+
Installation
|
7
|
+
------------
|
8
|
+
|
9
|
+
1. Install as gem from [rubygems](http://rubygems.org/gems/vacuum_cleaner): `sudo gem install vacuum_cleaner`.
|
10
|
+
Then load it in your app: `require 'rubygems'; require 'vacuum_cleaner'`
|
11
|
+
2. Install as gem using [bundler](http://github.com/carlhuda/bundler), add `gem "vacuum_cleaner"` to your
|
12
|
+
`Gemfile` and run `bundle install`
|
13
|
+
3. Or as a Rails plugin, for Rails 2.x run `./script/plugin install git://github.com/lwe/vacuum_cleaner.git`, when using Rails 3.x
|
14
|
+
goodeness run `rails plugin install git://github.com/lwe/vacuum_cleaner.git`
|
15
|
+
|
16
|
+
Usage
|
17
|
+
-----
|
18
|
+
|
19
|
+
It creates a new setter method for an attribute and thus allows the gem to reprozess the input value.
|
20
|
+
|
21
|
+
class Doctor
|
22
|
+
include VacuumCleaner::Normalizations # enables #normalizes, NOTE: not required for ActiveRecord models
|
23
|
+
attr_accessor :name # create some reader/writter
|
24
|
+
|
25
|
+
normalizes :name # enables strip/clean-up magic on attribute :name
|
26
|
+
end
|
27
|
+
|
28
|
+
@doc = Doctor.new
|
29
|
+
|
30
|
+
# set name with leading/trailing spaces
|
31
|
+
@doc.name = " Elliot Reid\n\t"
|
32
|
+
@doc.name # => "Elliot Reid"
|
33
|
+
|
34
|
+
# empty strings => nil
|
35
|
+
@doc.name = "\t\n"
|
36
|
+
@doc.name # => nil
|
37
|
+
|
38
|
+
Okay, this is it. Now, let the fun part begin...
|
39
|
+
|
40
|
+
# can be used with multiple attributes (if they all share the same normalizer)
|
41
|
+
normalizes :name, :company
|
42
|
+
|
43
|
+
# provides a fancy :downcase and :upcase normalizer (guess what they do)
|
44
|
+
normalizes :email, :downcase => true
|
45
|
+
# "JD@EXAMPLE.COM \n" => "jd@example.com"
|
46
|
+
|
47
|
+
# provides a :method normalizer which takes a string/symbol as argument which is
|
48
|
+
# then called upon the resulting value (if it respond_to)
|
49
|
+
normalizes :name, :method => :titleize
|
50
|
+
# "carla ESPINOSA" => "Carla Espinosa" PS: only works if ActiveSupport is available :)
|
51
|
+
|
52
|
+
# or a simple URL normalizer, which prefixes http:// if not starting with
|
53
|
+
# http or https
|
54
|
+
normalizes :homepage, :url => true
|
55
|
+
# "google.com" => "http://google.com"
|
56
|
+
# "http://example.com" => "http://example.com" PS: left as is
|
57
|
+
|
58
|
+
Assuming this already allows to fix 99.9% of all attribute normalization cases, if there's
|
59
|
+
that special need, then `normalizes` accepts a block:
|
60
|
+
|
61
|
+
# strips all whitespace within a string
|
62
|
+
normalizes(:phone) { |value| value.to_s.gsub(/\s+/, '') unless value.nil? }
|
63
|
+
# "\t+45 123 123 " => "+45123123" PS: yes, the standard strip etc. magic is still run
|
64
|
+
|
65
|
+
# no need for the default normalizer and feeling really custom?
|
66
|
+
normalizes(:phone, :default => false, :upcase => true) { |value| value.to_s.strip.gsub(/\s+/, '') }
|
67
|
+
# "\t0800 sacred heart" => "0800SACREDHEART"
|
68
|
+
# "\t\n" => ""
|
69
|
+
# nil => ""
|
70
|
+
|
71
|
+
Need access to the object within the block? As easy as:
|
72
|
+
|
73
|
+
# naming J.D. after some girly girl?
|
74
|
+
normalizes(:first_name) do |obj, attribute, value|
|
75
|
+
obj.name == "Dorian" ? %w{Agnes Shirley Denise}[rand(3)] : value
|
76
|
+
end
|
data/Rakefile
CHANGED
@@ -1,8 +1,10 @@
|
|
1
1
|
require 'rake'
|
2
2
|
require 'rake/testtask'
|
3
|
-
|
3
|
+
|
4
4
|
require File.join(File.dirname(__FILE__), 'lib', 'vacuum_cleaner')
|
5
5
|
|
6
|
+
task :default => :test
|
7
|
+
|
6
8
|
desc 'Test the vacuum_cleaner plugin.'
|
7
9
|
Rake::TestTask.new(:test) do |t|
|
8
10
|
t.libs << 'lib'
|
@@ -11,13 +13,18 @@ Rake::TestTask.new(:test) do |t|
|
|
11
13
|
t.verbose = true
|
12
14
|
end
|
13
15
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
16
|
+
begin
|
17
|
+
require 'yard'
|
18
|
+
desc 'Generate documentation for vacuum_cleaner. (requires yard)'
|
19
|
+
YARD::Rake::YardocTask.new(:doc) do |t|
|
20
|
+
t.files = ['lib/**/*.rb']
|
21
|
+
t.options = [
|
22
|
+
"--readme", "README.md",
|
23
|
+
"--title", "vacuum_cleaner (v#{VacuumCleaner::VERSION}) API Documentation"
|
24
|
+
]
|
25
|
+
end
|
26
|
+
rescue LoadError
|
27
|
+
puts "yard is not available (required for doc task). Install it with: sudo gem install yard (or use rdoc manually)"
|
21
28
|
end
|
22
29
|
|
23
30
|
begin
|
@@ -26,11 +33,7 @@ begin
|
|
26
33
|
gemspec.name = "vacuum_cleaner"
|
27
34
|
gemspec.version = VacuumCleaner::VERSION
|
28
35
|
gemspec.summary = "Ruby (and Rails) attribute cleaning support, provides some nice default normalization strategies."
|
29
|
-
description =
|
30
|
-
** Swoooosh ** - and all those leading and trailing whitespaces are gone, or ** Frooom ** - and the value
|
31
|
-
is normalized to always be prefixed by 'http://' and much more. Works with both plain old Ruby, and Rails (ActiveModel
|
32
|
-
and ActiveSupport).
|
33
|
-
DESC
|
36
|
+
description = "Ruby (and Rails) attribute cleaning support, provides some nice and easy to enhance default normalization strategies."
|
34
37
|
gemspec.description = description.strip
|
35
38
|
gemspec.email = "lukas.westermann@gmail.com"
|
36
39
|
gemspec.homepage = "http://github.com/lwe/vacuum_cleaner"
|
@@ -40,7 +43,9 @@ begin
|
|
40
43
|
|
41
44
|
gemspec.add_development_dependency('shoulda', '>= 2.10.2')
|
42
45
|
gemspec.add_development_dependency('rr', '>= 0.10.5')
|
43
|
-
|
46
|
+
gemspec.add_development_dependency('activesupport', '>= 2.3.5')
|
47
|
+
|
48
|
+
gemspec.files.reject! { |file| file =~ /\.gemspec$/ } # kinda redundant
|
44
49
|
end
|
45
50
|
Jeweler::GemcutterTasks.new
|
46
51
|
rescue LoadError
|
data/init.rb
CHANGED
@@ -1,4 +1 @@
|
|
1
|
-
require
|
2
|
-
|
3
|
-
ActiveRecord::Base.class_eval { include VacuumCleaner::Normalizations } # all versions of rails
|
4
|
-
ActiveModel::Base.class_eval { include VacuumCleaner::Normalizations } if defined?(ActiveModel::Base) # Rails 3+
|
1
|
+
require File.dirname(__FILE__) + "rails/init"
|
@@ -33,7 +33,7 @@ module VacuumCleaner
|
|
33
33
|
block_given? ? (block.arity == 1 ? yield(value) : yield(self, attribute, value)) : value
|
34
34
|
end
|
35
35
|
original_setter = "#{attribute}#{VacuumCleaner::WITHOUT_NORMALIZATION_SUFFIX}=".to_sym
|
36
|
-
send(:alias_method, original_setter, "#{attribute}=") if instance_methods.include?("#{attribute}=")
|
36
|
+
send(:alias_method, original_setter, "#{attribute}=") if instance_methods.include?(RUBY_VERSION =~ /^1.9/ ? :"#{attribute}=" : "#{attribute}=")
|
37
37
|
|
38
38
|
rb_src = <<-RUBY
|
39
39
|
def #{attribute}=(value) # 1. def name=(value)
|
@@ -52,9 +52,9 @@ module VacuumCleaner
|
|
52
52
|
|
53
53
|
# Okay, because this library currently does not depend on
|
54
54
|
# <tt>ActiveSupport</tt> or anything similar an "independent" camelizing process is
|
55
|
-
# required.
|
55
|
+
# required.
|
56
56
|
#
|
57
|
-
# If <tt>value.to_s</tt> responds to <tt>:camelize</tt>, then call it else use implementation
|
57
|
+
# <b>How it works:</b> If <tt>value.to_s</tt> responds to <tt>:camelize</tt>, then call it else, use implementation
|
58
58
|
# taken from http://github.com/rails/rails/blob/master/activesupport/lib/active_support/inflector/methods.rb#L25
|
59
59
|
def camelize_value(value)
|
60
60
|
value = value.to_s
|
@@ -2,31 +2,32 @@ module VacuumCleaner #:nodoc:
|
|
2
2
|
|
3
3
|
# A small base class for implementing custom value normalizers.
|
4
4
|
# Might seem like a slight overkill, yet makes the library pretty
|
5
|
-
# reusable and all.
|
6
|
-
#
|
7
|
-
#
|
8
|
-
# class Person
|
9
|
-
# include VacuumCleaner::Normalizations
|
10
|
-
# normalizes :name, :titleize => true
|
11
|
-
# end
|
5
|
+
# reusable and all. Works like ActiveModel validators.
|
12
6
|
#
|
13
7
|
# class TitleizeNormalizer < VacuumCleaner::Normalizer
|
14
8
|
# def normalize_value(value)
|
15
9
|
# value.titelize unless value.blank?
|
16
10
|
# end
|
17
11
|
# end
|
12
|
+
#
|
13
|
+
# class Person
|
14
|
+
# include VacuumCleaner::Normalizations
|
15
|
+
# normalizes :name, :titleize => true
|
16
|
+
# end
|
18
17
|
#
|
19
18
|
# Any class that inherits from +VacuumCleaner::Normalizer+ must implement
|
20
|
-
# a method called <tt>normalize_value</tt> which accepts the <tt>value</tt> to normalize.
|
21
|
-
# the value returned by <tt>
|
19
|
+
# a method called <tt>normalize_value</tt> which accepts the <tt>value</tt> to normalize.
|
20
|
+
# Furthermore the value returned by <tt>normalize_value</tt> is used as the new value for
|
22
21
|
# the attribute.
|
23
22
|
#
|
24
23
|
# To reuse the behaviour as defined by the default normalizer (strip & empty),
|
25
24
|
# just use <tt>super</tt>.
|
26
25
|
#
|
27
|
-
# class
|
26
|
+
# class MoreNilNormalizer < VacuumCleaner::Normalizer
|
28
27
|
# def normalize_value(value)
|
29
|
-
# super
|
28
|
+
# value = super
|
29
|
+
# value = value.downcase if value.respond_to(:downcase)
|
30
|
+
# %w{0 nil null nul zero nix}.include?(value) ? nil : value
|
30
31
|
# end
|
31
32
|
# end
|
32
33
|
#
|
@@ -39,9 +40,15 @@ module VacuumCleaner #:nodoc:
|
|
39
40
|
# end
|
40
41
|
# end
|
41
42
|
#
|
43
|
+
# When the normalization process is started for an attribute, the method
|
44
|
+
# +normalize+ is called with the object, attribute and the value. The default
|
45
|
+
# implementation of +normalize+ (as provided by this class) just calls
|
46
|
+
# +normalize_value+.
|
47
|
+
#
|
42
48
|
# This can be used together with the +normalizes+ method (see
|
43
|
-
# VacuumCleaner::Normalizers.normalizes for more on this).
|
49
|
+
# {{VacuumCleaner::Normalizers.normalizes}} for more on this).
|
44
50
|
class Normalizer
|
51
|
+
# Options as supplied to the normalizer.
|
45
52
|
attr_reader :options
|
46
53
|
|
47
54
|
# Accepts an array of options, which will be made available through the +options+ reader.
|
data/lib/vacuum_cleaner.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
|
+
# Fancy value normalization utility for ruby (and rails).
|
1
2
|
module VacuumCleaner
|
2
|
-
|
3
|
+
# +VacuumCleaner+ version string, current version is 0.1.0.
|
4
|
+
VERSION = "0.5.0".freeze
|
3
5
|
|
4
6
|
autoload :Normalizer, 'vacuum_cleaner/normalizer'
|
5
7
|
autoload :Normalizations, 'vacuum_cleaner/normalizations'
|
data/rails/init.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
1
|
require 'vacuum_cleaner'
|
2
2
|
|
3
3
|
ActiveRecord::Base.class_eval { include VacuumCleaner::Normalizations } # all versions of rails
|
4
|
-
ActiveModel::Base.class_eval { include VacuumCleaner::Normalizations } if defined?(ActiveModel::Base) # Rails 3+
|
4
|
+
# ActiveModel::Base.class_eval { include VacuumCleaner::Normalizations } if defined?(ActiveModel::Base) # Rails 3+
|
@@ -50,32 +50,32 @@ class VacuumCleaner::NormalizationsTest < Test::Unit::TestCase
|
|
50
50
|
should "take multiple symbols as argument" do
|
51
51
|
klass = Class.new { include VacuumCleaner::Normalizations; normalizes(:name, :first_name) }
|
52
52
|
assert_respond_to klass, :normalizes
|
53
|
-
#assert_respond_to klass, :normalize_name
|
54
|
-
#assert_respond_to klass, :normalize_first_name
|
55
53
|
end
|
56
54
|
|
57
55
|
should "create a setter for supplied attribute" do
|
58
56
|
obj = Class.new { include VacuumCleaner::Normalizations; normalizes(:name) }.new
|
59
57
|
assert_respond_to obj, :name=
|
58
|
+
assert_respond_to obj, :normalize_name
|
60
59
|
end
|
61
60
|
|
62
61
|
should "set the instance variable using the setter" do
|
63
|
-
obj = Class.new { include VacuumCleaner::Normalizations; normalizes(:name) }.new
|
62
|
+
obj = Class.new { include VacuumCleaner::Normalizations; attr_reader :name; normalizes(:name) }.new
|
64
63
|
obj.name = "J.D."
|
65
|
-
assert_equal "J.D.", obj.
|
64
|
+
assert_equal "J.D.", obj.name
|
66
65
|
end
|
67
66
|
|
68
67
|
should "alias method to <attr>_without_normalization= if <attr>= already defined" do
|
69
68
|
klass = Class.new do
|
70
69
|
include VacuumCleaner::Normalizations
|
70
|
+
attr_reader :name, :foo
|
71
71
|
def name=(name); @foo = name end
|
72
72
|
normalizes :name
|
73
73
|
end
|
74
74
|
obj = klass.new
|
75
75
|
obj.name = "Elliot Reid"
|
76
76
|
assert_respond_to obj, :name_without_normalization=
|
77
|
-
assert_equal "Elliot Reid", obj.
|
78
|
-
assert_nil obj.
|
77
|
+
assert_equal "Elliot Reid", obj.foo
|
78
|
+
assert_nil obj.name
|
79
79
|
end
|
80
80
|
|
81
81
|
should "convert any blank input, like empty string, nil etc. to => <nil>" do
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
7
|
+
- 5
|
8
8
|
- 0
|
9
|
-
version: 0.
|
9
|
+
version: 0.5.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Lukas Westermann
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-
|
17
|
+
date: 2010-03-09 00:00:00 +01:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -45,10 +45,21 @@ dependencies:
|
|
45
45
|
version: 0.10.5
|
46
46
|
type: :development
|
47
47
|
version_requirements: *id002
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: activesupport
|
50
|
+
prerelease: false
|
51
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
segments:
|
56
|
+
- 2
|
57
|
+
- 3
|
58
|
+
- 5
|
59
|
+
version: 2.3.5
|
60
|
+
type: :development
|
61
|
+
version_requirements: *id003
|
62
|
+
description: Ruby (and Rails) attribute cleaning support, provides some nice and easy to enhance default normalization strategies.
|
52
63
|
email: lukas.westermann@gmail.com
|
53
64
|
executables: []
|
54
65
|
|
@@ -73,7 +84,6 @@ files:
|
|
73
84
|
- test/unit/vacuum_cleaner/normalizations/url_test.rb
|
74
85
|
- test/unit/vacuum_cleaner/normalizations_test.rb
|
75
86
|
- test/unit/vacuum_cleaner/normalizer_test.rb
|
76
|
-
- vacuum_cleaner.gemspec
|
77
87
|
has_rdoc: true
|
78
88
|
homepage: http://github.com/lwe/vacuum_cleaner
|
79
89
|
licenses:
|
data/vacuum_cleaner.gemspec
DELETED
@@ -1,69 +0,0 @@
|
|
1
|
-
# Generated by jeweler
|
2
|
-
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
-
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
-
# -*- encoding: utf-8 -*-
|
5
|
-
|
6
|
-
Gem::Specification.new do |s|
|
7
|
-
s.name = %q{vacuum_cleaner}
|
8
|
-
s.version = "0.1.0"
|
9
|
-
|
10
|
-
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
-
s.authors = ["Lukas Westermann"]
|
12
|
-
s.date = %q{2010-02-24}
|
13
|
-
s.description = %q{** Swoooosh ** - and all those leading and trailing whitespaces are gone, or ** Frooom ** - and the value
|
14
|
-
is normalized to always be prefixed by 'http://' and much more. Works with both plain old Ruby, and Rails (ActiveModel
|
15
|
-
and ActiveSupport).}
|
16
|
-
s.email = %q{lukas.westermann@gmail.com}
|
17
|
-
s.extra_rdoc_files = [
|
18
|
-
"README.md"
|
19
|
-
]
|
20
|
-
s.files = [
|
21
|
-
".gitignore",
|
22
|
-
"LICENSE",
|
23
|
-
"README.md",
|
24
|
-
"Rakefile",
|
25
|
-
"init.rb",
|
26
|
-
"lib/vacuum_cleaner.rb",
|
27
|
-
"lib/vacuum_cleaner/normalizations.rb",
|
28
|
-
"lib/vacuum_cleaner/normalizations/method.rb",
|
29
|
-
"lib/vacuum_cleaner/normalizations/url.rb",
|
30
|
-
"lib/vacuum_cleaner/normalizer.rb",
|
31
|
-
"rails/init.rb",
|
32
|
-
"test/test_helper.rb",
|
33
|
-
"test/unit/vacuum_cleaner/normalizations/method_test.rb",
|
34
|
-
"test/unit/vacuum_cleaner/normalizations/url_test.rb",
|
35
|
-
"test/unit/vacuum_cleaner/normalizations_test.rb",
|
36
|
-
"test/unit/vacuum_cleaner/normalizer_test.rb",
|
37
|
-
"vacuum_cleaner.gemspec"
|
38
|
-
]
|
39
|
-
s.homepage = %q{http://github.com/lwe/vacuum_cleaner}
|
40
|
-
s.licenses = ["LICENSE"]
|
41
|
-
s.rdoc_options = ["--charset=UTF-8"]
|
42
|
-
s.require_paths = ["lib"]
|
43
|
-
s.rubygems_version = %q{1.3.6}
|
44
|
-
s.summary = %q{Ruby (and Rails) attribute cleaning support, provides some nice default normalization strategies.}
|
45
|
-
s.test_files = [
|
46
|
-
"test/test_helper.rb",
|
47
|
-
"test/unit/vacuum_cleaner/normalizations/method_test.rb",
|
48
|
-
"test/unit/vacuum_cleaner/normalizations/url_test.rb",
|
49
|
-
"test/unit/vacuum_cleaner/normalizations_test.rb",
|
50
|
-
"test/unit/vacuum_cleaner/normalizer_test.rb"
|
51
|
-
]
|
52
|
-
|
53
|
-
if s.respond_to? :specification_version then
|
54
|
-
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
55
|
-
s.specification_version = 3
|
56
|
-
|
57
|
-
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
58
|
-
s.add_development_dependency(%q<shoulda>, [">= 2.10.2"])
|
59
|
-
s.add_development_dependency(%q<rr>, [">= 0.10.5"])
|
60
|
-
else
|
61
|
-
s.add_dependency(%q<shoulda>, [">= 2.10.2"])
|
62
|
-
s.add_dependency(%q<rr>, [">= 0.10.5"])
|
63
|
-
end
|
64
|
-
else
|
65
|
-
s.add_dependency(%q<shoulda>, [">= 2.10.2"])
|
66
|
-
s.add_dependency(%q<rr>, [">= 0.10.5"])
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|