scrubby
文件大小: unknow
源码售价: 5 个金币 积分规则     积分充值
资源说明:Clean up your incoming ActiveRecord model attributes
= scrubby

It's so simple! +scrubby+ makes sure that all your attributes are cleaned up before they make their way into your models.

== Installation

In environment.rb:

  Rails::Initializer.run do |config|
    ...
    config.gem 'scrubby'
    ...
  end

At your application root, run:

  $ sudo rake gems:install

== Example

By default, +scrubby+ will strip extra whitespace from strings, and turn blank strings into nil values:

  class User < ActiveRecord::Base
    scrub :first_name, :middle_name, :last_name
  end


  >> u = User.new(:first_name => " Steve", :middle_name => "  ", :last_name => "Richert ")
  => #

Or you can override the default behavior, just by defining a +scrub+ instance method:

  class User < ActiveRecord::Base
    scrub :first_name, :middle_name, :last_name
    
    def scrub(value)
      value.to_s.upcase
    end
  end


  >> u = User.new(:first_name => "Steve", :last_name => "Richert")
  => #

And defining your own scrubbers inline is super easy:

  class User < ActiveRecord::Base
    scrub(:first_name, :last_name){|v| v.blank? ? nil : v.titleize }
    
    scrub :middle_name do |value|
      initials = value.split(" ").map{|n| n.first.upcase + "." }
      initials.empty? ? nil : initials.join(" ")
    end
  end


  class Admin < User
    scrub(:middle_name){ nil }
  end


  >> u = User.new(:first_name => "stephen", :middle_name => "joel michael", :last_name => "richert")
  => #
  >> a = Admin.new(:first_name => "stephen", :middle_name => "joel michael", :last_name => "richert")
  => #

== How It Works

It's pretty straightforward, and there's nothing all that fancy happening behind the scenes.

Scrubbing happens only when attributes are set. That way, no time is wasted scrubbing every time an attribute is retrieved.

Scrubbing is done at the +write_attribute+ level, without stepping on ActiveRecord's toes by creating a bunch of unnecessary setter methods.

Inheritance is respected, just as you'd expect, down through model subclasses. Scrubbers are overridden in subclasses with no problem.

== Inspiration

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.

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 simplify the back-end of an already awesome interface, and to gear it specifically toward ActiveRecord.

== Contributing

* Fork the project
* Make your feature addition or bug fix
* Add, change or remove corresponding tests
* Commit (please don't change Rakefile or VERSION)
* Send me a pull request

== Copyright

Copyright (c) 2010 Steve Richert. See LICENSE for details.

本源码包内暂不包含可直接显示的源代码文件,请下载源码包。