Project

normalize

0.0
No commit activity in last 3 years
No release in over 3 years
The attribute normalizer.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

 Project Readme

Normalize

Normalize your attributes with ease.

Install

Add normalize to your Gemfile like this:

gem 'normalize'

Usage

Create a directory app/normalizers in your Rails project. This directory will contain your normalizers. A normalizer is a class that responds to call with the value that has to be normalized and returns the normalized value.

Example URL normalizer:

class UrlNormalizer
  def self.call(input)
    url = input.dup
    url.prepend('http://') unless url =~ %r{^[a-zA-Z]+://}
    
    begin
      uri = URI.parse(url)
      uri.scheme = uri.scheme.downcase
      uri.host   = uri.host.downcase
    rescue
      return input
    end
    
    uri.to_s
  end
end

Now in your ActiveRecord model, simply use the normalize method to normalize input:

class Website < ActiveRecord::Base
  normalize :url, with: UrlNormalizer
end

License

Copyright 2011 Tom-Eric Gerritsen. You may use this work without restrictions, as long as this notice is included. The work is provided "as is" without warranty of any kind, neither express nor implied.