scrubby 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/README.rdoc +17 -13
  2. data/VERSION +1 -1
  3. data/lib/scrubby.rb +60 -1
  4. data/scrubby.gemspec +1 -1
  5. metadata +1 -1
@@ -1,6 +1,6 @@
1
1
  = scrubby
2
2
 
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.
3
+ It's so simple! +scrubby+ makes sure that all your attributes are cleaned up before they make their way into your models.
4
4
 
5
5
  == Installation
6
6
 
@@ -18,25 +18,27 @@ At your application root, run:
18
18
 
19
19
  == Example
20
20
 
21
- By default, <tt>scrubby</tt> will strip extra whitespace from strings, and turn blank strings into nil values:
21
+ By default, +scrubby+ will strip extra whitespace from strings, and turn blank strings into nil values:
22
22
 
23
23
  class User < ActiveRecord::Base
24
24
  scrub :first_name, :middle_name, :last_name
25
25
  end
26
26
 
27
+
27
28
  >> u = User.new(:first_name => " Steve", :middle_name => " ", :last_name => "Richert ")
28
29
  => #<User first_name: "Steve", middle_name: nil, last_name: "Richert">
29
30
 
30
- Or you can override the default behavior, just by defining a <tt>scrub</tt> instance method:
31
+ Or you can override the default behavior, just by defining a +scrub+ instance method:
31
32
 
32
33
  class User < ActiveRecord::Base
33
34
  scrub :first_name, :middle_name, :last_name
34
-
35
+
35
36
  def scrub(value)
36
37
  value.to_s.upcase
37
38
  end
38
39
  end
39
40
 
41
+
40
42
  >> u = User.new(:first_name => "Steve", :last_name => "Richert")
41
43
  => #<User first_name: "STEVE", middle_name: "", last_name: "RICHERT">
42
44
 
@@ -44,17 +46,19 @@ And defining your own scrubbers inline is super easy:
44
46
 
45
47
  class User < ActiveRecord::Base
46
48
  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 + "." }
49
+
50
+ scrub :middle_name do |value|
51
+ initials = value.split(" ").map{|n| n.first.upcase + "." }
50
52
  initials.empty? ? nil : initials.join(" ")
51
- end
53
+ end
52
54
  end
53
55
 
56
+
54
57
  class Admin < User
55
58
  scrub(:middle_name){ nil }
56
59
  end
57
60
 
61
+
58
62
  >> u = User.new(:first_name => "stephen", :middle_name => "joel michael", :last_name => "richert")
59
63
  => #<User first_name: "Stephen", middle_name: "J. M.", last_name: "Richert">
60
64
  >> a = Admin.new(:first_name => "stephen", :middle_name => "joel michael", :last_name => "richert")
@@ -66,21 +70,21 @@ It's pretty straightforward, and there's nothing all that fancy happening behind
66
70
 
67
71
  Scrubbing happens only when attributes are set. That way, no time is wasted scrubbing every time an attribute is retrieved.
68
72
 
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.
73
+ For column attributes, the scrubbing is done at the +write_attribute+ level, without stepping on ActiveRecord's toes by creating a bunch of unnecessary setter methods.
70
74
 
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>.
75
+ +scrubby+ also works with virtual attributes, simply by adding an +alias_method_chain+ to the corresponding setter method, so the original can still be accessed via <tt>attribute_without_scrub=</tt>.
72
76
 
73
77
  Inheritance is respected, just as you'd expect, down through model subclasses. Scrubbers are overridden in subclasses with no problem.
74
78
 
75
79
  == Inspiration
