Skip to content
This repository


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

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

tag: v0.2.0

Fetching latest commit…

Cannot retrieve the latest commit at this time


Attribute Normalizer

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

Recent Changes

  • 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 are no longer get ‘strip’ called on them before getting passed on to your defined normalization blocks
  • 0.1.2
    • Changed post-normalization attribute assignment to call super to avoid preventing plugins/gems/hooks to work on the same attribute.

Supported ORMs

  • Active Record
  • CassandraObjects

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.

# your_initializer.rb
require 'attribute_normalizer'
YourORM::Base.send :include, AttributeNormalizer


sudo gem install attribute_normalizer

Then just required it. Rails usages is as follows.

# config/environment.rb
  gem.config 'attribute_normalizer'

It also still works as a traditional Rails plugin.

./script/plugin install git://


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

# spec/models/book_spec.rb
describe Book do
  it { should normalize_attribute(:author) }
  it { should normalize_attribute(:price).from('$3,450.98').to(3450.98) }
  it { should normalize_attribute(:summary).from('Here is my summary that is a little to long').to('Here is m...') }
  it { should normalize_attribute(:title).from('pick up chicks with magic tricks').to('Pick Up Chicks With Magic Tricks')}

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.

# config/initializers/attribute_normalizer.rb
AttributeNormalizer.configure do |config|

  config.normalizers[:currency] = lambda do |value, options|
    value.is_a?(String) ? value.gsub(/[^0-9\.]+/, '') : value

  config.normalizers[:truncate] = lambda do |text, options|
    if text.is_a?(String)
      options.reverse_merge!(:length => 30, :omission => "...")
      l = options[:length] - options[:omission].mb_chars.length
      chars = text.mb_chars
      (chars.length > options[:length] ? chars[0...l] + options[:omission] : text).to_s


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

class Book < ActiveRecord::Base

  # By default it will strip leading and trailing whitespace
  # and set to nil if blank.
  normalize_attributes :author, :publisher

  # Using one of our predefined normalizers.
  normalize_attribute  :price, :with => :currency

  # Using one of our predefined normalizers with options
  normalize_attribute :summary, :with => :truncate, :length => 12

  # You can also define your normalization block inline.
  normalize_attribute :title do |value|
    value.is_a?(String) ? value.titleize.strip : value


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

summary = 'Here is my summary that is a little to long'
title   = 'pick up chicks with magic tricks'
book    = Book.create!(:author => '', :price => '$3,450.89', :summary => summary, :title => title)  # => nil
book.price   # => 3450.89
book.summary # => 'Here is m...'
book.title   # => 'Pick Up Chicks With Magic Tricks'

Test Helpers

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

# spec/spec_helper.rb
# ...

Spec::Runner.configure do |config|
  # ...
  config.include AttributeNormalizer::RSpecMatcher, :type => :models

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


Original module code and concept was taken from Dan Kubb 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.


Copyright © 2009-2010 Michael Deering See MIT-LICENSE for details.

Something went wrong with that request. Please try again.