rut_validator 0.0.1

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/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/.simplecov ADDED
@@ -0,0 +1,5 @@
1
+ if ENV['COVERAGE']
2
+ SimpleCov.start do
3
+ add_filter '/spec/'
4
+ end
5
+ end
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ rvm:
2
+ - 1.9.2
3
+ - 1.9.3
4
+ - jruby-19mode
5
+ - ruby-head
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rut_validator.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Ignacio Ortega
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # RutValidator
2
+
3
+ Chilean RUT validator for ActiveRecord on rails 3.
4
+
5
+ [![Build Status](https://secure.travis-ci.org/iortega/rut_validator.png)](http://travis-ci.org/iortega/rut_validator)
6
+ [![Dependency Status](https://gemnasium.com/iortega/rut_validator.png)](https://gemnasium.com/iortega/rut_validator)
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'rut_validator'
13
+
14
+ Run:
15
+
16
+ $ bundle
17
+
18
+ ## Usage
19
+
20
+ Add the following to your model:
21
+
22
+ validates :rut_attribute, rut: true
23
+
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,11 @@
1
+ en:
2
+ activemodel:
3
+ errors:
4
+ models:
5
+ test_user:
6
+ attributes:
7
+ rut:
8
+ rut_invalid: invalid
9
+ errors:
10
+ messages:
11
+ rut_invalid: invalid
@@ -0,0 +1,3 @@
1
+ es:
2
+ errors:
3
+ rut_invalid: inválido
@@ -0,0 +1,47 @@
1
+ class Rut
2
+ attr_reader :code
3
+
4
+ def initialize(rut)
5
+ @rut = rut.to_s.strip
6
+ @number = @rut.gsub(/[^0-9K]/i,'')[0...-1]
7
+ @code = @rut[-1]
8
+ end
9
+
10
+ def valid?
11
+ size_valid? && code_valid?
12
+ end
13
+
14
+ def size_valid?
15
+ @number.size <= 8
16
+ end
17
+
18
+ def code_valid?
19
+ @code == calculated_code
20
+ end
21
+
22
+ def calculated_code
23
+ all_codes = (0..9).to_a + ['K']
24
+ reverse_digits = @number.split('').reverse
25
+ factors = (2..7).to_a * 2
26
+ partial_sum = reverse_digits.zip(factors).inject(0) do |r, a|
27
+ digit, factor = *a
28
+ r += digit.to_i * factor
29
+ end
30
+ all_codes[11 - partial_sum%11].to_s
31
+ end
32
+
33
+ def number_with_delimiter(delimiter='.')
34
+ @number.to_s.gsub(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{delimiter}")
35
+ end
36
+
37
+ def format(opts={})
38
+ delimiter = opts[:delimiter] || '.'
39
+ with_dash = opts[:with_dash].nil? ? true : opts[:with_dash]
40
+ formatted_rut = number_with_delimiter(delimiter)
41
+ formatted_rut << '-' if with_dash
42
+ formatted_rut << @code
43
+ end
44
+
45
+ def to_s; format; end
46
+
47
+ end
@@ -0,0 +1,10 @@
1
+ require 'rut_validator/rut'
2
+ I18n.load_path += Dir.glob( File.dirname(__FILE__) + "/rut_validator/locale/*.{rb,yml}" )
3
+
4
+ class RutValidator < ActiveModel::EachValidator
5
+ def validate_each(record, attribute, value)
6
+ unless Rut.new(value).valid?
7
+ record.errors.add(attribute, :rut_invalid)
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.name = "rut_validator"
7
+ gem.version = "0.0.1"
8
+ gem.authors = ["Ignacio Ortega"]
9
+ gem.email = ["ignacio.ortega@gmail.com"]
10
+ gem.description = %q{Chilean RUT validator for rails 3. See homepage for details http://github.com/iortega/rut_validator}
11
+ gem.summary = %q{Chilean RUT validator}
12
+ gem.homepage = %{http://github.com/iortega/rut_validator}
13
+
14
+ gem.files = `git ls-files`.split($/)
15
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
16
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
17
+ gem.require_paths = ["lib"]
18
+
19
+ gem.add_dependency 'activemodel', '>=0'
20
+ gem.add_development_dependency "rake"
21
+ gem.add_development_dependency 'rspec', '>= 0'
22
+ gem.add_development_dependency 'simplecov', '>=0'
23
+ end
data/spec/rut_spec.rb ADDED
@@ -0,0 +1,39 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rut do
4
+
5
+ context 'valid inputs' do
6
+
7
+ [11222333, '11222333', '11222333-9', '11.222.333-9'].each do |r|
8
+ let(:rut) { Rut.new(r) }
9
+
10
+ it "#{r.inspect} formatted" do
11
+ rut.format.should == '11.222.333-9'
12
+ end
13
+
14
+ it "#{r.inspect} no delimiters formatting" do
15
+ rut.format(delimiter: '').should == '11222333-9'
16
+ end
17
+
18
+ it "#{r.inspect} only numbers formatting" do
19
+ rut.format(delimiter: '', with_dash: false).should == '112223339'
20
+ end
21
+
22
+ it "#{r.inspect} should be valid" do
23
+ rut.valid?.should be_true
24
+ end
25
+ end
26
+
27
+ end
28
+
29
+ context 'invalid inputs' do
30
+
31
+ ['invalid', '112223338', 1234, nil].each do |r|
32
+ it "#{r.inspect} should be invalid" do
33
+ Rut.new(r).valid?.should be_false
34
+ end
35
+ end
36
+
37
+ end
38
+
39
+ end
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ class TestUser < TestModel
4
+ validates :rut, rut: true
5
+ end
6
+
7
+ class TestUserAllowNil < TestModel
8
+ validates :rut, rut: true, allow_nil: true
9
+ end
10
+
11
+ describe RutValidator do
12
+
13
+ context 'valid models' do
14
+ it 'should be valid' do
15
+ TestUser.new(rut: '11222333-9').should be_valid
16
+ end
17
+
18
+ it 'should not be valid' do
19
+ TestUser.new(rut: 'invalid').should_not be_valid
20
+ end
21
+
22
+ it 'should be valid' do
23
+ TestUserAllowNil.new(rut: nil).should be_valid
24
+ end
25
+ end
26
+
27
+ context 'errors messages' do
28
+ subject { TestUser.new(rut: 'invalid') }
29
+ before { subject.valid? }
30
+
31
+ it 'should contains errors messages' do
32
+ subject.errors[:rut].should include 'invalid'
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,17 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'simplecov'
4
+ require 'active_model'
5
+ require 'rut_validator'
6
+
7
+ class TestModel
8
+ include ActiveModel::Validations
9
+
10
+ def initialize(attributes={})
11
+ @attributes = attributes
12
+ end
13
+
14
+ def read_attribute_for_validation(key)
15
+ @attributes[key]
16
+ end
17
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rut_validator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ignacio Ortega
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-25 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activemodel
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: simplecov
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: Chilean RUT validator for rails 3. See homepage for details http://github.com/iortega/rut_validator
79
+ email:
80
+ - ignacio.ortega@gmail.com
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .gitignore
86
+ - .rspec
87
+ - .simplecov
88
+ - .travis.yml
89
+ - Gemfile
90
+ - LICENSE.txt
91
+ - README.md
92
+ - Rakefile
93
+ - lib/rut_validator.rb
94
+ - lib/rut_validator/locale/en.yml
95
+ - lib/rut_validator/locale/es.yml
96
+ - lib/rut_validator/rut.rb
97
+ - rut_validator.gemspec
98
+ - spec/rut_spec.rb
99
+ - spec/rut_validator_spec.rb
100
+ - spec/spec_helper.rb
101
+ homepage: http://github.com/iortega/rut_validator
102
+ licenses: []
103
+ post_install_message:
104
+ rdoc_options: []
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ none: false
109
+ requirements:
110
+ - - ! '>='
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ none: false
115
+ requirements:
116
+ - - ! '>='
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubyforge_project:
121
+ rubygems_version: 1.8.23
122
+ signing_key:
123
+ specification_version: 3
124
+ summary: Chilean RUT validator
125
+ test_files:
126
+ - spec/rut_spec.rb
127
+ - spec/rut_validator_spec.rb
128
+ - spec/spec_helper.rb