italian_job 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,3 +1,6 @@
1
1
  pkg/*
2
2
  *.gem
3
3
  .bundle
4
+ .yardoc
5
+ doc
6
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --format nested
3
+ --tty
data/README.textile ADDED
@@ -0,0 +1,35 @@
1
+ The aim of this gem is to provide goodies for developers that are dealing with
2
+ typical italian business problem with rails 3. Any help will be more than
3
+ welcome.
4
+
5
+ h2. Validators
6
+
7
+ There are two validators:
8
+
9
+ * codice_fiscale_format
10
+ The Italian Tax code is called Codice Fiscale. For more information please
11
+ take a look at "http://en.wikipedia.org/wiki/italian_fiscal_code_card":http://en.wikipedia.org/wiki/Italian_fiscal_code_card.
12
+
13
+ * partita_iva_format
14
+ The Italian VAT is called partita iva. For more information please
15
+ take a look at "http://en.wikipedia.org/wiki/Value_added_tax_identification_number":http://en.wikipedia.org/wiki/Value_added_tax_identification_number.
16
+
17
+ h2. Helpers
18
+
19
+ Please take a look to the roadmap for future developments.
20
+
21
+ h2. Roadmap
22
+
23
+ * write some code comments
24
+ * add cellulare validator ( for italian mobile phones)
25
+ * add numero_fisso validator ( for italian landline numbers)
26
+ * add some helpers for italian regions and provinces.
27
+ * try to get feedback and add more stuff here
28
+
29
+ h2. Copyright
30
+
31
+ This program is free software. It comes without any warranty,
32
+ to the extent permitted by applicable law. You can redistribute
33
+ it and/or modify it under the terms of the Do What The Fuck You
34
+ Want To Public License, Version 2, as published by Sam Hocevar.
35
+ See http://sam.zoy.org/wtfpl/COPYING for more details.
data/Rakefile CHANGED
@@ -1,2 +1,8 @@
1
1
  require 'bundler'
2
2
  Bundler::GemHelper.install_tasks
3
+
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new
7
+
8
+ task :default => :spec
data/italian_job.gemspec CHANGED
@@ -3,19 +3,23 @@ $:.push File.expand_path("../lib", __FILE__)
3
3
  require "italian_job/version"
4
4
 
5
5
  Gem::Specification.new do |s|
6
- s.name = "italian_job"
7
- s.version = ItalianJob::VERSION
8
- s.platform = Gem::Platform::RUBY
9
- s.authors = ["Luca Pette"]
10
- s.email = ["lucapette@gmail.com"]
11
- s.homepage = "http://github.com/lucapette/italian_job"
12
- s.summary = %q{ActiveRecord Validators for dealing with typical italian business problems}
13
- s.description = %q{ If you want to validate Italian tax Code ( Codice Fiscale) or Italian VAT (Partita IVA) then you need this gem.}
6
+ s.name = "italian_job"
7
+ s.version = ItalianJob::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Luca Pette"]
10
+ s.email = ["lucapette@gmail.com"]
11
+ s.homepage = "http://github.com/lucapette/italian_job"
12
+ s.summary = %q{ActiveRecord Validators for dealing with typical italian business problems}
13
+ s.description = %q{If you want to validate Italian tax Code (Codice Fiscale) or Italian VAT (Partita IVA) then you need this gem.}
14
14
 
15
- s.rubyforge_project = "italian_job"
15
+ s.rubyforge_project = "italian_job"
16
16
 
17
- s.files = `git ls-files`.split("\n")
18
- s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
- s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
- s.require_paths = ["lib"]
17
+ s.add_development_dependency 'sqlite3'
18
+ s.add_dependency "rails", ">= 3.0.0"
19
+ s.add_development_dependency "rspec-rails", ">= 2.5.0"
20
+
21
+ s.files = `git ls-files`.split("\n")
22
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
23
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
24
+ s.require_paths = ["lib"]
21
25
  end
data/lib/italian_job.rb CHANGED
@@ -1,3 +1,4 @@
1
- module ItalianJob
2
- # Your code goes here...
3
- end
1
+ require "active_record"
2
+ require "italian_job/validators"
3
+ require "italian_job/validators/codice_fiscale_format"
4
+ require "italian_job/validators/partita_iva_format"
@@ -0,0 +1 @@
1
+ require "active_model"
@@ -0,0 +1,33 @@
1
+ module ActiveModel
2
+ module Validations
3
+ class CodiceFiscaleFormatValidator < ActiveModel::EachValidator
4
+ REGEX=Regexp.compile("^[A-Z]{6}[0-9]{2}[A-Z][0-9]{2}[A-Z][0-9]{3}[A-Z]$")
5
+
6
+ DISPARI=[1, 0, 5, 7, 9, 13, 15, 17, 19, 21, 1, 0, 5, 7, 9, 13, 15, 17, 19, 21, 2, 4, 18, 20, 11, 3, 6, 8, 12, 14, 16, 10, 22, 25, 24, 23]
7
+
8
+ def validate_each(object, attribute, value)
9
+ if value.blank?
10
+ object.errors[attribute] << I18n.translate("activerecord.errors.codice_fiscale.empty")
11
+ return
12
+ end
13
+ unless value.match(REGEX)
14
+ object.errors[attribute] << I18n.translate("activerecord.errors.codice_fiscale.invalid_format")
15
+ return
16
+ end
17
+ unless control_code_valid?(value)
18
+ object.errors[attribute] << I18n.translate("activerecord.errors.codice_fiscale.invalid_format")
19
+ return
20
+ end
21
+ end
22
+
23
+ private
24
+ def control_code_valid?(value)
25
+ odds, evens = [], []
26
+ value[0..14].split('').each_with_index {|e,i| (i+1).odd? ? odds << e : evens << e}
27
+ odd = odds.inject(0) { |sum, current_char| sum + DISPARI[(current_char.ord < 65 ? current_char.to_i : ((current_char.ord - 54)-1))] }
28
+ even = evens.inject(0) { |sum, current_char| sum + (current_char.ord < 65 ? current_char.to_i : current_char.ord - 65) }
29
+ ( ((odd + even) % 26) + 65).chr == value[15].chr
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,35 @@
1
+ module ActiveModel
2
+ module Validations
3
+ class PartitaIvaFormatValidator < ActiveModel::EachValidator
4
+ REGEX=Regexp.compile("^[0-9]{11}$")
5
+
6
+
7
+ def validate_each(object, attribute, value)
8
+ if value.blank?
9
+ object.errors[attribute] << I18n.translate("activerecord.errors.partita_iva.empty")
10
+ return
11
+ end
12
+ unless value.match(REGEX)
13
+ object.errors[attribute] << I18n.translate("activerecord.errors.partita_iva.invalid_format")
14
+ return
15
+ end
16
+ unless control_code_valid?(value)
17
+ object.errors[attribute] << I18n.translate("activerecord.errors.partita_iva.invalid_format")
18
+ return
19
+ end
20
+ end
21
+
22
+ private
23
+ def control_code_valid?(value)
24
+ odds, evens = [], []
25
+ value[0..9].split('').map(&:to_i).each_with_index {|e,i| (i+1).odd? ? odds << e : evens << e}
26
+ x = odds.inject(0) { |sum,d| sum + d }
27
+ y = 2 * ( evens.inject(0) { |sum,d| sum + d } )
28
+ z = evens.select { |d| d >= 5 }.size
29
+ t = ( x + y + z ) % 10
30
+ value[10].chr.to_i == (10 - t) % 10
31
+ end
32
+
33
+ end
34
+ end
35
+ end
@@ -1,3 +1,3 @@
1
1
  module ItalianJob
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -0,0 +1,55 @@
1
+ require "spec_helper"
2
+
3
+ describe "codice_fiscale_validator" do
4
+
5
+ before(:each) do
6
+ @user = User.new
7
+ end
8
+
9
+ context "with no value" do
10
+
11
+ it "is invalid" do
12
+ @user.should_not be_valid
13
+ end
14
+
15
+ it "has an error" do
16
+ @user.should have(1).errors_on(:codice_fiscale)
17
+ end
18
+
19
+ it "returns an empty error" do
20
+ @user.should_not be_valid
21
+ @user.errors[:codice_fiscale].should == [I18n.translate("activerecord.errors.codice_fiscale.empty")]
22
+ end
23
+
24
+ end
25
+
26
+
27
+ VALID_CODICE_FISCALE_CODES.each do |code|
28
+ context "with the valid code #{code}" do
29
+ it "has no errors" do
30
+ @user.codice_fiscale = code
31
+ @user.should have(:no).errors_on(:codice_fiscale)
32
+ end
33
+ end
34
+ end
35
+
36
+ INVALID_CODICE_FISCALE_CODES.each do |code|
37
+
38
+ context "with the invalid code #{code}" do
39
+
40
+ it "has an error" do
41
+ @user.codice_fiscale = code
42
+ @user.should have(1).error_on(:codice_fiscale)
43
+ end
44
+
45
+ it "returns an invalid_format error" do
46
+ @user.codice_fiscale = code
47
+ @user.should_not be_valid
48
+ @user.errors[:codice_fiscale].should == [I18n.translate("activerecord.errors.codice_fiscale.invalid_format")]
49
+ end
50
+
51
+
52
+ end
53
+ end
54
+
55
+ end
@@ -0,0 +1,55 @@
1
+ require "spec_helper"
2
+
3
+ describe "partita_iva_validator" do
4
+
5
+ before(:each) do
6
+ @user = User.new
7
+ end
8
+
9
+ context "with no value" do
10
+
11
+ it "is invalid" do
12
+ @user.should_not be_valid
13
+ end
14
+
15
+ it "has an error" do
16
+ @user.should have(1).errors_on(:partita_iva)
17
+ end
18
+
19
+ it "returns an empty error" do
20
+ @user.should_not be_valid
21
+ @user.errors[:partita_iva].should == [I18n.translate("activerecord.errors.partita_iva.empty")]
22
+ end
23
+
24
+ end
25
+
26
+
27
+ VALID_PARTITA_IVA_CODES.each do |code|
28
+ context "with the valid code #{code}" do
29
+ it "has no errors" do
30
+ @user.partita_iva = code
31
+ @user.should have(:no).errors_on(:partita_iva)
32
+ end
33
+ end
34
+ end
35
+
36
+ INVALID_PARTITA_IVA_CODES.each do |code|
37
+
38
+ context "with the invalid code #{code}" do
39
+
40
+ it "has an error" do
41
+ @user.partita_iva = code
42
+ @user.should have(1).error_on(:partita_iva)
43
+ end
44
+
45
+ it "returns an invalid_format error" do
46
+ @user.partita_iva = code
47
+ @user.should_not be_valid
48
+ @user.errors[:partita_iva].should == [I18n.translate("activerecord.errors.partita_iva.invalid_format")]
49
+ end
50
+
51
+
52
+ end
53
+ end
54
+
55
+ end
data/spec/schema.rb ADDED
@@ -0,0 +1,9 @@
1
+ ActiveRecord::Schema.define(:version => 0) do
2
+
3
+ create_table "users", :force => true do |t|
4
+ t.string "email"
5
+ t.string "codice_fiscale"
6
+ t.string "partita_iva"
7
+ end
8
+
9
+ end
@@ -0,0 +1,27 @@
1
+ require "italian_job"
2
+
3
+ Bundler.setup
4
+ require "rails/all"
5
+ Bundler.require(:default)
6
+
7
+ require "rspec/rails"
8
+
9
+ Dir[File.dirname(__FILE__) + "/support/**/*.rb"].each {|f| require f}
10
+
11
+ ActiveRecord::Base.establish_connection :adapter => "sqlite3", :database => ":memory:"
12
+ load "schema.rb"
13
+
14
+ I18n.load_path << File.dirname(__FILE__) + "/support/locale.yml"
15
+
16
+ RSpec.configure do |config|
17
+ config.before do
18
+ I18n.locale = :en
19
+
20
+ ActiveRecord::Base.descendants.each do |model|
21
+ model.delete_all
22
+ Object.class_eval { remove_const model.name if const_defined?(model.name) }
23
+ end
24
+
25
+ load File.dirname(__FILE__) + "/support/user.rb"
26
+ end
27
+ end
@@ -0,0 +1,5 @@
1
+ VALID_CODICE_FISCALE_CODES = %w{PTTLCU82P16F839E RSSMRA00A01F839E}
2
+ INVALID_CODICE_FISCALE_CODES = %w{PTTLCU82P16F839 MRA00A01F839E PTTLCU82P16F839A PtTcLU82p16F839e}
3
+
4
+ VALID_PARTITA_IVA_CODES = %w{06363391001 00950501007}
5
+ INVALID_PARTITA_IVA_CODES = %w{4894389 4348394934 06363391003}
@@ -0,0 +1,6 @@
1
+ en:
2
+ activerecord:
3
+ errors:
4
+ codice_fiscale:
5
+ empty: cannot be empty
6
+ invalid_format: is invalid
@@ -0,0 +1,8 @@
1
+ class User < ActiveRecord::Base
2
+
3
+ attr_accessible :codice_fiscale, :partita_iva, :email
4
+
5
+ validates :codice_fiscale, :codice_fiscale_format => true
6
+ validates :partita_iva, :partita_iva_format => true
7
+
8
+ end
metadata CHANGED
@@ -1,12 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: italian_job
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 0
8
- - 1
9
- version: 0.0.1
4
+ prerelease:
5
+ version: 0.0.2
10
6
  platform: ruby
