acts_as_filterable 0.1.4
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/.document +5 -0
- data/.gitignore +6 -0
- data/LICENSE +20 -0
- data/README.rdoc +24 -0
- data/Rakefile +60 -0
- data/VERSION.yml +4 -0
- data/acts_as_filterable.gemspec +62 -0
- data/init.rb +1 -0
- data/lib/acts_as_filterable/base.rb +69 -0
- data/lib/acts_as_filterable.rb +8 -0
- data/rails/init.rb +1 -0
- data/test/acts_as_filterable_integration_test.rb +122 -0
- data/test/test_helper.rb +34 -0
- metadata +108 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Rob Ares
|
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,24 @@
|
|
1
|
+
= acts_as_filterable
|
2
|
+
|
3
|
+
== Description
|
4
|
+
|
5
|
+
acts_as_filterable is a small ActiveRecord plugin that was thrown together
|
6
|
+
to avoid duplicating the same text filtering logic all over the place in a domain model.
|
7
|
+
|
8
|
+
The only filter in place so far is one that will strip everything out of a string except the digit information. You might ask, "Why don't you just change the column type to a numeric one?". That _is_ the right way to go but we were faced with tens of millions of records and a lot of text processing (with a lot of bad data that would be hard to apply a deterministic ruleset to). So putting this in place will avoid the garbage data coming in moving forward.
|
9
|
+
|
10
|
+
I'd like to expand the ruleset moving forward to support different schemes like decimals and so on.
|
11
|
+
|
12
|
+
== Install as a gem:
|
13
|
+
|
14
|
+
config.gem "rares-acts_as_filterable", :source => "http://gems.github.com"
|
15
|
+
|
16
|
+
== To apply to fields on a model, add the following inside the class body:
|
17
|
+
|
18
|
+
filter_for_digits :phone_number, :fax_number
|
19
|
+
|
20
|
+
if something is broken or it is missing a feature; you know the deal. Don't be part of the problem.
|
21
|
+
|
22
|
+
== Copyright
|
23
|
+
|
24
|
+
Copyright (c) 2009 Rob Ares. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require "rake"
|
3
|
+
|
4
|
+
begin
|
5
|
+
require "jeweler"
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "acts_as_filterable"
|
8
|
+
gem.summary = %Q{TODO}
|
9
|
+
gem.email = "rob.ares@gmail.com"
|
10
|
+
gem.homepage = "http://github.com/rares/acts_as_filterable"
|
11
|
+
gem.authors = ["Rob Ares"]
|
12
|
+
|
13
|
+
gem.add_dependency("activerecord", ">= 1.15.0")
|
14
|
+
gem.add_runtime_dependency("activesupport", ">= 1.4.4")
|
15
|
+
gem.add_development_dependency("Shoulda")
|
16
|
+
gem.add_development_dependency("matchy")
|
17
|
+
end
|
18
|
+
rescue LoadError
|
19
|
+
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
20
|
+
end
|
21
|
+
|
22
|
+
require "rake/testtask"
|
23
|
+
Rake::TestTask.new(:test) do |test|
|
24
|
+
test.libs << "lib" << "test"
|
25
|
+
test.pattern = "test/**/*_test.rb"
|
26
|
+
test.verbose = false
|
27
|
+
test.options = "-v"
|
28
|
+
end
|
29
|
+
|
30
|
+
begin
|
31
|
+
require "rcov/rcovtask"
|
32
|
+
Rcov::RcovTask.new do |test|
|
33
|
+
test.libs << "test"
|
34
|
+
test.pattern = "test/**/*_test.rb"
|
35
|
+
test.verbose = true
|
36
|
+
end
|
37
|
+
rescue LoadError
|
38
|
+
task :rcov do
|
39
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
|
44
|
+
task :default => :test
|
45
|
+
|
46
|
+
require "rake/rdoctask"
|
47
|
+
Rake::RDocTask.new do |rdoc|
|
48
|
+
if File.exist?("VERSION.yml")
|
49
|
+
config = YAML.load(File.read("VERSION.yml"))
|
50
|
+
version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
|
51
|
+
else
|
52
|
+
version = ""
|
53
|
+
end
|
54
|
+
|
55
|
+
rdoc.rdoc_dir = "rdoc"
|
56
|
+
rdoc.title = "acts_as_filterable #{version}"
|
57
|
+
rdoc.rdoc_files.include("README*")
|
58
|
+
rdoc.rdoc_files.include("lib/**/*.rb")
|
59
|
+
end
|
60
|
+
|
data/VERSION.yml
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{acts_as_filterable}
|
5
|
+
s.version = "0.1.4"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Rob Ares"]
|
9
|
+
s.date = %q{2009-09-08}
|
10
|
+
s.email = %q{rob.ares@gmail.com}
|
11
|
+
s.extra_rdoc_files = [
|
12
|
+
"LICENSE",
|
13
|
+
"README.rdoc"
|
14
|
+
]
|
15
|
+
s.files = [
|
16
|
+
".document",
|
17
|
+
".gitignore",
|
18
|
+
"LICENSE",
|
19
|
+
"README.rdoc",
|
20
|
+
"Rakefile",
|
21
|
+
"VERSION.yml",
|
22
|
+
"acts_as_filterable.gemspec",
|
23
|
+
"init.rb",
|
24
|
+
"lib/acts_as_filterable.rb",
|
25
|
+
"lib/acts_as_filterable/base.rb",
|
26
|
+
"rails/init.rb",
|
27
|
+
"test/acts_as_filterable_integration_test.rb",
|
28
|
+
"test/test_helper.rb"
|
29
|
+
]
|
30
|
+
s.has_rdoc = true
|
31
|
+
s.homepage = %q{http://github.com/rares/acts_as_filterable}
|
32
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
33
|
+
s.require_paths = ["lib"]
|
34
|
+
s.rubygems_version = %q{1.3.2}
|
35
|
+
s.summary = %q{An ActiveRecord plugin that allows attribute-based filtering in order to normalize numeric data}
|
36
|
+
s.test_files = [
|
37
|
+
"test/acts_as_filterable_integration_test.rb",
|
38
|
+
"test/test_helper.rb"
|
39
|
+
]
|
40
|
+
|
41
|
+
if s.respond_to? :specification_version then
|
42
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
43
|
+
s.specification_version = 3
|
44
|
+
|
45
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
46
|
+
s.add_runtime_dependency(%q<activerecord>, [">= 1.15.0"])
|
47
|
+
s.add_runtime_dependency(%q<activesupport>, [">= 1.4.4"])
|
48
|
+
s.add_development_dependency(%q<Shoulda>, [">= 0"])
|
49
|
+
s.add_development_dependency(%q<matchy>, [">= 0"])
|
50
|
+
else
|
51
|
+
s.add_dependency(%q<activerecord>, [">= 1.15.0"])
|
52
|
+
s.add_dependency(%q<activesupport>, [">= 1.4.4"])
|
53
|
+
s.add_dependency(%q<Shoulda>, [">= 0"])
|
54
|
+
s.add_dependency(%q<matchy>, [">= 0"])
|
55
|
+
end
|
56
|
+
else
|
57
|
+
s.add_dependency(%q<activerecord>, [">= 1.15.0"])
|
58
|
+
s.add_dependency(%q<activesupport>, [">= 1.4.4"])
|
59
|
+
s.add_dependency(%q<Shoulda>, [">= 0"])
|
60
|
+
s.add_dependency(%q<matchy>, [">= 0"])
|
61
|
+
end
|
62
|
+
end
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/rails/init"
|
@@ -0,0 +1,69 @@
|
|
1
|
+
module ActsAsFilterable
|
2
|
+
|
3
|
+
module ActiveRecordExt
|
4
|
+
|
5
|
+
module Base
|
6
|
+
|
7
|
+
def self.included(klazz)
|
8
|
+
klazz.extend ClassMethods
|
9
|
+
klazz.before_validation :apply_filters
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
module Language
|
15
|
+
|
16
|
+
def filter(&blk)
|
17
|
+
instance_eval blk
|
18
|
+
end
|
19
|
+
|
20
|
+
def digits(*args)
|
21
|
+
filtered_attributes[:digits] |= args unless args.empty?
|
22
|
+
end
|
23
|
+
|
24
|
+
def lowercase(*args)
|
25
|
+
filtered_attributes[:lowercase] |= args unless args.empty?
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
module ClassMethods
|
31
|
+
|
32
|
+
def filter_for_digits(*args)
|
33
|
+
filtered_attributes[:digits] |= args unless args.empty?
|
34
|
+
end
|
35
|
+
|
36
|
+
def filters
|
37
|
+
@filters ||= returning(Hash.new([])) do |f|
|
38
|
+
f[:digits] = Commands::Digits.new
|
39
|
+
f[:lowercase] = Commands::Lowercase.new
|
40
|
+
end.freeze
|
41
|
+
end
|
42
|
+
|
43
|
+
def filtered_attributes
|
44
|
+
@filtered_attributes ||= Hash.new []
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
protected
|
50
|
+
|
51
|
+
def apply_filters
|
52
|
+
self.class.filtered_attributes.each do |key, value|
|
53
|
+
value.each do |attr|
|
54
|
+
apply_filter self.class.filters[key], attr
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
private
|
60
|
+
|
61
|
+
def apply_filter(filter, attr)
|
62
|
+
filter.apply self, attr
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
data/rails/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "acts_as_filterable"
|
@@ -0,0 +1,122 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class ActsAsFilterableIntegrationTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
context "An ActiveRecord model using acts_as_filterable" do
|
6
|
+
setup do
|
7
|
+
@model = ContactDetail.new do |cd|
|
8
|
+
cd.name = "joe smith"
|
9
|
+
cd.phone_number = "2223334444"
|
10
|
+
cd.discount = 0.25
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
should "add an #apply_filters instance method" do
|
15
|
+
@model.send(:apply_filters).nil?.should_not be(true)
|
16
|
+
end
|
17
|
+
|
18
|
+
should "know about the types of filters that will be applied to the attributes" do
|
19
|
+
ContactDetail.respond_to?(:filtered_attributes).should be(true)
|
20
|
+
end
|
21
|
+
|
22
|
+
should "make it's filters available" do
|
23
|
+
ContactDetail.respond_to?(:filters).should be(true)
|
24
|
+
end
|
25
|
+
|
26
|
+
should "default filters that don't exist to an empty array" do
|
27
|
+
ContactDetail.filters[:test].empty?.should be(true)
|
28
|
+
end
|
29
|
+
|
30
|
+
should "contain some filters initially" do
|
31
|
+
ContactDetail.filters[:numeric].nil?.should_not be(true)
|
32
|
+
end
|
33
|
+
|
34
|
+
should "freeze the macro collection so it cannot be mutated" do
|
35
|
+
lambda { ContactDetail.filters.store(:test, /./) }.should raise_error
|
36
|
+
end
|
37
|
+
|
38
|
+
should "add a macro to filter non-numeric values from string fields" do
|
39
|
+
ContactDetail.respond_to?(:filter_for_digits).should be(true)
|
40
|
+
end
|
41
|
+
|
42
|
+
should "be savable with valid data" do
|
43
|
+
@model.save.should be(true)
|
44
|
+
end
|
45
|
+
|
46
|
+
context "with formatted phone number data" do
|
47
|
+
setup do
|
48
|
+
@model.phone_number = "(222) 333-4444"
|
49
|
+
@model.valid?
|
50
|
+
end
|
51
|
+
|
52
|
+
should "strip all formatting" do
|
53
|
+
@model.phone_number.should be("2223334444")
|
54
|
+
end
|
55
|
+
|
56
|
+
should "return a coercable numeric value" do
|
57
|
+
@model.phone_number.to_f.should be(2223334444)
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
|
62
|
+
context "with a nil attribute value" do
|
63
|
+
setup do
|
64
|
+
@model.phone_number = nil
|
65
|
+
end
|
66
|
+
|
67
|
+
should "not raise any errors due to a nil attribute value" do
|
68
|
+
lambda { @model.valid? }.should_not raise_error
|
69
|
+
end
|
70
|
+
|
71
|
+
should "not attempt to change the attribute value" do
|
72
|
+
@model.valid?
|
73
|
+
@model.phone_number.nil?.should be(true)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
context "with non-character attributes" do
|
78
|
+
setup do
|
79
|
+
ContactDetail.filter_for_digits :discount
|
80
|
+
end
|
81
|
+
|
82
|
+
should "not raise any errors due to a non-character attribute value" do
|
83
|
+
lambda { @model.valid? }.should_not raise_error
|
84
|
+
end
|
85
|
+
|
86
|
+
should "not attempt to change the attribute value" do
|
87
|
+
@model.valid?
|
88
|
+
@model.discount.should be(0.25)
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
92
|
+
|
93
|
+
context "with an attribute value that contains no non-numeric values to be stripped" do
|
94
|
+
setup do
|
95
|
+
@model.phone_number = "2223334444"
|
96
|
+
@model.valid?
|
97
|
+
end
|
98
|
+
|
99
|
+
should "not change the attribute value" do
|
100
|
+
@model.phone_number.should be("2223334444")
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
context "that has filtered attribute names that are identical to another filtered model" do
|
105
|
+
|
106
|
+
should "hold seperate collections of filtered_attributes" do
|
107
|
+
User.filtered_attributes.should_not == ContactDetail.filtered_attributes
|
108
|
+
end
|
109
|
+
|
110
|
+
should "not overwrite attributes for other models" do
|
111
|
+
ContactDetail.filtered_attributes.include?(:fax_number).should_not be(nil)
|
112
|
+
end
|
113
|
+
|
114
|
+
should "not add filtered attributes to models that they are not intended for" do
|
115
|
+
User.filtered_attributes.include?(:phone_number).should_not be(true)
|
116
|
+
end
|
117
|
+
|
118
|
+
end
|
119
|
+
|
120
|
+
end
|
121
|
+
|
122
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
require "test/unit"
|
2
|
+
require "activerecord"
|
3
|
+
require "shoulda"
|
4
|
+
require "matchy"
|
5
|
+
|
6
|
+
require "acts_as_filterable"
|
7
|
+
|
8
|
+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
9
|
+
ActiveRecord::Migration.verbose = false
|
10
|
+
|
11
|
+
ActiveRecord::Schema.define do
|
12
|
+
create_table :contact_details, :force => true do |t|
|
13
|
+
t.string :name
|
14
|
+
t.string :phone_number
|
15
|
+
t.string :fax_number
|
16
|
+
t.float :discount
|
17
|
+
end
|
18
|
+
|
19
|
+
create_table :user do |t|
|
20
|
+
t.string :handle
|
21
|
+
t.string :phone_number
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class ContactDetail < ActiveRecord::Base
|
26
|
+
filter_for_digits :phone_number, :fax_number
|
27
|
+
end
|
28
|
+
|
29
|
+
class User < ActiveRecord::Base
|
30
|
+
filter_for_digits :phone_number
|
31
|
+
end
|
32
|
+
|
33
|
+
class Test::Unit::TestCase
|
34
|
+
end
|
metadata
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: acts_as_filterable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.4
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Rob Ares
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-09-08 00:00:00 -04:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: activerecord
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.15.0
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: activesupport
|
27
|
+
type: :runtime
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.4.4
|
34
|
+
version:
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: Shoulda
|
37
|
+
type: :development
|
38
|
+
version_requirement:
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: "0"
|
44
|
+
version:
|
45
|
+
- !ruby/object:Gem::Dependency
|
46
|
+
name: matchy
|
47
|
+
type: :development
|
48
|
+
version_requirement:
|
49
|
+
version_requirements: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: "0"
|
54
|
+
version:
|
55
|
+
description:
|
56
|
+
email: rob.ares@gmail.com
|
57
|
+
executables: []
|
58
|
+
|
59
|
+
extensions: []
|
60
|
+
|
61
|
+
extra_rdoc_files:
|
62
|
+
- LICENSE
|
63
|
+
- README.rdoc
|
64
|
+
files:
|
65
|
+
- .document
|
66
|
+
- .gitignore
|
67
|
+
- LICENSE
|
68
|
+
- README.rdoc
|
69
|
+
- Rakefile
|
70
|
+
- VERSION.yml
|
71
|
+
- acts_as_filterable.gemspec
|
72
|
+
- init.rb
|
73
|
+
- lib/acts_as_filterable.rb
|
74
|
+
- lib/acts_as_filterable/base.rb
|
75
|
+
- rails/init.rb
|
76
|
+
- test/acts_as_filterable_integration_test.rb
|
77
|
+
- test/test_helper.rb
|
78
|
+
has_rdoc: true
|
79
|
+
homepage: http://github.com/rares/acts_as_filterable
|
80
|
+
licenses: []
|
81
|
+
|
82
|
+
post_install_message:
|
83
|
+
rdoc_options:
|
84
|
+
- --charset=UTF-8
|
85
|
+
require_paths:
|
86
|
+
- lib
|
87
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: "0"
|
92
|
+
version:
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: "0"
|
98
|
+
version:
|
99
|
+
requirements: []
|
100
|
+
|
101
|
+
rubyforge_project:
|
102
|
+
rubygems_version: 1.3.4
|
103
|
+
signing_key:
|
104
|
+
specification_version: 3
|
105
|
+
summary: An ActiveRecord plugin that allows attribute-based filtering in order to normalize numeric data
|
106
|
+
test_files:
|
107
|
+
- test/acts_as_filterable_integration_test.rb
|
108
|
+
- test/test_helper.rb
|