~srushe/validates_as_uk_postcode

f4d3d05a44a98a58ccac7db2b416e6461aa448cb — Stephen Rushe 14 years ago master
Update with the latest readme
6 files changed, 212 insertions(+), 0 deletions(-)

A CHANGELOG
A README
A Rakefile
A init.rb
A lib/validates_as_uk_postcode.rb
A test/validates_as_uk_postcode_test.rb
A  => CHANGELOG +9 -0
@@ 1,9 @@
1.0 2009-03-17

  * Updated to Creative Commons Attribution-Share Alike 3.0 Unported License.
  * Fixed the name of the CHANGELOG
  * Released on GitHub.

0.1 - 2007

  * Initial version, written by the author and released by Design By Front.
\ No newline at end of file

A  => README +64 -0
@@ 1,64 @@
Validates As UK Postcode (version 1.0)
======================================

This Ruby on Rails plugin implements an ActiveRecord validation helper
called validates_as_uk_postcode. The helper acts as if
validates_format_of was used with a regular expression that defines an
valid UK postcode:

  http://www.govtalk.gov.uk/gdsc/html/frames/PostCode.htm

Installation
------------

ruby script/plugin install http://github.com/srushe/validates_as_uk_postcode/tree/master

Full documentation on script/plugin can be obtained by invoking the
plugin script with no arguments:

ruby script/plugin

Usage
-----

In your model file do something like:

class MyClass < ActiveRecord::Base
  validates_as_uk_postcode :postcode
end

License
-------

This code is released under a Creative Commons Attribution-Share Alike
3.0 Unported License. The license can be seen here:

  http://creativecommons.org/licenses/by-sa/3.0/

The original version of the code (see History) was released under the
Creative Commons Attribution-Share Alike 2.5 License, which can be seen
at:

  http://creativecommons.org/licenses/by-sa/2.5/

History
-------

This plugin was originally written in 2007 when I worked at Design By
Front. I took the code for validates_as_email as my inspiration so the
plugin was released under the same license, the Creative Commons
Attribution-Share Alike 2.5 License, and made available via the company
svn server.

In recent months (as of March 2009) I've noticed that the address the
code was available at has not been responding. Therefore I've located
the code, tweaked some things, and I'm releasing this version of it.

This new version is being released under the newer Creative Commons
Attribution-Share Alike 3.0 Unported License.

Credits
-------

Written by Stephen Rushe (http://deeden.co.uk/)
Original version written by the author at Design By Front.
\ No newline at end of file

A  => Rakefile +22 -0
@@ 1,22 @@
require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'

desc 'Default: run unit tests.'
task :default => :test

desc 'Test the validates_as_uk_postcode plugin.'
Rake::TestTask.new(:test) do |t|
  t.libs << 'lib'
  t.pattern = 'test/**/*_test.rb'
  t.verbose = true
end

desc 'Generate documentation for the validates_as_uk_postcode plugin.'
Rake::RDocTask.new(:rdoc) do |rdoc|
  rdoc.rdoc_dir = 'rdoc'
  rdoc.title    = 'ValidatesAsUkPostcode'
  rdoc.options << '--line-numbers' << '--inline-source'
  rdoc.rdoc_files.include('README')
  rdoc.rdoc_files.include('lib/**/*.rb')
end

A  => init.rb +1 -0
@@ 1,1 @@
require 'validates_as_uk_postcode'
\ No newline at end of file

A  => lib/validates_as_uk_postcode.rb +44 -0
@@ 1,44 @@
#
# Licensed under a Creative Commons Attribution-ShareAlike 2.5 License
# http://creativecommons.org/licenses/by-sa/2.5/
# 
module Postcodes
  UK = begin    
    an_naa_or_ann_naa   = '^[A-PR-UWYZ]{1}\d{1,2}\s?\d[ABD-HJLNP-UWXYZ]{2}$'
    aan_naa_or_aann_naa = '^[A-PR-UWYZ]{1}[A-HK-Y]{1}\d{1,2}\s?\d[ABD-HJLNP-UWXYZ]{2}$'
    ana_naa             = '^[A-PR-UWYZ]{1}\d[A-HJKSTUW]{1}\s?\d[ABD-HJLNP-UWXYZ]{2}$'
    aana_naa            = '^[A-PR-UWYZ]{1}[A-HK-Y]{1}\d[ABEHMNPRVWXY]{1}\s?\d[ABD-HJLNP-UWXYZ]{2}$'
    historic_code="GIR\s?0AA"
    postcode_spec = "#{an_naa_or_ann_naa}|#{aan_naa_or_aann_naa}|#{ana_naa}|#{aana_naa}|#{historic_code}"
    pattern = /#{postcode_spec}/i
  end
end

# Validation helper for ActiveRecord derived objects that cleanly and simply
# allows the model to check if the given string is a syntactically valid email
# address (by using the RFC822 module above).
#
# Original code by Ximon Eighteen <ximon.eightee@int.greenpeace.org> which was
# heavily based on code I can no longer find on the net, my apologies to the
# author!
#
# Huge credit goes to Dan Kubb <dan.kubb@autopilotmarketing.com> for
# submitting a patch to massively simplify this code and thereby instruct me
# in the ways of Rails too! I reflowed the patch a little to keep the line
# length to a maximum of 78 characters, an old habit.

module ActiveRecord
  module Validations
    module ClassMethods
      def validates_as_uk_postcode(*attr_names)
        configuration = {
          :message   => 'is an invalid uk postcode',
          :with      => Postcodes::UK,
          :allow_nil => true }
        configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)

        validates_format_of attr_names, configuration
      end
    end
  end