11
7
  authors:
12
8
  - Luca Pette
@@ -14,11 +10,43 @@ autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
12
 
17
- date: 2011-03-22 00:00:00 +01:00
13
+ date: 2011-04-02 00:00:00 +02:00
18
14
  default_executable:
19
- dependencies: []
20
-
21
- description: " If you want to validate Italian tax Code ( Codice Fiscale) or Italian VAT (Partita IVA) then you need this gem."
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: sqlite3
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "0"
25
+ type: :development
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: rails
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: 3.0.0
36
+ type: :runtime
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: rspec-rails
40
+ prerelease: false
41
+ requirement: &id003 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 2.5.0
47
+ type: :development
48
+ version_requirements: *id003
49
+ description: If you want to validate Italian tax Code (Codice Fiscale) or Italian VAT (Partita IVA) then you need this gem.
22
50
  email:
23
51
  - lucapette@gmail.com
24
52
  executables: []
@@ -29,12 +57,23 @@ extra_rdoc_files: []
29
57
 
30
58
  files:
31
59
  - .gitignore
60
+ - .rspec
32
61
  - Gemfile
33
- - README
62
+ - README.textile
34
63
  - Rakefile
35
64
  - italian_job.gemspec
36
65
  - lib/italian_job.rb
66
+ - lib/italian_job/validators.rb
67
+ - lib/italian_job/validators/codice_fiscale_format.rb
68
+ - lib/italian_job/validators/partita_iva_format.rb
37
69
  - lib/italian_job/version.rb
