Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Easy attribute normalization

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 lib
Octocat-spinner-32 spec
Octocat-spinner-32 .gitignore
Octocat-spinner-32 .rspec
Octocat-spinner-32 .rvmrc
Octocat-spinner-32 Gemfile
Octocat-spinner-32 README.md
Octocat-spinner-32 normalize.gemspec
README.md

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.

Something went wrong with that request. Please try again.