attribute_normalizer

Adds the ability to normalize attributes cleanly with code blocks and predefined normalizers

MIT License

Downloads
4.8M
Stars
476
Committers
25

h1. Attribute Normalizer

!https://secure.travis-ci.org/mdeering/attribute_normalizer.png?branch=master(Build Status)!:http://travis-ci.org/mdeering/attribute_normalizer

p. A little normalization goes a long way in helping maintain data integrity.

h2. Change History

  • 1.1.0 ** Allow the use of default normalizers before and after the evaluation of a given block

  • 1.0.0 ** -DSL Changes- ** Default attributes to normalize ** mongid support

  • 0.3.0 ** Normalizer Chaining ** Built-in common normalizers ** Ability to change the default attribute normalization.

  • 0.2.1 ** ActiveModel Support Built-in ** Fix for :with option getting dropped when normalizing several attributes

  • 0.2.0 ** Removed the normalization on reads. ** Added out of the box support for CassandraObjects ** Included RSpec matcher normalizer_attribute ** Added the ability to define global normalizers ** Strings no longer get 'strip' called on them before getting passed on to your defined normalization blocks

h2. Supported ORMs

  • Active Model
  • Active Record
  • CassandraObjects

p. I will gladly take pull requests to automatically load Attribute Normalizer into your ORM of choice if requested. To test it out on your ORM just include the AttributeNormalizer module after requiring it.

h2. Install

p. Then just required it. Rails usages is as follows.

h3. Rails 2

h3. Rails 3

p. It also still works as a traditional Rails plugin.

h2. Usage

p. Lets create a quick test/spec for what we want to accomplish as far as normalization using the built in RSpec matcher.

p. The following normalizers are already included with the +0.3 version of the gem.

  • :blank Will return nil on empty strings
  • :phone Will strip out all non-digit characters and return nil on empty strings
  • :strip Will strip leading and trailing whitespace.
  • :squish Will strip leading and trailing whitespace and convert any consecutive spaces to one space each

p. And lets predefine some normalizers that we may use in other classes/models or that we don't want to clutter up our class/model's readability with.

The normalize_attributes method is eager loaded into your ORM. normalize_attribute is aliased to normalize_attributes and both can take in a single attribute or an array of attributes.

p. All the specs will pass now. Here is quick look at the behaviour from a console.

h2. Test Helpers

p. If you are running RSpec there is a matcher available for use. Usage can been seen above. Include it as follows.

h3. Rails 2

h3. Rails 3

p. I will gladly take a patch to add a macro to Test::Unit if someone submits it.

h2. Credits

Original module code and concept was taken from "Dan Kubb":http://github.com/dkubb during a project we worked on together. I found that I was consistently using this across all my projects so I wanted to plugin-er-size and gem this up for easy reuse.

h2. Copyright

Copyright (c) 2009-2010 "Michael Deering(Edmonton Ruby on Rails)":http://mdeering.com See MIT-LICENSE for details.