Attribute Normalizer: Now also in ruby gem flavor

Posted on JANUARY 2ND, 2010
sudo gem install attribute_normalizer

The Attribute Normalizer rails plugin has been packaged up into a ruby gem hosted over at Gemcutter.

This release (0.1.1) also includes bug, feature, and documenation fixes.

I am looking for feedback on what I have planned for the 0.2 release of the gem/plugin.

Please take a look at the roadmap over on github and email me or drop a comment here with your feedback.

Cheers,
Mike D.

Tags

ActiveRecord Attribute Normalization (Rails Plugin)

Posted on SEPTEMBER 15TH, 2009

I like to keep my Active Record models as strict as possible. There are few things more enoying to me then having to deal with bad legacy data when a simple change to a database column to not allow NULL values or adding validation in a model to disalow ’’ could have saved hours of data massaging.

Enter the attribute_normalizer rails plugin

class Klass < ActiveRecord::Base
  
  # Can take an array of attributes if you want
  normalize_attributes :first_name, :last_name
  
end
object = Klass.new
# Blank normalizes to nil
object.first_name = ''
object.first_name # => nil

# Whitespace is also cleaned up 
object.last_name = "\tDeering\n"
object.last_name # => 'Deering'

And for more bonus points then I can throw at it also accepts blocks!

class Klass < ActiveRecord::Base
  
  normalize_attributes :home_phone_number, :office_phone_number_ do |value|
    return nil unless value.is_a?(String)
    value.gsub(/\W/, '').gsub(/^1/, '')
  end

  normalize_attributes :postal_code do |value|
    value.upcase.gsub(/\W/,'')
  end
  
end
object = Klass.new
# Your given block will be executed to normalize
object.home_phone_number = '+1 (555) 123.4567'
object.home_phone_number # => '5551234567'

# Normalization happens before any validations.
# This will surely help avoid a lot of regex within validates_format_of
object.postal_code = 't6m 2x2'
object.postal_code # => 'T6M2X2'

This has become standard on every one of my rails projects. I hope you can find a use for it also. I have some backend Ruby work coming up so I should get around to making this a gem soon.

Cheers,
Mike D.

I’m on Twitter

Tags

Available For Hire

Contact Me

Michael Deering http://mdeeirng.com Cell: 780.906.6632

Testimonials

" Michael understands what is means in business to abide by timelines and budget. His great work ethic and agile perspective allow him to motivate and manage himself, and his team, seemingly without effort. He has a great attitude and enjoys sinking his teeth into projects that leverage and push his skill set to the limit. "
Erik Lagerway / Co-Founder & CEO / Gaboogie

" Michael's attention to detail and intense interest in continuously improving the software development process made working with him a very enjoyable experience. Michael was a great manager to work with during my time at Lypp. In addition he was an excellent software developer that set a high standard for others to meet. I would definitely work with Michael again in the future. "
Dan Kubb / Ruby Hero

" Mike is a very sharp technologist with an eye for simplicity. He is able to break down complex concepts into elegant solutions and communicate them effectively with a remote team. Mike's leadership is apparent by his ability to manage scope, keep deadlines and translate evolving business requirements into flexible technical solutions. "
Jason Cartwright / CTO / Cogwise Software

Post Categories