more_validators 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,7 @@
1
+ 0.1.2 - May 13th 2010
2
+ * new validators:
3
+ ** validates_as_uf_br
4
+
1
5
  0.1.0 - May 12th 2010
2
6
  * First release, with the following validators:
3
7
  ** validates_as_cpf
data/README CHANGED
@@ -17,7 +17,7 @@ Install the gem(s):
17
17
  gem install more_validators
18
18
 
19
19
  Add to environment.rb initializer block:
20
- config.gem 'more_validators', :version => '>=0.1.0', :lib => 'validates_as_'
20
+ config.gem 'more_validators', :lib => 'validates_as_'
21
21
 
22
22
  Usage:
23
23
  ======
@@ -40,6 +40,9 @@ class MyClass < ActiveRecord::Base
40
40
  # CNPJ validation
41
41
  validates_as_cnpj :cnpj, :message => 'Invalid', :allow_nil => true
42
42
 
43
+ # Brazilian UF validation, both upcase and downcase are allowed by default
44
+ validates_as_uf_br :uf, :message => 'Invalid', :allow_nil => true, :allow_up => true, :allow_down => true
45
+
43
46
  end
44
47
 
45
48
  Tests:
data/lib/validates_as_.rb CHANGED
@@ -3,4 +3,5 @@ require 'validates_as_cnpj'
3
3
  require 'validates_as_cpf'
4
4
  require 'validates_as_email'
5
5
  require 'validates_as_phonenumber_br'
6
- require 'validates_as_website'
6
+ require 'validates_as_website'
7
+ require 'validates_as_uf_br'
@@ -0,0 +1,32 @@
1
+ module UF_BR
2
+ LIST_UP = %w(AC AL AM AP BA CE DF ES GO MA MG MS MT PA PB PE PI PR RJ RN RO RR RS SC SE SP TO)
3
+ LIST_DOWN = %w(ac al am ap ba ce df es go ma mg ms mt pa pb pe pi pr rj rn ro rr rs sc se sp to)
4
+
5
+ def self.valid?(uf_br, allow_up = true, allow_down = true)
6
+ (allow_up and LIST_UP.include? uf_br) or (allow_down and LIST_DOWN.include? uf_br)
7
+ end
8
+
9
+ def self.invalid?(uf_br, allow_up = true, allow_down = true)
10
+ !valid?(uf_br, allow_up, allow_down)
11
+ end
12
+ end
13
+
14
+ module ActiveRecord
15
+ module Validations
16
+ module ClassMethods
17
+ def validates_as_uf_br(*attr_names)
18
+ configuration = {
19
+ :message => I18n.translate('activerecord.errors.messages.invalid', :default => 'invalid' ),
20
+ :allow_nil => true,
21
+ :allow_down => true,
22
+ :allow_up => true }
23
+ configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)
24
+
25
+ validates_each attr_names, configuration do |record, attribute, value|
26
+ record.errors.add(attribute, configuration[:message]) if UF_BR.invalid?(value, configuration[:allow_up], configuration[:allow_down])
27
+ end
28
+
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,43 @@
1
+ require 'test/unit'
2
+
3
+ begin
4
+ require File.dirname(__FILE__) + '/../../../../config/boot'
5
+ require 'active_record'
6
+ require 'i18n'
7
+ require 'validates_as_uf_br'
8
+ rescue LoadError
9
+ require 'rubygems'
10
+ require 'active_record'
11
+ ActiveRecord::ActiveRecordError # work-around to solve problems with ActiveRecord::Validations
12
+ require 'i18n'
13
+ require File.dirname(__FILE__) + '/../lib/validates_as_uf_br'
14
+ end
15
+
16
+ class TestRecord < ActiveRecord::Base
17
+ def self.columns; []; end
18
+ attr_accessor :uf_br
19
+ validates_as_uf_br :uf_br
20
+ end
21
+
22
+ class ValidatesAsUFBRTest < Test::Unit::TestCase
23
+ def test_illegal_uf_br
24
+ values = [
25
+ 'rh',
26
+ 'cc',
27
+ 'sao paulo',
28
+ 'saop'
29
+ ]
30
+ values.each do |value|
31
+ assert !TestRecord.new(:uf_br => value).valid?, "#{value} should be illegal."
32
+ end
33
+ end
34
+
35
+ def test_legal_uf_br
36
+ values = %w(ac al am ap ba ce df es go ma mg ms mt pa pb pe pi pr rj rn ro rr rs sc se sp to
37
+ AC AL AM AP BA CE DF ES GO MA MG MS MT PA PB PE PI PR RJ RN RO RR RS SC SE SP TO)
38
+
39
+ values.each do |value|
40
+ assert TestRecord.new(:uf_br => value).valid?, "#{value} should be legal."
41
+ end
42
+ end
43
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 1
9
- version: 0.1.1
8
+ - 2
9
+ version: 0.1.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Rafael Barbolo
@@ -17,7 +17,7 @@ autorequire:
17
17
  bindir: bin
18
18
  cert_chain: []
19
19
 
20
- date: 2010-05-12 00:00:00 -03:00
20
+ date: 2010-05-13 00:00:00 -03:00
21
21
  default_executable:
22
22
  dependencies: []
23
23
 
@@ -42,12 +42,14 @@ files:
42
42
  - lib/validates_as_email.rb
43
43
  - lib/validates_as_phonenumber_br.rb
44
44
  - lib/validates_as_website.rb
45
+ - lib/validates_as_uf_br.rb
45
46
  - test/validates_as_cep_test.rb
46
47
  - test/validates_as_cnpj_test.rb
47
48
  - test/validates_as_cpf_test.rb
48
49
  - test/validates_as_email_test.rb
49
50
  - test/validates_as_phonenumber_br_test.rb
50
51
  - test/validates_as_website_test.rb
52
+ - test/validates_as_uf_br_test.rb
51
53
  has_rdoc: true
52
54
  homepage: http://github.com/barbolo/more_validators
53
55
  licenses: []
@@ -85,3 +87,4 @@ test_files:
85
87
  - test/validates_as_email_test.rb
86
88
  - test/validates_as_phonenumber_br_test.rb
87
89
  - test/validates_as_website_test.rb
90
+ - test/validates_as_uf_br_test.rb