70
+ - spec/italian_job/validators/codice_fiscale_format_spec.rb
71
+ - spec/italian_job/validators/partita_iva_format_spec.rb
72
+ - spec/schema.rb
73
+ - spec/spec_helper.rb
74
+ - spec/support/codes.rb
75
+ - spec/support/locale.yml
76
+ - spec/support/user.rb
38
77
  has_rdoc: true
39
78
  homepage: http://github.com/lucapette/italian_job
40
79
  licenses: []
@@ -49,21 +88,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
49
88
  requirements:
50
89
  - - ">="
51
90
  - !ruby/object:Gem::Version
52
- segments:
53
- - 0
54
91
  version: "0"
55
92
  required_rubygems_version: !ruby/object:Gem::Requirement
56
93
  none: false
57
94
  requirements:
58
95
  - - ">="
59
96
  - !ruby/object:Gem::Version
60
- segments:
61
- - 0
62
97
  version: "0"
63
98
  requirements: []
64
99
 
65
100
  rubyforge_project: italian_job
66
- rubygems_version: 1.3.7
101
+ rubygems_version: 1.6.2
67
102
  signing_key:
68
103
  specification_version: 3
69
104
  summary: ActiveRecord Validators for dealing with typical italian business problems
data/README DELETED
@@ -1 +0,0 @@
1
- Early stage development. Well, nothing to see yet.