nif 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -20,10 +20,9 @@ Just:
20
20
  => true
21
21
 
22
22
  ### Description
23
-
24
- This is a simple project used to learn and teach the basics of ruby syntax, unit testing and code coverage.
25
- It covers the generation and validation of the Portuguese Fiscal Code (or NIF).
26
- If you want to contribute be sure to make a test covering your changes.
23
+ This gem covers the generation and validation of the Portuguese Fiscal Code (or NIF).
24
+ I encourage you to contribute but be sure to include a test covering your changes in your pull request.
25
+ I've tested it on ruby 1.8.7-p358, 1.9.3-p125 and rails 3.2.3.
27
26
 
28
27
  Have fun!
29
28
 
@@ -37,5 +36,26 @@ Have fun!
37
36
  # check coverage
38
37
  rake rcov # open coverage/index.html for the results
39
38
 
39
+ ### Rails Custom Validator
40
+ To have this included as a rails custom validator you have to include it on your model like this:
41
+
42
+ class Person < ActiveRecord::Base
43
+ include Nif
44
+
45
+ validates :nif, :nif=> true
46
+ end
47
+
48
+
49
+ nif comes with it's own I18n file but you can also define the :invalid key in your locale to have a custom message in your models like this:
50
+
51
+ en:
52
+ activerecord:
53
+ errors:
54
+ models:
55
+ person:
56
+ attributes:
57
+ nif:
58
+ invalid: 'is not valid!'
59
+
40
60
  ### TODO
41
- Make a CustomValidator for rails.
61
+ Have javascript validations available.
data/lib/nif.rb CHANGED
@@ -56,5 +56,14 @@ module Nif
56
56
  check_number > 9 ? 0 : check_number
57
57
  end
58
58
  end
59
-
59
+
60
+ if defined?(ActiveModel::EachValidator)
61
+ class NifValidator < ActiveModel::EachValidator
62
+ def validate_each(record, attribute, value)
63
+ record.errors.add(attribute, :invalid) unless Nif::Validator.validate(value)
64
+ end
65
+ end
66
+
67
+ I18n.load_path << File.join(File.dirname(__FILE__), "nif", "locale", 'en.yml')
68
+ end
60
69
  end
@@ -0,0 +1,4 @@
1
+ en:
2
+ errors:
3
+ messages:
4
+ invalid: is not valid
@@ -0,0 +1,4 @@
1
+ en:
2
+ errors:
3
+ messages:
4
+ invalid: é inválido
@@ -1,3 +1,3 @@
1
1
  module Nif
2
- VERSION = Version = '0.0.2'
2
+ VERSION = Version = '0.0.3'
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nif
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - "Pedro Carri\xC3\xA7o"
@@ -15,11 +15,11 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-04-22 00:00:00 +01:00
18
+ date: 2012-06-02 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
22
- description: This is a simple project that covers the generation and validation of the Portuguese Fiscal Code (or NIF).
22
+ description: This gem covers the generation and validation of the Portuguese Fiscal Code (or NIF). It also includes a rails custom validator.
23
23
  email: pedro.carrico@gmail.com
24
24
  executables: []
25
25
 
@@ -29,6 +29,8 @@ extra_rdoc_files: []
29
29
 
30
30
  files:
31
31
  - lib/nif.rb
32
+ - lib/nif/locale/en.yml
33
+ - lib/nif/locale/pt-PT.yml
32
34
  - lib/nif/version.rb
33
35
  - README.md
34
36
  - LICENSE