scrubby 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +1 -1
- data/README.rdoc +86 -12
- data/VERSION +1 -1
- data/lib/scrubby.rb +12 -0
- data/scrubby.gemspec +1 -1
- data/test/test_scrubby.rb +14 -2
- metadata +1 -1
data/LICENSE
CHANGED
data/README.rdoc
CHANGED
@@ -1,17 +1,91 @@
|
|
1
1
|
= scrubby
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
==
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
3
|
+
It's so simple! <tt>scrubby</tt> makes sure that all your attributes are cleaned up before they make their way into your models.
|
4
|
+
|
5
|
+
== Installation
|
6
|
+
|
7
|
+
In <tt>environment.rb</tt>:
|
8
|
+
|
9
|
+
Rails::Initializer.run do |config|
|
10
|
+
...
|
11
|
+
config.gem 'scrubby'
|
12
|
+
...
|
13
|
+
end
|
14
|
+
|
15
|
+
At your application root, run:
|
16
|
+
|
17
|
+
$ sudo rake gems:install
|
18
|
+
|
19
|
+
== Example
|
20
|
+
|
21
|
+
By default, <tt>scrubby</tt> will strip extra whitespace from strings, and turn blank strings into nil values:
|
22
|
+
|
23
|
+
class User < ActiveRecord::Base
|
24
|
+
scrub :first_name, :middle_name, :last_name
|
25
|
+
end
|
26
|
+
|
27
|
+
>> u = User.new(:first_name => " Steve", :middle_name => " ", :last_name => "Richert ")
|
28
|
+
=> #<User first_name: "Steve", middle_name: nil, last_name: "Richert">
|
29
|
+
|
30
|
+
Or you can override the default behavior, just by defining a <tt>scrub</tt> instance method:
|
31
|
+
|
32
|
+
class User < ActiveRecord::Base
|
33
|
+
scrub :first_name, :middle_name, :last_name
|
34
|
+
|
35
|
+
def scrub(value)
|
36
|
+
value.to_s.upcase
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
>> u = User.new(:first_name => "Steve", :last_name => "Richert")
|
41
|
+
=> #<User first_name: "STEVE", middle_name: "", last_name: "RICHERT">
|
42
|
+
|
43
|
+
And defining your own scrubbers inline is super easy:
|
44
|
+
|
45
|
+
class User < ActiveRecord::Base
|
46
|
+
scrub(:first_name, :last_name){|v| v.blank? ? nil : v.titleize }
|
47
|
+
|
48
|
+
scrub(:middle_name) do |value|
|
49
|
+
initials = value.split(" ").map{|n| n.first + "." }
|
50
|
+
initials.empty? ? nil : initials.join(" ")
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
class Admin < User
|
55
|
+
scrub(:middle_name){ nil }
|
56
|
+
end
|
57
|
+
|
58
|
+
>> u = User.new(:first_name => "stephen", :middle_name => "joel michael", :last_name => "richert")
|
59
|
+
=> #<User first_name: "Stephen", middle_name: "J. M.", last_name: "Richert">
|
60
|
+
>> a = Admin.new(:first_name => "stephen", :middle_name => "joel michael", :last_name => "richert")
|
61
|
+
=> #<Admin first_name: "Stephen", middle_name: nil, last_name: "Richert">
|
62
|
+
|
63
|
+
== How It Works
|
64
|
+
|
65
|
+
It's pretty straightforward, and there's nothing all that fancy happening behind the scenes.
|
66
|
+
|
67
|
+
Scrubbing happens only when attributes are set. That way, no time is wasted scrubbing every time an attribute is retrieved.
|
68
|
+
|
69
|
+
For column attributes, the scrubbing is done at the <tt>write_attribute</tt> level, without stepping on ActiveRecord's toes by creating a bunch of unnecessary setter methods.
|
70
|
+
|
71
|
+
<tt>scrubby</tt> also works with virtual attributes, simply by adding an <tt>alias_method_chain</tt> to the corresponding setter method, so the original can still be accessed via <tt>attribute_without_scrub=</tt>.
|
72
|
+
|
73
|
+
Inheritance is respected, just as you'd expect, down through model subclasses. Scrubbers are overridden in subclasses with no problem.
|
74
|
+
|
75
|
+
== Inspiration
|
76
|
+
|
77
|
+
Those of you who are familiar with <tt>attribute_normalizer</tt>[http://github.com/mdeering/attribute_normalizer] will recognize the how <tt>scrubby</tt> looks and feels, and that's no accident! <tt>attribute_normalizer</tt> has a simple, clean and effective interface.
|
78
|
+
|
79
|
+
But <tt>scrubby</tt> was written to be an improvement on what's happening behind the scenes, particularly with regards to inheritance and avoiding unnecessary method definitions. Basically, the idea was to really clean up and simplify the back-end of an already awesome interface.
|
80
|
+
|
81
|
+
== Contributing
|
82
|
+
|
83
|
+
* Fork[http://github.com/laserlemon/scrubby/fork] the project
|
84
|
+
* Make your feature addition or bug fix
|
85
|
+
* Add, change or remove corresponding tests
|
86
|
+
* Commit (please don't change Rakefile or VERSION)
|
87
|
+
* Send me a pull request
|
14
88
|
|
15
89
|
== Copyright
|
16
90
|
|
17
|
-
Copyright (c) 2010
|
91
|
+
Copyright (c) 2010 Steve Richert. See LICENSE for details.
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
data/lib/scrubby.rb
CHANGED
@@ -15,6 +15,18 @@ module Scrubby
|
|
15
15
|
def scrub(*attributes, &block)
|
16
16
|
scrubber = block_given? ? block : instance_method(:scrub)
|
17
17
|
self.scrubbers = attributes.inject({}){|s,a| s.merge!(a.to_s => scrubber) }
|
18
|
+
|
19
|
+
attributes.reject{|a| column_names.include?(a.to_s) }.each do |virtual_attribute|
|
20
|
+
unless instance_methods.include?("#{virtual_attribute}_with_scrub=")
|
21
|
+
define_method "#{virtual_attribute}_with_scrub=" do |value|
|
22
|
+
scrubber = scrubbers[virtual_attribute.to_s]
|
23
|
+
value = scrubber.bind(self).call(value)
|
24
|
+
send("#{virtual_attribute}_without_scrub=", value)
|
25
|
+
end
|
26
|
+
|
27
|
+
alias_method_chain "#{virtual_attribute}=", :scrub
|
28
|
+
end
|
29
|
+
end
|
18
30
|
end
|
19
31
|
end
|
20
32
|
|
data/scrubby.gemspec
CHANGED
data/test/test_scrubby.rb
CHANGED
@@ -83,10 +83,10 @@ class TestScrubby < Test::Unit::TestCase
|
|
83
83
|
@user = User.new
|
84
84
|
end
|
85
85
|
|
86
|
-
should '
|
86
|
+
should 'clean the value' do
|
87
87
|
middle_name = ' Joel Michael '
|
88
88
|
@user.middle_name = middle_name
|
89
|
-
|
89
|
+
assert_not_equal middle_name, @user.middle_name
|
90
90
|
end
|
91
91
|
end
|
92
92
|
|
@@ -117,6 +117,18 @@ class TestScrubby < Test::Unit::TestCase
|
|
117
117
|
end
|
118
118
|
end
|
119
119
|
|
120
|
+
context 'over multiple declarations' do
|
121
|
+
setup do
|
122
|
+
User.scrub(:first_name)
|
123
|
+
User.scrub(:last_name)
|
124
|
+
@user = User.new
|
125
|
+
end
|
126
|
+
|
127
|
+
should 'respect all scrubbers' do
|
128
|
+
assert_same_elements %w(first_name last_name), User.scrubbers.keys
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
120
132
|
context 'an inherited model' do
|
121
133
|
setup do
|
122
134
|
User.scrub(:first_name)
|