/attribute_normalizer

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

Primary LanguageRubyMIT LicenseMIT

Attribute Normalizer

Build Status

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

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

Supported ORMs

  • Active Model
  • 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

Install

sudo gem install attribute_normalizer

Then just required it. Rails usages is as follows.

Rails 2

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

Rails 3

# Gemfile
gem 'attribute_normalizer'

It also still works as a traditional Rails plugin.

./script/plugin install git://github.com/mdeering/attribute_normalizer.git

Usage

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') }
  it { should normalize_attribute(:slug).from(' Social Life at the Edge of Chaos    ').to('social-life-at-the-edge-of-chaos') }
  it { should normalize_attribute(:limited_slug).from(' Social Life at the Edge of Chaos    ').to('social-life') }
end

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

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
  end

  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
    else
      text
    end
  end

  # The default normalizers if no :with option or block is given is to apply the :strip and :blank normalizers (in that order).
  # You can change this if you would like as follows:
  # config.default_normalizers = :strip, :blank

end

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.

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 more then one of our predefined normalizers including one with options
  normalize_attribute :summary, :with => [ :strip, { :truncate => { :length => 12 } } ]

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

  # Or use a combination of normalizers plus an inline block.
  # the normalizers in the :with option will each be evalulated
  # in order and the result will be given to the block.
  # You could also use option :before in place of :with
  normalize_attribute :slug, :with => [ :strip, :blank ] do |value|
    value.present? && value.is_a?(String) ? value.downcase.gsub(/\s+/, '-') : value
  end

  # Use builtin normalizers before and after the evaluation of your inline
  # block
  normalize_attribute :limited_slug, :before => [ :strip, :blank ], :after => [ { :truncate => { :length => 11, :omission => '' } } ] do |value|
    value.present? && value.is_a?(String) ? value.downcase.gsub(/\s+/, '-') : value
  end

end

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, :slug => title, :limited_slug => title)
book.author       # => nil
book.price        # => 3450.89
book.summary      # => 'Here is m...'
book.title        # => 'Pick Up Chicks With Magic Tricks'
book.slug         # => 'pick-up-chicks-with-magic-tricks'
book.limited_slug # => 'pick-up-chi'

Test Helpers

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

Rails 2

# spec/spec_helper.rb
RSpec.configure do |config|
  config.include AttributeNormalizer::RSpecMatcher, :type => :models
end

Rails 3

# spec/spec_helper.rb
RSpec.configure do |config|
  config.include AttributeNormalizer::RSpecMatcher, :type => :model
end

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

Credits

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

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