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 CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2009 laserlemon
1
+ Copyright (c) 2009 Steve Richert
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.rdoc CHANGED
@@ -1,17 +1,91 @@
1
1
  = scrubby
2
2
 
3
- Description goes here.
4
-
5
- == Note on Patches/Pull Requests
6
-
7
- * Fork the project.
8
- * Make your feature addition or bug fix.
9
- * Add tests for it. This is important so I don't break it in a
10
- future version unintentionally.
11
- * Commit, do not mess with rakefile, version, or history.
12
- (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
- * Send me a pull request. Bonus points for topic branches.
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 laserlemon. See LICENSE for details.
91
+ Copyright (c) 2010 Steve Richert. See LICENSE for details.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
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
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{scrubby}
8
- s.version = "0.1.1"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Steve Richert"]
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 'do nothing' do
86
+ should 'clean the value' do
87
87
  middle_name = ' Joel Michael '
88
88
  @user.middle_name = middle_name
89
- assert_equal middle_name, @user.middle_name
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)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scrubby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steve Richert