cuit_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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9d5d7cb8889002c667be291d4d8f2d38eaac6a0f
4
+ data.tar.gz: 009c892f1dab9c4334e39ad56c03547d4e341e1e
5
+ SHA512:
6
+ metadata.gz: ac30a37f0900fe16d307ae3b9749ef16fde14eeb6e9af7e4045be06d7f50a22237a6d2faf197a94ce899812e4778c1c7f0c1fdf6e9fa629221ab8d3ecf884ab4
7
+ data.tar.gz: 173f9ae0b333e8ee2c9ef801c959e52b0023edb6cf0de92ad6bec104263920bba2c20d74678331468eb7a91d54d1c203255bc2832c7aea1f2560b16bb6a0a336
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2015 Maximiliano García
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,33 @@
1
+ ## Usage
2
+
3
+ Add to your Gemfile:
4
+
5
+ ```ruby
6
+ gem 'cuit_validator'
7
+ ```
8
+
9
+ Run:
10
+
11
+ ```
12
+ bundle install
13
+ ```
14
+
15
+ Then add the following to your model:
16
+
17
+ ```ruby
18
+ validates :my_cuit, :cuit => true
19
+ ```
20
+
21
+ ## Validation outside a model
22
+
23
+ If you need to validate the cuit outside a model:
24
+
25
+ ### Normal mode
26
+
27
+ ```ruby
28
+ CuitValidator.valid?('20-11111111-2') # boolean
29
+ ```
30
+
31
+ ## Thread safety
32
+
33
+ This gem is thread safe, with one caveat: `EmailValidator.default_options` must be configured before use in a multi-threaded environment. If you configure `default_options` in a Rails initializer file, then you're good to go since initializers are run before worker threads are spawned.
data/Rakefile ADDED
@@ -0,0 +1,25 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+ require 'bundler'
9
+ require 'rspec/core/rake_task'
10
+
11
+ Bundler::GemHelper.install_tasks
12
+
13
+ RDoc::Task.new(:rdoc) do |rdoc|
14
+ rdoc.rdoc_dir = 'rdoc'
15
+ rdoc.title = 'CuitValidator'
16
+ rdoc.options << '--line-numbers'
17
+ rdoc.rdoc_files.include('README.rdoc')
18
+ rdoc.rdoc_files.include('lib/**/*.rb')
19
+ end
20
+
21
+ RSpec::Core::RakeTask.new do |t|
22
+ t.rspec_opts = %w(--format documentation --colour)
23
+ end
24
+
25
+ task :default => 'spec'
@@ -0,0 +1,34 @@
1
+ class CuitValidator < ActiveModel::EachValidator
2
+ @@default_options = {}
3
+
4
+ def self.default_options
5
+ @@default_options
6
+ end
7
+
8
+ def self.valid?(value, options = {})
9
+ unless value.empty?
10
+ value.gsub!('-','')
11
+ unless value.length != 11
12
+ xy, dni, z = value[0, 2], value[2, 8], value[10, 1]
13
+ acum = 0
14
+ secuence = [5,4,3,2,7,6,5,4,3,2]
15
+
16
+ "#{xy}#{dni}".split('').each_with_index { |num, i| acum += num.to_i * secuence[i] }
17
+
18
+ z_calculated = (11 - (acum % 11) )
19
+
20
+ (z_calculated.to_i == z.to_i)
21
+ end
22
+ else
23
+ true if options[:allow_empty]
24
+ end
25
+ end
26
+
27
+ def validate_each(record, attribute, value)
28
+ options = @@default_options.merge(self.options)
29
+ unless self.class.valid?(value, options)
30
+ record.errors.add(attribute, options[:message] || :invalid)
31
+ end
32
+ end
33
+
34
+ end
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+
3
+ class TestUser < TestModel
4
+ validates :cuit, :cuit => true
5
+ end
6
+
7
+ class TestUser2 < TestModel
8
+ validates :cuit, :cuit => {:allow_empty => true}
9
+ end
10
+
11
+ RSpec.describe CuitValidator do
12
+
13
+ context "#valid" do
14
+ it "must return true when cuit is valid" do
15
+ expect(TestUser.new(:cuit => "20-11111111-2")).to be_valid
16
+ end
17
+
18
+ it "must return false when cuit is not valid" do
19
+ expect(TestUser.new(:cuit => "20-11111111-4")).to_not be_valid
20
+ end
21
+
22
+ it "must return true when the flag allow empty is true" do
23
+ expect(TestUser2.new(:cuit => "")).to be_valid
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,20 @@
1
+ require 'rubygems'
2
+ require 'rspec'
3
+ require 'active_model'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+
8
+ require 'cuit_validator'
9
+
10
+ class TestModel
11
+ include ActiveModel::Validations
12
+
13
+ def initialize(attributes = {})
14
+ @attributes = attributes
15
+ end
16
+
17
+ def read_attribute_for_validation(key)
18
+ @attributes[key]
19
+ end
20
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cuit_validator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Maximiliano García
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-08-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activemodel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Allows validate CUIT and CUIL in Ruby applications
56
+ email:
57
+ - magf.1987@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - MIT-LICENSE
63
+ - README.rdoc
64
+ - Rakefile
65
+ - lib/cuit_validator.rb
66
+ - spec/cuit_validator_spec.rb
67
+ - spec/spec_helper.rb
68
+ homepage: https://github.com/maxigarciaf/cuit_validator
69
+ licenses:
70
+ - MIT
71
+ metadata: {}
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirements: []
87
+ rubyforge_project:
88
+ rubygems_version: 2.4.5
89
+ signing_key:
90
+ specification_version: 4
91
+ summary: A cuit validator for Ruby and Rails >= 3
92
+ test_files:
93
+ - spec/spec_helper.rb
94
+ - spec/cuit_validator_spec.rb