valideez 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/.rvmrc +2 -0
- data/Gemfile +4 -0
- data/LICENSE.md +7 -0
- data/README.md +19 -0
- data/Rakefile +1 -0
- data/lib/valideez/base.rb +33 -0
- data/lib/valideez/bastard.rb +10 -0
- data/lib/valideez/common.rb +12 -0
- data/lib/valideez/nip.rb +32 -0
- data/lib/valideez/pesel.rb +57 -0
- data/lib/valideez/pesel_validator.rb +18 -0
- data/lib/valideez/phone.rb +24 -0
- data/lib/valideez/version.rb +3 -0
- data/lib/valideez.rb +5 -0
- data/spec/bastard_spec.rb +7 -0
- data/spec/nip_spec.rb +9 -0
- data/spec/polish/pesel_spec.rb +29 -0
- data/spec/spec_helper.rb +6 -0
- data/valideez.gemspec +25 -0
- metadata +108 -0
data/.gitignore
ADDED
data/.rvmrc
ADDED
data/Gemfile
ADDED
data/LICENSE.md
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
Copyright (c) 2011 Zaiste! de Grengolada
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
4
|
+
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
6
|
+
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Valideez
|
2
|
+
========
|
3
|
+
|
4
|
+
Let's valideez lousy objects once and forever!
|
5
|
+
|
6
|
+
Instalation
|
7
|
+
-----------
|
8
|
+
|
9
|
+
gem install valideez
|
10
|
+
|
11
|
+
Usage
|
12
|
+
-----
|
13
|
+
|
14
|
+
License
|
15
|
+
-------
|
16
|
+
|
17
|
+
Released under the MIT License. See the [LICENSE][license] file for further details.
|
18
|
+
|
19
|
+
[license]: https://github.com/zaiste/valideez/blob/master/LICENSE.md
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Valideez
|
2
|
+
class Base
|
3
|
+
attr_reader :val
|
4
|
+
attr_accessor :validable
|
5
|
+
|
6
|
+
def initialize(val)
|
7
|
+
@val = val.to_s.gsub(/-/, '')
|
8
|
+
@validable = []
|
9
|
+
end
|
10
|
+
|
11
|
+
def valid?
|
12
|
+
validable.map { |var| send("validate_#{var.to_s}") if respond_to?("validate_#{var.to_s}", true) }.compact!.all?
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def assign(opts)
|
18
|
+
opts.each do |attr, val|
|
19
|
+
# tricky: don't create an instance variable if
|
20
|
+
# value is nil OR value is false
|
21
|
+
if val
|
22
|
+
instance_variable_set("@#{attr}", val)
|
23
|
+
# create read-only accessor
|
24
|
+
self.class.send(:define_method, attr.to_s) do
|
25
|
+
instance_variable_get("@" + attr.to_s)
|
26
|
+
end
|
27
|
+
|
28
|
+
validable << attr.to_s
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
data/lib/valideez/nip.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
module Valideez
|
2
|
+
class Nip < Valideez::Base
|
3
|
+
include Valideez::Common
|
4
|
+
|
5
|
+
def initialize(val, options = {})
|
6
|
+
super val
|
7
|
+
|
8
|
+
assign({
|
9
|
+
:sum_control => true,
|
10
|
+
:format => /\A\d{10}\Z/,
|
11
|
+
:length => 10,
|
12
|
+
:mask => [6, 5, 7, 2, 3, 4, 5, 6, 7],
|
13
|
+
:modulo => 11,
|
14
|
+
}.merge(options))
|
15
|
+
|
16
|
+
@arr = []
|
17
|
+
end
|
18
|
+
|
19
|
+
def validate_sum_control
|
20
|
+
mod = checksum % modulo
|
21
|
+
mod === @arr.shift
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def checksum
|
27
|
+
@arr = val.split("").collect(&:to_i)
|
28
|
+
mask.inject(0) {|sum, w| sum + w * @arr.shift}
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'date'
|
2
|
+
|
3
|
+
module Valideez
|
4
|
+
class Pesel < Valideez::Base
|
5
|
+
include Valideez::Common
|
6
|
+
|
7
|
+
def initialize(val, options = {})
|
8
|
+
super val
|
9
|
+
|
10
|
+
assign({
|
11
|
+
:sum_control => true,
|
12
|
+
:format => /\A\d{11}\Z/,
|
13
|
+
:length => 11,
|
14
|
+
:mask => [1, 3, 7, 9, 1, 3, 7, 9, 1, 3],
|
15
|
+
:modulo => 10,
|
16
|
+
:age => 0,
|
17
|
+
}.merge(options))
|
18
|
+
|
19
|
+
@arr = []
|
20
|
+
end
|
21
|
+
|
22
|
+
def validate_sum_control
|
23
|
+
mod = checksum % modulo
|
24
|
+
mod = modulo - mod
|
25
|
+
mod = 0 if mod == modulo
|
26
|
+
|
27
|
+
mod === @arr.shift
|
28
|
+
end
|
29
|
+
|
30
|
+
def validate_age
|
31
|
+
now = Time.now
|
32
|
+
y = (now.strftime("%Y").to_i - age).to_s
|
33
|
+
m = now.strftime("%m")
|
34
|
+
d = now.strftime("%d")
|
35
|
+
|
36
|
+
age == 0 || convert_to_date(val[0..5]) <= Date.parse([y, m, d].join("-"))
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def checksum
|
42
|
+
@arr = val.split("").collect(&:to_i)
|
43
|
+
mask.inject(0) {|sum, w| sum + w * @arr.shift}
|
44
|
+
end
|
45
|
+
|
46
|
+
def convert_to_date(sample)
|
47
|
+
y, m, d = sample.scan(/../)
|
48
|
+
|
49
|
+
year = %w(19 20 21 22)[m[0].to_i / 2].to_s + y
|
50
|
+
month = (m.to_i - m[0].to_i * 10).to_s
|
51
|
+
|
52
|
+
Date.parse [year, month, d].join("-")
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'active_model'
|
2
|
+
|
3
|
+
class PeselValidator < ActiveModel::EachValidator
|
4
|
+
def validate_each(record, attribute, value)
|
5
|
+
@message = options[:message] || "invalid format"
|
6
|
+
record.errors.add(attribute, @message) unless Valideez::Pesel.new(value).valid?
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
module ActiveModel
|
11
|
+
module Validations
|
12
|
+
module HelperMethods
|
13
|
+
def validates_pesel_of(*attr_names)
|
14
|
+
validates_with PeselValidator, _merge_attributes(attr_names)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Valideez
|
2
|
+
class Phone < Valideez::Base
|
3
|
+
include Valideez::Common
|
4
|
+
|
5
|
+
def initialize(val, options = {})
|
6
|
+
super val
|
7
|
+
|
8
|
+
assign({
|
9
|
+
:mobile => false,
|
10
|
+
:format => /\A\d{9}\Z/,
|
11
|
+
}.merge(options))
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def validate_mobile
|
17
|
+
%w(500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
|
18
|
+
530 531 532 533 534 535 600 601 602 603 604 605 606 607 608 609 660 661 662 663
|
19
|
+
664 665 666 667 668 669 690 691 692 693 694 695 696 697 698 699 720 721 722 723
|
20
|
+
724 725 726 727 728 729 780 781 782 783 784 785 786 787 788 789 790 791 792 793
|
21
|
+
794 795 796 797 798 799 881 882 882 883 883 884 885 886 887 888 889).include?(@val[0..2])
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/lib/valideez.rb
ADDED
data/spec/nip_spec.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Valideez::Pesel do
|
4
|
+
it "should be valid"do
|
5
|
+
%w[49040501580 93031908746].each do |n|
|
6
|
+
Valideez::Pesel.new(n).should be_valid
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should be invalid" do
|
11
|
+
%w[59040501580 870132114508].each do |n|
|
12
|
+
Valideez::Pesel.new(n).should_not be_valid
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should be valid with minimum age defined" do
|
17
|
+
%w[49040501580].each do |n|
|
18
|
+
Valideez::Pesel.new(n, :age => 18).should be_valid
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should be invalid when pesel is valid but the person is too young" do
|
24
|
+
%w[05220471553].each do |n|
|
25
|
+
Valideez::Pesel.new(n).should be_valid
|
26
|
+
Valideez::Pesel.new(n, :age => 18).should_not be_valid
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
data/spec/spec_helper.rb
ADDED
data/valideez.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "valideez/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "valideez"
|
7
|
+
s.version = Valideez::VERSION
|
8
|
+
s.authors = ["zaiste!"]
|
9
|
+
s.email = ["oh@zaiste.net"]
|
10
|
+
s.homepage = "http://dev.zaiste.net/gem/valideez"
|
11
|
+
s.summary = %q{Let's valideez lousy objects once and forever}
|
12
|
+
s.description = %q{Your favourite validations with Ruby}
|
13
|
+
|
14
|
+
s.rubyforge_project = "valideez"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
# specify any dependencies here; for example:
|
22
|
+
s.add_development_dependency "rspec"
|
23
|
+
s.add_development_dependency "rake"
|
24
|
+
s.add_development_dependency 'activemodel','>= 3.0.0'
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: valideez
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- zaiste!
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-08-15 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: &70327217773920 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70327217773920
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rake
|
27
|
+
requirement: &70327217773480 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70327217773480
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: activemodel
|
38
|
+
requirement: &70327217772940 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 3.0.0
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70327217772940
|
47
|
+
description: Your favourite validations with Ruby
|
48
|
+
email:
|
49
|
+
- oh@zaiste.net
|
50
|
+
executables: []
|
51
|
+
extensions: []
|
52
|
+
extra_rdoc_files: []
|
53
|
+
files:
|
54
|
+
- .gitignore
|
55
|
+
- .rvmrc
|
56
|
+
- Gemfile
|
57
|
+
- LICENSE.md
|
58
|
+
- README.md
|
59
|
+
- Rakefile
|
60
|
+
- lib/valideez.rb
|
61
|
+
- lib/valideez/base.rb
|
62
|
+
- lib/valideez/bastard.rb
|
63
|
+
- lib/valideez/common.rb
|
64
|
+
- lib/valideez/nip.rb
|
65
|
+
- lib/valideez/pesel.rb
|
66
|
+
- lib/valideez/pesel_validator.rb
|
67
|
+
- lib/valideez/phone.rb
|
68
|
+
- lib/valideez/version.rb
|
69
|
+
- spec/bastard_spec.rb
|
70
|
+
- spec/nip_spec.rb
|
71
|
+
- spec/polish/pesel_spec.rb
|
72
|
+
- spec/spec_helper.rb
|
73
|
+
- valideez.gemspec
|
74
|
+
homepage: http://dev.zaiste.net/gem/valideez
|
75
|
+
licenses: []
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options: []
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
segments:
|
87
|
+
- 0
|
88
|
+
hash: -2149050442087088595
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
none: false
|
91
|
+
requirements:
|
92
|
+
- - ! '>='
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
segments:
|
96
|
+
- 0
|
97
|
+
hash: -2149050442087088595
|
98
|
+
requirements: []
|
99
|
+
rubyforge_project: valideez
|
100
|
+
rubygems_version: 1.8.6
|
101
|
+
signing_key:
|
102
|
+
specification_version: 3
|
103
|
+
summary: Let's valideez lousy objects once and forever
|
104
|
+
test_files:
|
105
|
+
- spec/bastard_spec.rb
|
106
|
+
- spec/nip_spec.rb
|
107
|
+
- spec/polish/pesel_spec.rb
|
108
|
+
- spec/spec_helper.rb
|