76
80
 
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.
81
+ Those of you who are familiar with +attribute_normalizer+[http://github.com/mdeering/attribute_normalizer] will recognize the how +scrubby+ looks and feels, and that's no accident! +attribute_normalizer+ has a simple, clean and effective interface.
78
82
 
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.
83
+ But +scrubby+ 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
84
 
81
85
  == Contributing
82
86
 
83
- * Fork[http://github.com/laserlemon/scrubby/fork] the project
87
+ * Fork the project
84
88
  * Make your feature addition or bug fix
85
89
  * Add, change or remove corresponding tests
86
90
  * Commit (please don't change Rakefile or VERSION)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.2.1
@@ -1,5 +1,25 @@
1
+ # +scrubby+ cleans up incoming ActiveRecord model attributes by hijacking attribute setters and
2
+ # reformatting the attribute value before passing it along to be set as usual.
3
+ #
4
+ # Author:: Steve Richert
5
+ # Copyright:: Copyright (c) 2010 Steve Richert
6
+ # License:: MIT License (http://www.opensource.org/licenses/mit-license.php)
7
+ #
8
+ # To enable scrubbing on a model, simply use the +scrub+ method:
9
+ #
10
+ # class User < ActiveRecord::Base
11
+ # scrub :name
12
+ # end
13
+ #
14
+ #
15
+ # user = User.new(:name => " Steve Richert ")
16
+ # user.name # => "Steve Richert"
17
+ # noname = User.new(:name => " ")
18
+ # noname.name # => nil
19
+ #
20
+ # See the +scrub+ class method documentation for more details.
1
21
  module Scrubby
2
- def self.included(base)
22
+ def self.included(base) #:nodoc:
3
23
  base.class_eval do
4
24
  class_inheritable_hash :scrubbers
5
25
  self.scrubbers = {}
@@ -12,6 +32,34 @@ module Scrubby
12
32
  end
13
33
 
14
34
  module ClassMethods
35
+ # +scrub+ accepts multiple symbol or string arguments, representing model attributes that are
36
+ # to be scrubbed before being set. The +scrub+ method accepts no options, but an optional block
37
+ # can be included which will be used for scrubbing. Otherwise, the +scrub+ instance method is
38
+ # used.
39
+ #
40
+ # The +scrub+ instance method and the optional block both expect one argument: the incoming
41
+ # attribute value, and should return one value: the new, all-cleaned-up attribute value.
42
+ # However, when a custom block is given, the single argument can be left out if for some
43
+ # reason it's not needed.
44
+ #
45
+ # == Examples
46
+ #
47
+ # class User < ActiveRecord::Base
48
+ # scrub :first_name, :last_name
49
+ # end
50
+ #
51
+ #
52
+ # class User < ActiveRecord::Base
53
+ # scrub :first_name, :last_name do |value|
54
+ # value.blank? ? nil : value.titleize
55
+ # end
56
+ # end
57
+ #
58
+ #
59
+ # class User < ActiveRecord::Base
60
+ # scrub(:first_name){|v| v.to_s.upcase }
61
+ # scrub(:last_name){ nil }
62
+ # end
15
63
  def scrub(*attributes, &block)
16
64
  scrubber = block_given? ? block : instance_method(:scrub)
17
65
  self.scrubbers = attributes.inject({}){|s,a| s.merge!(a.to_s => scrubber) }
@@ -31,6 +79,12 @@ module Scrubby
31
79
  end
32
80
 
33
81
  module InstanceMethods
82
+ # An alias of the +write_attribute+ method, +write_attribute_with_scrub+ will check whether a
83
+ # scrubber exists for the given attribute and if so, scrub the given value before passing it
84
+ # on to the original +write_attribute+ method.
85
+ #
86
+ # This is used for column-based attributes, while virtual attributes are handled by aliasing
87
+ # the corresponding setter methods and scrubbing there.
34
88
  def write_attribute_with_scrub(attribute, value)
35
89
  if scrubber = scrubbers[attribute.to_s]
36
90
  value = scrubber.bind(self).call(value)
@@ -39,6 +93,11 @@ module Scrubby
39
93
  write_attribute_without_scrub(attribute, value)
40
94
  end
41
95
 
96
+ # The +scrub+ instance method is the default way in which incoming values are cleaned. By
97
+ # default, the behavior is to strip string values and to return nil for blank strings.
98
+ #
99
+ # If a different behavoir is required, this method can be overridden at the ActiveRecord::Base
100
+ # level and/or at the level of any of ActiveRecord's inherited models.
42
101
  def scrub(value)
43
102
  value = value.dup if value.duplicable?
44
103
 
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{scrubby}
8
- s.version = "0.2.0"
8
+ s.version = "0.2.1"
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"]
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.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steve Richert