end

A  => test/validates_as_uk_postcode_test.rb +72 -0
@@ 1,72 @@
require 'test/unit'

begin
  require File.dirname(__FILE__) + '/../../../../config/boot'
  require 'active_record'
  require 'validates_as_uk_postcode'
rescue LoadError
  require 'rubygems'
  gem 'activerecord'
  require File.dirname(__FILE__) + '/../lib/validates_as_uk_postcode'
end

class TestRecord < ActiveRecord::Base
  def self.columns; []; end
  attr_accessor :postcode
  validates_as_uk_postcode :postcode
end

class ValidatesAsUkPostcodeTest < Test::Unit::TestCase
  def test_invalid_postcodes
    postcodes = [
      'Q1 1AA', 'V1 1AA', 'X1 1AA', 'M1 CAA', 'M1 IAA', 'M1 KAA', 'M1 MAA', 'M1 OAA', 'M1 VAA',

      'Q60 1NW', 'V60 1NW', 'X60 1NW', 'M60 1CW', 'M60 1IW', 'M60 1KW', 'M60 1MW', 'M60 1OW', 
      'M60 1VW', 'M60 1NC', 'M60 1NI', 'M60 1NK', 'M60 1NM', 'M60 1NO', 'M60 1NV', 

      'QR2 6XH', 'VR2 6XH', 'XR2 6XH', 'CI2 6XH', 'CJ2 6XH', 'CZ2 6XH', 'CR2 6CH', 'CR2 6IH',
      'CR2 6KH', 'CR2 6MH', 'CR2 6OH', 'CR2 6VH', 'CR2 6XC', 'CR2 6XI', 'CR2 6XK', 'CR2 6XM',
      'CR2 6XO', 'CR2 6XV',

      'QN55 1PT', 'VN55 1PT', 'XN55 1PT', 'DI55 1PT', 'DJ55 1PT', 'DZ55 1PT', 'DN55 1CT',
      'DN55 1IT', 'DN55 1KT', 'DN55 1MT', 'DN55 1OT', 'DN55 1VT', 'DN55 1PC', 'DN55 1PI',
      'DN55 1PK', 'DN55 1PM', 'DN55 1PO', 'DN55 1PV',

      'Q1A 1HQ', 'V1A 1HQ', 'X1A 1HQ', 'W1I 1HQ', 'W1L 1HQ', 'W1M 1HQ', 'W1N 1HQ', 'W1O 1HQ',
      'W1P 1HQ', 'W1Q 1HQ', 'W1R 1HQ', 'W1V 1HQ', 'W1X 1HQ', 'W1Y 1HQ', 'W1Z 1HQ', 'W1A 1CQ',
      'W1A 1IQ', 'W1A 1KQ', 'W1A 1MQ', 'W1A 1OQ', 'W1A 1VQ', 'W1A 1HC', 'W1A 1HI', 'W1A 1HK',
      'W1A 1HM', 'W1A 1HO', 'W1A 1HV',

      'QC1A 1BB', 'VC1A 1BB', 'XC1A 1BB', 'EI1A 1BB', 'EJ1A 1BB', 'EZ1A 1BB', 'EC1C 1BB',
      'EC1D 1BB', 'EC1F 1BB', 'EC1G 1BB', 'EC1I 1BB', 'EC1J 1BB', 'EC1K 1BB', 'EC1L 1BB',
      'EC1O 1BB', 'EC1Q 1BB', 'EC1S 1BB', 'EC1T 1BB', 'EC1U 1BB', 'EC1Z 1BB', 'EC1A 1IB',
      'EC1A 1MB', 'EC1A 1OB', 'EC1A 1VB', 'EC1A 1BC', 'EC1A 1BI', 'EC1A 1BK', 'EC1A 1BM',
      'EC1A 1BO', 'EC1A 1BV',
      ]
    postcodes.each do |postcode|
      assert !TestRecord.new(:postcode => postcode).valid?, "#{postcode} should be illegal."
      assert !TestRecord.new(:postcode => postcode.downcase).valid?, "#{postcode.downcase} should be illegal."
      assert !TestRecord.new(:postcode => postcode.gsub(' ', '')).valid?, "#{postcode} (with no spaces) should be illegal."
      assert !TestRecord.new(:postcode => postcode.downcase.gsub(' ', '')).valid?, "#{postcode.downcase} (with no spaces) should be illegal."
    end
  end

  def test_valid_postcodes
    postcodes = [
      'M1 1AA',
      'M60 1NW', 
      'CR2 6XH',
      'DN55 1PT',
      'W1A 1HQ',
      'EC1A 1BB',
      'BT9 7JL',
      'GIR 0AA',
      ]
    postcodes.each do |postcode|
      assert TestRecord.new(:postcode => postcode).valid?, "#{postcode} should be legal."
      assert TestRecord.new(:postcode => postcode.downcase).valid?, "#{postcode.downcase} should be legal."
      assert TestRecord.new(:postcode => postcode.gsub(' ', '')).valid?, "#{postcode} (with no spaces) should be legal."
      assert TestRecord.new(:postcode => postcode.downcase.gsub(' ', '')).valid?, "#{postcode.downcase} (with no spaces) should be legal."
    end
  end
end