README.md in tiny_sweeper-0.0.5 vs README.md in tiny_sweeper-1.0.0
- old
+ new
@@ -1,75 +1,120 @@
# TinySweeper
TinySweeper keeps your objects tidy!
+![Hold me closer, Tiny Sweeper](https://github.com/ContinuityControl/tiny_sweeper/raw/master/tiny-sweeper.png)
+
It's a handy way to clean attributes on your Rails models, though it's independent of Rails, and can be used in any Ruby project. It gives you a light-weigt way to override your methods and declare how their inputs should be cleaned.
[![Build Status](https://travis-ci.org/ContinuityControl/tiny_sweeper.png?branch=master)](https://travis-ci.org/ContinuityControl/tiny_sweeper)
[![Code Climate](https://codeclimate.com/github/ContinuityControl/tiny_sweeper/badges/gpa.svg)](https://codeclimate.com/github/ContinuityControl/tiny_sweeper)
+[![Test Coverage](https://codeclimate.com/github/ContinuityControl/tiny_sweeper/badges/coverage.svg)](https://codeclimate.com/github/ContinuityControl/tiny_sweeper/coverage)
## How Do I Use It?
```ruby
class Sundae
- attr_accessor :ice_cream, :topping
+ attr_accessor :ice_cream
include TinySweeper
- sweep(:ice_cream, :topping) { |flavor| flavor.strip.downcase }
+ sweep(:ice_cream) { |flavor| flavor.strip.downcase }
end
```
Now your Sundae toppings will be tidied up:
```ruby
dessert = Sundae.new
dessert.ice_cream = ' CHOCOlate '
-dessert.topping = ' ButTTERscotCH '
dessert.ice_cream #=> 'chocolate'. Tidy!
-dessert.topping #=> 'butterscotch'. Tidy!
```
TinySweeper will not bother you about your nil values; they're your job to handle.
```ruby
Sundae.new.topping = nil # No topping? TinySweeper won't sweep it.
```
-If you have an object with lots of attributes that need cleaning (because, say, they were loaded from the database), you can do that, too:
+If lots of attributes need to be swept the same way, you can pass an array of field names:
```ruby
-dessert.sweep_up!
-# or:
-Sundae.sweep_up!(dessert)
-```
+class Sundae
+ attr_accessor :ice_cream, :topping, :nuts
-### Future Ideas
+ include TinySweeper
+ sweep [:ice_cream, :topping, :nuts] { |item| item.strip.downcase }
+end
-Just spit-balling here...
+dessert = Sundae.new
+dessert.ice_cream = ' CHOCOlate '
+dessert.topping = ' ButTTERscotCH '
+dessert.nuts = ' CRUSHED peaNUtS '
+dessert.ice_cream #=> 'chocolate'
+dessert.topping #=> 'butterscotch'
+dessert.nuts #=> 'crushed peanuts'
+```
-If you often sweep up fields in the same way - say, squishing and nilifying blanks - it'd be nice to bundle that up in some way, so you don't have to repeat yourself. Something like this might be nice:
+TinySweeper already knows a few sweeping tricks, and you can ask for them by name:
```ruby
-# in config/initializers/tiny_sweeper.rb, or similar:
-TinySweeper.sweep_style(:squish_and_nil_blanks) { |value|
- ...
-}
+class Sundae
+ attr_accessor :ice_cream
+ include TinySweeper
+ sweep :ice_cream, :blanks_to_nil
+end
+
+dessert = Sundae.new
+dessert.ice_cream = ""
+dessert.ice_cream #=> nil
+```
+
+You can use as many as you need, and TinySweeper will apply them all, left-to-right:
+
+```ruby
class Sundae
- sweep :topping, :squish_and_nil_blanks
+ attr_accessor :ice_cream
+
+ include TinySweeper
+ sweep :ice_cream, :strip, :blanks_to_nil
+
+dessert = Sundae.new
+dessert.ice_cream = " "
+dessert.ice_cream #=> nil
end
```
-If TinySweeper doesn't know the sweeping technique you asked for, it would send it to the value in the typical symbol-to-proc fashion:
+TinySweeper currently only knows a few tricks...
+* `blanks_to_nil`: turn empty strings into nils
+* `strip`: just like `String#strip`: removes trailing and leading whitespace
+* `dumb_quotes`: replace [Smart Quotes](https://en.wikipedia.org/wiki/Quotation_marks_in_English) with their simpler siblings
+
+...but you can teach it new ones:
+
```ruby
+TinySweeper::Brooms.add(:strip_html) { |value| Nokogiri::HTML(value).text }
+```
+
+And you can always combine the built-in tricks with a block:
+
+```ruby
class Sundae
- # This:
- sweep :topping, :strip
- # ...would be the same as this:
- sweep :topping { |t| t.strip }
+ ...
+ sweep(:topping, :strip, :dumb_quotes) { |topping| topping.downcase }
end
```
+
+If you have an object with lots of attributes that need cleaning (because, say, they were loaded from the database), you can do that, too:
+
+```ruby
+dessert.sweep_up!
+# or:
+Sundae.sweep_up!(dessert)
+```
+
+### Future Ideas
#### Other Ways to Sweep
Rails models are clearly the natural use-case for this. So it would make sense to have an easy way to auto-clean up models in a table. We'll see. Right now, this works (though it's slow):