validates_as_postal_code 0.5.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +3 -0
- data/LICENSE +5 -0
- data/README +36 -0
- data/Rakefile +22 -0
- data/init.rb +1 -0
- data/lib/validates_as_postal_code.rb +26 -0
- data/test/validates_as_postal_code_test.rb +61 -0
- metadata +62 -0
data/CHANGELOG
ADDED
data/LICENSE
ADDED
data/README
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
ValidatesAsPostalCode
|
2
|
+
=====================
|
3
|
+
|
4
|
+
This Ruby on Rails plugin/gem implements an ActiveRecord validation helper called
|
5
|
+
validates_as_postal_code which validates U.S. postal codes. The helper acts as if
|
6
|
+
validates_format_of was used with a regular expression that defines:
|
7
|
+
|
8
|
+
00000
|
9
|
+
00000-0000
|
10
|
+
|
11
|
+
|
12
|
+
Installation:
|
13
|
+
=============
|
14
|
+
gem sources -a http://gems.github.com
|
15
|
+
|
16
|
+
Install the gem(s):
|
17
|
+
sudo gem install gbdev-validates_as_phone_number
|
18
|
+
|
19
|
+
Add to environment.rb initializer block:
|
20
|
+
config.gem 'gbdev-validates_as_postal_code', :version => '>=0.5.0', :lib => 'validates_as_postal_code', :source => 'http://gems.github.com'
|
21
|
+
|
22
|
+
Usage:
|
23
|
+
======
|
24
|
+
In your model file do something like:
|
25
|
+
|
26
|
+
class MyClass < ActiveRecord::Base
|
27
|
+
validates_as_postal_code :postal_code, :message => 'Invalid Postal Code', :allow_nil => true
|
28
|
+
end
|
29
|
+
|
30
|
+
Tests:
|
31
|
+
======
|
32
|
+
Some tests have been added.
|
33
|
+
|
34
|
+
License:
|
35
|
+
========
|
36
|
+
See the LICENSE file.
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
|
5
|
+
desc 'Default: run unit tests.'
|
6
|
+
task :default => :test
|
7
|
+
|
8
|
+
desc 'Test the validates_as_postal_code plugin.'
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
10
|
+
t.libs << 'lib'
|
11
|
+
t.pattern = 'test/**/*_test.rb'
|
12
|
+
t.verbose = true
|
13
|
+
end
|
14
|
+
|
15
|
+
desc 'Generate documentation for the validates_as_postal_code plugin.'
|
16
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
17
|
+
rdoc.rdoc_dir = 'rdoc'
|
18
|
+
rdoc.title = 'ValidatesAsPostalCode'
|
19
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
20
|
+
rdoc.rdoc_files.include('README')
|
21
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
22
|
+
end
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'validates_as_postal_code'
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# Licensed under a Creative Commons Attribution-ShareAlike 2.5 License
|
2
|
+
# http://creativecommons.org/licenses/by-sa/2.5/
|
3
|
+
|
4
|
+
# Validation helper for ActiveRecord derived objects that cleanly and simply
|
5
|
+
# allows the model to check if the given string is a valid US postal code
|
6
|
+
# in the form 00000 or 00000-0000
|
7
|
+
|
8
|
+
module ActiveRecord
|
9
|
+
module Validations
|
10
|
+
module ClassMethods
|
11
|
+
def validates_as_postal_code(*attr_names)
|
12
|
+
regExpStr = '\d{5}(-\d{4})?'
|
13
|
+
configuration = {
|
14
|
+
:message => 'is an invalid postal code',
|
15
|
+
:with => /^#{regExpStr}$/,
|
16
|
+
:allow_nil => false }
|
17
|
+
|
18
|
+
configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)
|
19
|
+
|
20
|
+
configuration[:with] = /^(#{regExpStr})?$/ if configuration[:allow_nil]
|
21
|
+
|
22
|
+
validates_format_of attr_names, configuration
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
|
3
|
+
begin
|
4
|
+
require File.dirname(__FILE__) + '/../../../../config/boot'
|
5
|
+
require 'active_record'
|
6
|
+
require 'validates_as_postal_code'
|
7
|
+
rescue LoadError
|
8
|
+
require 'rubygems'
|
9
|
+
require 'activerecord'
|
10
|
+
require File.dirname(__FILE__) + '/../lib/validates_as_postal_code'
|
11
|
+
end
|
12
|
+
|
13
|
+
class TestRecord < ActiveRecord::Base
|
14
|
+
def self.columns; []; end
|
15
|
+
attr_accessor :postal_code
|
16
|
+
validates_as_postal_code :postal_code, :allow_nil => false # false is default
|
17
|
+
end
|
18
|
+
|
19
|
+
class TestRecord2 < ActiveRecord::Base
|
20
|
+
def self.columns; []; end
|
21
|
+
attr_accessor :postal_code
|
22
|
+
validates_as_postal_code :postal_code, :allow_nil => true
|
23
|
+
end
|
24
|
+
|
25
|
+
class ValidatesAsPostalCodeTest < Test::Unit::TestCase
|
26
|
+
|
27
|
+
def test_valid_postal_codes
|
28
|
+
postal_codes = [
|
29
|
+
'89511',
|
30
|
+
'89511-1000'
|
31
|
+
]
|
32
|
+
postal_codes.each do |code|
|
33
|
+
assert TestRecord.new(:postal_code => code).valid?, "#{code} should be legal."
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_invalid_postal_codes
|
38
|
+
postal_codes = [
|
39
|
+
'895112',
|
40
|
+
'8951',
|
41
|
+
'895110044',
|
42
|
+
'89511-00001',
|
43
|
+
'8951X'
|
44
|
+
]
|
45
|
+
postal_codes.each do |code|
|
46
|
+
assert !TestRecord.new(:postal_code => code).valid?, "#{code} should be illegal."
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_blank_postal_code_not_allowed
|
51
|
+
assert !TestRecord.new(:postal_code => nil).valid?, "Blank postal code should be illegal."
|
52
|
+
assert !TestRecord.new(:postal_code => '').valid?, "Blank postal code should be illegal."
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_blank_postal_code_allowed
|
56
|
+
assert TestRecord2.new(:postal_code => nil).valid?, "Blank postal code should be legal."
|
57
|
+
assert TestRecord2.new(:postal_code => '').valid?, "Blank postal code should be legal."
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
|
metadata
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: validates_as_postal_code
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.5.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Wes Hays
|
8
|
+
- John Dell
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2008-08-15 00:00:00 -07:00
|
14
|
+
default_executable:
|
15
|
+
dependencies: []
|
16
|
+
|
17
|
+
description: Rails gem that implements an ActiveRecord validation helper called validates_as_postal_code which validates U.S. postal codes
|
18
|
+
email: gems@gbdev.com
|
19
|
+
executables: []
|
20
|
+
|
21
|
+
extensions: []
|
22
|
+
|
23
|
+
extra_rdoc_files: []
|
24
|
+
|
25
|
+
files:
|
26
|
+
- CHANGELOG
|
27
|
+
- LICENSE
|
28
|
+
- README
|
29
|
+
- Rakefile
|
30
|
+
- init.rb
|
31
|
+
- lib/validates_as_postal_code.rb
|
32
|
+
- test/validates_as_postal_code_test.rb
|
33
|
+
has_rdoc: true
|
34
|
+
homepage: http://github.com/gbdev/validates_as_postal_code
|
35
|
+
licenses: []
|
36
|
+
|
37
|
+
post_install_message:
|
38
|
+
rdoc_options: []
|
39
|
+
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: "0"
|
47
|
+
version:
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: "0"
|
53
|
+
version:
|
54
|
+
requirements: []
|
55
|
+
|
56
|
+
rubyforge_project:
|
57
|
+
rubygems_version: 1.3.5
|
58
|
+
signing_key:
|
59
|
+
specification_version: 3
|
60
|
+
summary: Rails gem to validate format of U.S. postal codes
|
61
|
+
test_files:
|
62
|
+
- test/validates_as_postal_code_test.rb
|