vacuum_cleaner 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +1 -0
- data/README.md +53 -13
- data/Rakefile +16 -4
- data/init.rb +1 -1
- data/lib/vacuum_cleaner/normalizations/active_support.rb +33 -0
- data/lib/vacuum_cleaner/normalizations/method.rb +23 -9
- data/lib/vacuum_cleaner/normalizations/numeric.rb +39 -0
- data/lib/vacuum_cleaner/railtie.rb +7 -0
- data/lib/vacuum_cleaner.rb +12 -5
- data/test/integration/active_support_integration_test.rb +55 -0
- data/test/test_helper.rb +3 -0
- data/test/unit/vacuum_cleaner/normalizations/numeric_test.rb +35 -0
- data/test/unit/vacuum_cleaner/normalizations_test.rb +8 -5
- metadata +20 -5
- data/rails/init.rb +0 -3
data/.gitignore
CHANGED
data/README.md
CHANGED
|
@@ -3,12 +3,15 @@ Vacuum Cleaner
|
|
|
3
3
|
|
|
4
4
|
A Ruby (and Rails) attribute normalization gem, which is supposedly [semver](http://semver.org/)-compliant.
|
|
5
5
|
|
|
6
|
+
Known to work with Ruby-1.9, JRuby and both Rails 2.3.x and 3. If there are any issues, please file a
|
|
7
|
+
[bug report](http://github.com/lwe/vacuum_cleaner/issues), or fix it and send me a pull request.
|
|
8
|
+
|
|
6
9
|
Installation
|
|
7
10
|
------------
|
|
8
11
|
|
|
9
12
|
1. Install as gem from [rubygems](http://rubygems.org/gems/vacuum_cleaner): `sudo gem install vacuum_cleaner`.
|
|
10
13
|
Then load it in your app: `require 'rubygems'; require 'vacuum_cleaner'`
|
|
11
|
-
2. Install as gem using [bundler](http://
|
|
14
|
+
2. Install as gem using [bundler](http://gembundler.com), add `gem "vacuum_cleaner"` to your
|
|
12
15
|
`Gemfile` and run `bundle install`
|
|
13
16
|
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
17
|
goodeness run `rails plugin install git://github.com/lwe/vacuum_cleaner.git`
|
|
@@ -16,26 +19,29 @@ Installation
|
|
|
16
19
|
Usage
|
|
17
20
|
-----
|
|
18
21
|
|
|
19
|
-
It creates a new setter method for an attribute and thus allows the
|
|
22
|
+
It creates a new setter method for an attribute and thus allows the normalizers defined to reprocess the input value.
|
|
20
23
|
|
|
21
24
|
class Doctor
|
|
22
|
-
include VacuumCleaner::Normalizations # enables #normalizes, NOTE: not required for ActiveRecord models
|
|
25
|
+
include VacuumCleaner::Normalizations # enables #normalizes, NOTE: not required for ActiveRecord models
|
|
23
26
|
attr_accessor :name # create some reader/writter
|
|
24
27
|
|
|
25
28
|
normalizes :name # enables strip/clean-up magic on attribute :name
|
|
26
29
|
end
|
|
27
30
|
|
|
31
|
+
Using `normalizes` just adds a default normalization implemenation, which removes leading/trailing
|
|
32
|
+
whitespace and converts spaces only to `nil`. Everything happens upon "set".
|
|
33
|
+
|
|
28
34
|
@doc = Doctor.new
|
|
29
|
-
|
|
30
|
-
# set name with leading/trailing spaces
|
|
31
35
|
@doc.name = " Elliot Reid\n\t"
|
|
32
|
-
@doc.name # => "Elliot Reid"
|
|
36
|
+
@doc.name # => "Elliot Reid" => trailing space was stripped
|
|
33
37
|
|
|
34
|
-
# empty strings => nil
|
|
35
38
|
@doc.name = "\t\n"
|
|
36
|
-
@doc.name # => nil
|
|
39
|
+
@doc.name # => nil => converted to nil
|
|
40
|
+
|
|
41
|
+
Okay, this is how it basically works, the `normalizes` call just generates a new setter method,
|
|
42
|
+
which `normalizes` the input value and then calls the original setter method.
|
|
37
43
|
|
|
38
|
-
|
|
44
|
+
What else can be done then?
|
|
39
45
|
|
|
40
46
|
# can be used with multiple attributes (if they all share the same normalizer)
|
|
41
47
|
normalizes :name, :company
|
|
@@ -54,9 +60,12 @@ Okay, this is it. Now, let the fun part begin...
|
|
|
54
60
|
normalizes :homepage, :url => true
|
|
55
61
|
# "google.com" => "http://google.com"
|
|
56
62
|
# "http://example.com" => "http://example.com" PS: left as is
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
63
|
+
|
|
64
|
+
Take a look at `VacuumCleaner::Normalizer`, about how the process works and how custom
|
|
65
|
+
reusable normalizers can be written. For the-quick-fix-that-shouldnt-have-been-used-but-was
|
|
66
|
+
case or if there's no reuse, `normalizes` takes a block as argument which is called
|
|
67
|
+
after any other normalizer in the chain. Note that normalizers are not halted, nor stopped
|
|
68
|
+
if they return `nil` or `false` or something similar, so ensure that case is handled properly.
|
|
60
69
|
|
|
61
70
|
# strips all whitespace within a string
|
|
62
71
|
normalizes(:phone) { |value| value.to_s.gsub(/\s+/, '') unless value.nil? }
|
|
@@ -68,9 +77,40 @@ that special need, then `normalizes` accepts a block:
|
|
|
68
77
|
# "\t\n" => ""
|
|
69
78
|
# nil => ""
|
|
70
79
|
|
|
71
|
-
Need access to the object within the block? As easy as:
|
|
80
|
+
Need access to the full object within the block? As easy as:
|
|
72
81
|
|
|
73
82
|
# naming J.D. after some girly girl?
|
|
74
83
|
normalizes(:first_name) do |obj, attribute, value|
|
|
75
84
|
obj.name == "Dorian" ? %w{Agnes Shirley Denise}[rand(3)] : value
|
|
76
85
|
end
|
|
86
|
+
|
|
87
|
+
Background
|
|
88
|
+
----------
|
|
89
|
+
|
|
90
|
+
As mentoined earlier `normalizes` creates a new setter method, so let's shortly take a look
|
|
91
|
+
at how.
|
|
92
|
+
|
|
93
|
+
normalizes(:name)
|
|
94
|
+
|
|
95
|
+
# 1. creates a :normalize_name method, which contains the normalization chain, block etc.
|
|
96
|
+
# 2. if :name= exists, it's aliased to :name_without_normalization=
|
|
97
|
+
# 3. creates a new :name= method, which calls :normalize_name, then tries to
|
|
98
|
+
# set the normalized value by one of:
|
|
99
|
+
# a) calling :name_without_normalization=, if defined
|
|
100
|
+
# b) self[:name] = v, if it responds to :[]= (for ActiveRecord support)
|
|
101
|
+
# c) or, as a fallback, sets @name to the result of :normalize_name
|
|
102
|
+
|
|
103
|
+
Lessons learned: when the need arises to set the value without any normalization and there's
|
|
104
|
+
a setter just use `@object.name_without_normalization = "har har har\n\t"`. Feel free to
|
|
105
|
+
completly override `normalize_<attribute>`, but a much smarter way to add very custom normalizers
|
|
106
|
+
is by a) providing a block to `normalizes` or b) create a custom `VacuumCleaner::Normalizer`
|
|
107
|
+
implementation.
|
|
108
|
+
|
|
109
|
+
Some info about the different files, might be a good place to look at when trying to figure
|
|
110
|
+
out how to write custom `VacuumCleaner::Normalizer` implemenations, or for a look at how
|
|
111
|
+
it works.
|
|
112
|
+
|
|
113
|
+
lib/vacuum_cleaner/normalizer.rb # Base Normalizer implementation
|
|
114
|
+
lib/vacuum_cleaner/normalizations/*.rb # Some default Normalizer implementations, like url, downcase etc.
|
|
115
|
+
lib/vacuum_cleaner/normalizations.rb # Provides the `normalizes` method and all the logic etc.
|
|
116
|
+
|
data/Rakefile
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
require 'rake'
|
|
2
2
|
require 'rake/testtask'
|
|
3
|
-
|
|
4
|
-
require
|
|
3
|
+
$: << File.join(File.dirname(__FILE__), 'lib')
|
|
4
|
+
require 'vacuum_cleaner'
|
|
5
5
|
|
|
6
6
|
begin
|
|
7
7
|
require 'yard'
|
|
@@ -35,7 +35,7 @@ begin
|
|
|
35
35
|
gemspec.add_development_dependency('shoulda', '>= 2.10.2')
|
|
36
36
|
gemspec.add_development_dependency('activesupport', '>= 2.3.5')
|
|
37
37
|
|
|
38
|
-
gemspec.files.reject! { |file| file =~ /\.gemspec$/ } #
|
|
38
|
+
gemspec.files.reject! { |file| file =~ /\.gemspec$/ || file =~ /\.png$/ || file =~ /\.watcher_example+$/} # do not include gemspec + png files
|
|
39
39
|
end
|
|
40
40
|
Jeweler::GemcutterTasks.new
|
|
41
41
|
rescue LoadError
|
|
@@ -64,7 +64,19 @@ namespace :test do
|
|
|
64
64
|
t.verbose = true
|
|
65
65
|
end
|
|
66
66
|
|
|
67
|
+
desc 'Run both the integration and unit tests'
|
|
67
68
|
task :all => [:'test:unit', :'test:integration']
|
|
69
|
+
|
|
70
|
+
desc 'Run test with supplied rakes like RAKES=jrake,rake19,rake'
|
|
71
|
+
task :portability do |t|
|
|
72
|
+
raise "No RAKES specified e.g. add RAKES=rake,rake19" unless ENV['RAKES']
|
|
73
|
+
ENV['RAKES'].split(/[,;\s]\s*/).each do |rake_cmd|
|
|
74
|
+
puts
|
|
75
|
+
puts " * * * runing `#{rake_cmd} test:all`"
|
|
76
|
+
puts
|
|
77
|
+
system("#{rake_cmd} test:all")
|
|
78
|
+
end
|
|
79
|
+
end
|
|
68
80
|
end
|
|
69
81
|
|
|
70
82
|
namespace :metrics do
|
|
@@ -85,7 +97,7 @@ namespace :metrics do
|
|
|
85
97
|
task :coverage do |t|
|
|
86
98
|
rm_f "doc/coverage"
|
|
87
99
|
mkdir_p "doc/coverage"
|
|
88
|
-
rcov = %(rcov -Ilib:test --exclude '\/gems\/' -o doc/coverage -T test/unit/vacuum_cleaner/*_test.rb -T test/unit/vacuum_cleaner/*/*_test.rb)
|
|
100
|
+
rcov = %(rcov -Ilib:test --exclude '\/gems\/' -o doc/coverage -T test/integration/*_test.rb -T test/unit/vacuum_cleaner/*_test.rb -T test/unit/vacuum_cleaner/*/*_test.rb)
|
|
89
101
|
system rcov
|
|
90
102
|
end
|
|
91
103
|
|
data/init.rb
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
require
|
|
1
|
+
require 'vacuum_cleaner'
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# Okay, because about 99.9% of the usage is with Rails/AS, I guess it should
|
|
2
|
+
# have better/deeper support for it, like support for Multibyte stuff and some
|
|
3
|
+
# (useful) inflections.
|
|
4
|
+
#
|
|
5
|
+
# Please note that the +TransliterateNormalizer+ requires rails 3.0.0.beta3 and
|
|
6
|
+
# when run with ruby 1.9.
|
|
7
|
+
#
|
|
8
|
+
if defined?(::ActiveSupport)
|
|
9
|
+
require 'active_support/inflector'
|
|
10
|
+
|
|
11
|
+
require 'vacuum_cleaner/normalizations/method'
|
|
12
|
+
|
|
13
|
+
module VacuumCleaner
|
|
14
|
+
module Normalizations
|
|
15
|
+
# Calls `ActiveSupport::Inflector.transliterate` if supplied
|
|
16
|
+
# +value+ responds to +to_str+, so it basically only works on strings.
|
|
17
|
+
class TransliterateNormalizer < Normalizer
|
|
18
|
+
def normalize_value(value)
|
|
19
|
+
::ActiveSupport::Inflector.transliterate(value.to_str).to_s if value.respond_to?(:to_str)
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# Calls the `titleize` method from AS on the supplied value, if possible.
|
|
24
|
+
TitleizeNormalizer = MethodNormalizer.build(:titleize)
|
|
25
|
+
|
|
26
|
+
# Calls the `humanize` method from AS on the supplied value.
|
|
27
|
+
HumanizeNormalizer = MethodNormalizer.build(:humanize)
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# Set the multibyte proxy class to AS::Multibyte::Chars, which in turn works perfectly with UTF8 chars et al.
|
|
32
|
+
VacuumCleaner::Normalizations::MethodNormalizer.multibyte_wrapper = ::ActiveSupport::Multibyte::Chars
|
|
33
|
+
end
|
|
@@ -13,13 +13,26 @@ module VacuumCleaner
|
|
|
13
13
|
# Subclasses of the +MethodNormalizer+ can take advantage of it's
|
|
14
14
|
# +normalize_if_respond_to+ method, to easily create custom
|
|
15
15
|
# normalizers based on methods availble on the result value.
|
|
16
|
-
class MethodNormalizer < Normalizer
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
16
|
+
class MethodNormalizer < Normalizer
|
|
17
|
+
|
|
18
|
+
class << self
|
|
19
|
+
# Helper method to "bake" a method normalizer from a method, enabling us to do stuff like.
|
|
20
|
+
#
|
|
21
|
+
# TitelizeNormalizer = MethodNormalizer.build(:titleize)
|
|
22
|
+
#
|
|
23
|
+
def build(sym)
|
|
24
|
+
module_eval "Class.new(MethodNormalizer) do; def initialize(*args); super({ :method => #{sym.inspect}}) end; end", __FILE__, __LINE__
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# Due to the lack of multibyte support in ruby core, a proxy class like
|
|
28
|
+
# {ActiveSupport::Multibyte::Chars} can be registered here and the proxy
|
|
29
|
+
# is then used to wrap string values within, so that methods like `upcase`
|
|
30
|
+
# or `downcase` work with characters outside the ASCII range as well.
|
|
31
|
+
#
|
|
32
|
+
# The wrapper is only used if the value supplied is a string, i.e. responds to
|
|
33
|
+
# +to_str+.
|
|
34
|
+
def multibyte_wrapper=(clazz); @multibyte_wrapper = clazz end
|
|
35
|
+
def multibyte_wrapper; @multibyte_wrapper end
|
|
23
36
|
end
|
|
24
37
|
|
|
25
38
|
# Accept either a hash or symbol name.
|
|
@@ -27,18 +40,19 @@ module VacuumCleaner
|
|
|
27
40
|
args = { :method => args } unless args.is_a?(Hash)
|
|
28
41
|
super(args)
|
|
29
42
|
end
|
|
30
|
-
|
|
43
|
+
|
|
31
44
|
# Normalize value by trying to call the method at hand, if
|
|
32
45
|
# +value+ does not respond to the defined method, returns +nil+.
|
|
33
46
|
def normalize_value(value)
|
|
34
47
|
sym = options[:method]
|
|
48
|
+
value = MethodNormalizer.multibyte_wrapper.new(value) if MethodNormalizer.multibyte_wrapper and value.respond_to?(:to_str)
|
|
35
49
|
value.respond_to?(sym) ? value.send(sym) : nil
|
|
36
50
|
end
|
|
37
51
|
end
|
|
38
52
|
|
|
39
53
|
# Downcase value unless nil or empty.
|
|
40
54
|
DowncaseNormalizer = MethodNormalizer.build(:downcase)
|
|
41
|
-
|
|
55
|
+
|
|
42
56
|
# Upcases value unless nil or empty.
|
|
43
57
|
UpcaseNormalizer = MethodNormalizer.build(:upcase)
|
|
44
58
|
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
module VacuumCleaner
|
|
2
|
+
module Normalizations
|
|
3
|
+
|
|
4
|
+
# The numeric normalizer tries to normalize a numeric string input
|
|
5
|
+
# like <tt>CHF 12'000.--</tt> into a string parseable by {{Kernel.Float}}
|
|
6
|
+
# or similar (result would be <tt>12000.</tt>).
|
|
7
|
+
#
|
|
8
|
+
# It basically strips any non valid character for a number from the
|
|
9
|
+
# input string (scientific notation is currently not support), so all
|
|
10
|
+
# whitespace, currency symbols and units are stripped. Furthermore also
|
|
11
|
+
# the decimal points are normalized, because in Germany numbers can look
|
|
12
|
+
# like: <tt>EUR 12.000,50</tt> and in Switzerland they can look like
|
|
13
|
+
# <tt>1,5 Mio.</tt>. So the normalizer tries to ensure that both are
|
|
14
|
+
# numbers parseable by {{Kernel.Float}}, by intelligently trying to
|
|
15
|
+
# figure out the seperator used and converting it to <tt>.</tt>.
|
|
16
|
+
#
|
|
17
|
+
# All values which do not respond to <tt>to_str</tt> are left as is.
|
|
18
|
+
#
|
|
19
|
+
# Note: no conversion or anything similar is done! The value wont be
|
|
20
|
+
# converted to a +Fixnum+ or whatever and will be left as string.
|
|
21
|
+
# When used with Rails validations, this might also certainly render
|
|
22
|
+
# the validations to check if it's a valid number obsolete, yet all
|
|
23
|
+
# the stuff about min/max, fixed only etc. work of course like a charm.
|
|
24
|
+
#
|
|
25
|
+
class NumericNormalizer < Normalizer
|
|
26
|
+
def normalize_value(value)
|
|
27
|
+
if value.respond_to?(:to_str)
|
|
28
|
+
num = value.to_str.gsub(/\s*/, '') # I. remove all spaces
|
|
29
|
+
num.gsub!(/([^\d\-])\./, '\1') # II. remove misleading points in units like "Mio." or "SFr."
|
|
30
|
+
num.gsub!(/[^\d,\.\-]/i, '') # III. remove all chars we are not interested in, like anything not related to numeric :)
|
|
31
|
+
num.gsub!("--", '') # IV. remove double dashes, like often used in CH
|
|
32
|
+
num = num.gsub(",", '.').gsub(".") { $'.include?(".") ? "" : "." }.gsub(/\.\z/, '') # V. intelligently convert comma to points...
|
|
33
|
+
else
|
|
34
|
+
value
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
data/lib/vacuum_cleaner.rb
CHANGED
|
@@ -1,12 +1,19 @@
|
|
|
1
1
|
# Fancy value normalization utility for ruby (and rails),
|
|
2
2
|
# see {VacuumCleaner::Normalizations} for more information about usage.
|
|
3
3
|
#
|
|
4
|
+
|
|
5
|
+
require 'vacuum_cleaner/normalizer'
|
|
6
|
+
require 'vacuum_cleaner/normalizations'
|
|
7
|
+
|
|
4
8
|
# @see VacuumCleaner::Normalizations
|
|
5
9
|
# @see VacuumCleaner::Normalizer
|
|
6
10
|
module VacuumCleaner
|
|
7
11
|
# +VacuumCleaner+ version
|
|
8
|
-
VERSION = "1.0.
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
12
|
+
VERSION = "1.0.1".freeze
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
if defined?(ActiveSupport) && ActiveSupport.respond_to?(:on_load)
|
|
16
|
+
ActiveSupport.on_load(:active_record) { include VacuumCleaner::Normalizations }
|
|
17
|
+
else
|
|
18
|
+
ActiveRecord::Base.send(:include, VacuumCleaner::Normalizations) if defined?(ActiveRecord)
|
|
19
|
+
end
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
$KCODE = 'U' if RUBY_VERSION < '1.9'
|
|
3
|
+
|
|
4
|
+
require 'test_helper'
|
|
5
|
+
|
|
6
|
+
# run init.rb, which should load VacuumCleaner
|
|
7
|
+
require 'active_support'
|
|
8
|
+
require 'active_support/version'
|
|
9
|
+
|
|
10
|
+
require File.join(File.dirname(__FILE__), '..', '..', 'init')
|
|
11
|
+
|
|
12
|
+
puts "Running integration tests against: active_support-#{ActiveSupport::VERSION::STRING}"
|
|
13
|
+
|
|
14
|
+
class ActiveSupportIntegrationTest < ::Test::Unit::TestCase
|
|
15
|
+
include VacuumCleaner::Normalizations
|
|
16
|
+
|
|
17
|
+
context "VacuumCleaner::Normalizations::ActiveSupport" do
|
|
18
|
+
context "TransliterateNormalizer" do
|
|
19
|
+
should "call AS::Inflector#translitrate if value responds to to_str" do
|
|
20
|
+
assert_equal "Geneve", TransliterateNormalizer.new.normalize_value("Genève")
|
|
21
|
+
assert_equal "Zurich", TransliterateNormalizer.new.normalize_value("Zürich")
|
|
22
|
+
assert_equal "Bern", TransliterateNormalizer.new.normalize_value("Bern")
|
|
23
|
+
assert_equal "", TransliterateNormalizer.new.normalize_value("")
|
|
24
|
+
assert_equal "\n ", TransliterateNormalizer.new.normalize_value("\n ")
|
|
25
|
+
|
|
26
|
+
assert_nil TransliterateNormalizer.new.normalize_value(nil)
|
|
27
|
+
assert_nil TransliterateNormalizer.new.normalize_value(12.5)
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
context "TitleizeNormalizer" do
|
|
32
|
+
should "call #titleize on string, else return nil" do
|
|
33
|
+
assert_equal "My First Day", TitleizeNormalizer.new.normalize_value("my first day")
|
|
34
|
+
assert_equal "My Mentor", TitleizeNormalizer.new.normalize_value("MY MENTOR")
|
|
35
|
+
assert_nil TitleizeNormalizer.new.normalize_value(nil)
|
|
36
|
+
assert_nil TitleizeNormalizer.new.normalize_value(42)
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
context "HumanizeNormalizer" do
|
|
41
|
+
should "call #humanize on string, else return nil" do
|
|
42
|
+
assert_equal "My old lady", HumanizeNormalizer.new.normalize_value("My Old Lady")
|
|
43
|
+
assert_equal "My two dads", HumanizeNormalizer.new.normalize_value("my two dads")
|
|
44
|
+
assert_nil HumanizeNormalizer.new.normalize_value(nil)
|
|
45
|
+
assert_nil HumanizeNormalizer.new.normalize_value(123.5)
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
should "translitarte and upcase input value" do
|
|
50
|
+
obj = Class.new { include VacuumCleaner::Normalizations; attr_reader :prefix; normalizes(:prefix, :upcase => true, :transliterate => true) }.new
|
|
51
|
+
obj.prefix = "Genève\n\t"
|
|
52
|
+
assert_equal "GENEVE", obj.prefix
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
data/test/test_helper.rb
CHANGED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
require 'test_helper'
|
|
2
|
+
require 'vacuum_cleaner/normalizer'
|
|
3
|
+
require 'vacuum_cleaner/normalizations/numeric'
|
|
4
|
+
|
|
5
|
+
class VacuumCleaner::Normalizations::NumericTtest < Test::Unit::TestCase
|
|
6
|
+
include VacuumCleaner::Normalizations
|
|
7
|
+
|
|
8
|
+
context "NumericNormalizer#normalize_value" do
|
|
9
|
+
should "remove any non numeric character, except decimal character" do
|
|
10
|
+
assert_equal "10.5", NumericNormalizer.new.normalize_value("10.5")
|
|
11
|
+
assert_equal "-12.3434", NumericNormalizer.new.normalize_value("-12.3434")
|
|
12
|
+
assert_equal "121250", NumericNormalizer.new.normalize_value("121'250 Mio. USD")
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
should "accept a negative prefix" do
|
|
16
|
+
assert_equal "-450.00", NumericNormalizer.new.normalize_value("CHF -450.00")
|
|
17
|
+
assert_equal "-.50", NumericNormalizer.new.normalize_value("- .50 SFr.")
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
should "strip single trailing points" do
|
|
21
|
+
assert_equal "1250", NumericNormalizer.new.normalize_value("CHF 1250.--")
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
should "work with German/Swiss notation of comma as decimal separator" do
|
|
25
|
+
assert_equal "40.50", NumericNormalizer.new.normalize_value("CHF 40,50")
|
|
26
|
+
assert_equal "1040.50", NumericNormalizer.new.normalize_value("EUR 1.040,50")
|
|
27
|
+
assert_equal "-1100040.50", NumericNormalizer.new.normalize_value("EUR -1.100.040,50")
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
should "leave numeric and nil values as is" do
|
|
31
|
+
assert_nil NumericNormalizer.new.normalize_value(nil)
|
|
32
|
+
assert_equal 12.5, NumericNormalizer.new.normalize_value(12.5)
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -36,13 +36,16 @@ class VacuumCleaner::NormalizationsTest < Test::Unit::TestCase
|
|
|
36
36
|
context "ClassMethods#normalizes" do
|
|
37
37
|
should "throw ArgumentError if no attributes are passed in" do
|
|
38
38
|
assert_raise ArgumentError do
|
|
39
|
-
klass = Class.new
|
|
40
|
-
|
|
41
|
-
normalizes
|
|
42
|
-
end
|
|
43
|
-
end
|
|
39
|
+
klass = Class.new { include VacuumCleaner::Normalizations; normalizes }
|
|
40
|
+
end
|
|
44
41
|
end
|
|
45
42
|
|
|
43
|
+
should "throw ArgumentError if invalid/unknown normalizer is used" do
|
|
44
|
+
assert_raise ArgumentError do
|
|
45
|
+
klass = Class.new { include VacuumCleaner::Normalizations; normalizes(:name, :foobar_unkown_normalizer => true) }
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
46
49
|
should "take a symbol as argument" do
|
|
47
50
|
assert_respond_to Class.new { include VacuumCleaner::Normalizations; normalizes(:name) }, :normalizes
|
|
48
51
|
end
|
metadata
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: vacuum_cleaner
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
+
hash: 21
|
|
4
5
|
prerelease: false
|
|
5
6
|
segments:
|
|
6
7
|
- 1
|
|
7
8
|
- 0
|
|
8
|
-
-
|
|
9
|
-
version: 1.0.
|
|
9
|
+
- 1
|
|
10
|
+
version: 1.0.1
|
|
10
11
|
platform: ruby
|
|
11
12
|
authors:
|
|
12
13
|
- Lukas Westermann
|
|
@@ -14,16 +15,18 @@ autorequire:
|
|
|
14
15
|
bindir: bin
|
|
15
16
|
cert_chain: []
|
|
16
17
|
|
|
17
|
-
date: 2010-
|
|
18
|
+
date: 2010-09-09 00:00:00 +02:00
|
|
18
19
|
default_executable:
|
|
19
20
|
dependencies:
|
|
20
21
|
- !ruby/object:Gem::Dependency
|
|
21
22
|
name: shoulda
|
|
22
23
|
prerelease: false
|
|
23
24
|
requirement: &id001 !ruby/object:Gem::Requirement
|
|
25
|
+
none: false
|
|
24
26
|
requirements:
|
|
25
27
|
- - ">="
|
|
26
28
|
- !ruby/object:Gem::Version
|
|
29
|
+
hash: 35
|
|
27
30
|
segments:
|
|
28
31
|
- 2
|
|
29
32
|
- 10
|
|
@@ -35,9 +38,11 @@ dependencies:
|
|
|
35
38
|
name: activesupport
|
|
36
39
|
prerelease: false
|
|
37
40
|
requirement: &id002 !ruby/object:Gem::Requirement
|
|
41
|
+
none: false
|
|
38
42
|
requirements:
|
|
39
43
|
- - ">="
|
|
40
44
|
- !ruby/object:Gem::Version
|
|
45
|
+
hash: 9
|
|
41
46
|
segments:
|
|
42
47
|
- 2
|
|
43
48
|
- 3
|
|
@@ -61,13 +66,17 @@ files:
|
|
|
61
66
|
- init.rb
|
|
62
67
|
- lib/vacuum_cleaner.rb
|
|
63
68
|
- lib/vacuum_cleaner/normalizations.rb
|
|
69
|
+
- lib/vacuum_cleaner/normalizations/active_support.rb
|
|
64
70
|
- lib/vacuum_cleaner/normalizations/method.rb
|
|
71
|
+
- lib/vacuum_cleaner/normalizations/numeric.rb
|
|
65
72
|
- lib/vacuum_cleaner/normalizations/url.rb
|
|
66
73
|
- lib/vacuum_cleaner/normalizer.rb
|
|
67
|
-
-
|
|
74
|
+
- lib/vacuum_cleaner/railtie.rb
|
|
68
75
|
- test/integration/active_record_integration_test.rb
|
|
76
|
+
- test/integration/active_support_integration_test.rb
|
|
69
77
|
- test/test_helper.rb
|
|
70
78
|
- test/unit/vacuum_cleaner/normalizations/method_test.rb
|
|
79
|
+
- test/unit/vacuum_cleaner/normalizations/numeric_test.rb
|
|
71
80
|
- test/unit/vacuum_cleaner/normalizations/url_test.rb
|
|
72
81
|
- test/unit/vacuum_cleaner/normalizations_test.rb
|
|
73
82
|
- test/unit/vacuum_cleaner/normalizer_test.rb
|
|
@@ -81,30 +90,36 @@ rdoc_options:
|
|
|
81
90
|
require_paths:
|
|
82
91
|
- lib
|
|
83
92
|
required_ruby_version: !ruby/object:Gem::Requirement
|
|
93
|
+
none: false
|
|
84
94
|
requirements:
|
|
85
95
|
- - ">="
|
|
86
96
|
- !ruby/object:Gem::Version
|
|
97
|
+
hash: 3
|
|
87
98
|
segments:
|
|
88
99
|
- 0
|
|
89
100
|
version: "0"
|
|
90
101
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
102
|
+
none: false
|
|
91
103
|
requirements:
|
|
92
104
|
- - ">="
|
|
93
105
|
- !ruby/object:Gem::Version
|
|
106
|
+
hash: 3
|
|
94
107
|
segments:
|
|
95
108
|
- 0
|
|
96
109
|
version: "0"
|
|
97
110
|
requirements: []
|
|
98
111
|
|
|
99
112
|
rubyforge_project:
|
|
100
|
-
rubygems_version: 1.3.
|
|
113
|
+
rubygems_version: 1.3.7
|
|
101
114
|
signing_key:
|
|
102
115
|
specification_version: 3
|
|
103
116
|
summary: Ruby (and Rails) attribute cleaning support, provides some nice default normalization strategies.
|
|
104
117
|
test_files:
|
|
105
118
|
- test/integration/active_record_integration_test.rb
|
|
119
|
+
- test/integration/active_support_integration_test.rb
|
|
106
120
|
- test/test_helper.rb
|
|
107
121
|
- test/unit/vacuum_cleaner/normalizations/method_test.rb
|
|
122
|
+
- test/unit/vacuum_cleaner/normalizations/numeric_test.rb
|
|
108
123
|
- test/unit/vacuum_cleaner/normalizations/url_test.rb
|
|
109
124
|
- test/unit/vacuum_cleaner/normalizations_test.rb
|
|
110
125
|
- test/unit/vacuum_cleaner/normalizer_test.rb
|
data/rails/init.rb
DELETED