valideez 0.0.5 → 0.0.6
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/lib/valideez/phone_validator.rb +21 -0
- data/lib/valideez/version.rb +1 -1
- data/spec/phone_spec.rb +29 -0
- data/spec/phone_validator_spec.rb +48 -0
- metadata +15 -10
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'active_model'
|
2
|
+
|
3
|
+
class PhoneValidator < ActiveModel::EachValidator
|
4
|
+
|
5
|
+
def validate_each(record, attribute, value)
|
6
|
+
@message = options[:message] || "invalid format"
|
7
|
+
opts = options.reject{ |k| k == :message }
|
8
|
+
|
9
|
+
record.errors.add(attribute, @message) unless Valideez::Phone.new(value, opts).valid?
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
module ActiveModel
|
14
|
+
module Validations
|
15
|
+
module HelperMethods
|
16
|
+
def validates_phone_of(*attr_names)
|
17
|
+
validates_with PhoneValidator, _merge_attributes(attr_names)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/lib/valideez/version.rb
CHANGED
data/spec/phone_spec.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Valideez::Phone do
|
4
|
+
it "should be valid"do
|
5
|
+
%w[490405015 930319087].each do |n|
|
6
|
+
Valideez::Phone.new(n).should be_valid
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should be invalid" do
|
11
|
+
%w[59040501580 870132114508 AABB].each do |n|
|
12
|
+
Valideez::Phone.new(n).should_not be_valid
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should be valid mobile" do
|
17
|
+
%w[503161754].each do |n|
|
18
|
+
Valideez::Phone.new(n, mobile: true).should be_valid
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should be valid phone but invalid mobile" do
|
24
|
+
%w[922123445].each do |n|
|
25
|
+
Valideez::Phone.new(n).should be_valid
|
26
|
+
Valideez::Phone.new(n, mobile: true).should_not be_valid
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'rspec'
|
3
|
+
require 'active_model'
|
4
|
+
|
5
|
+
class PhoneBastard
|
6
|
+
include ActiveModel::Validations
|
7
|
+
|
8
|
+
attr_accessor :phone
|
9
|
+
validates :phone, :presence => true, :phone => true
|
10
|
+
end
|
11
|
+
|
12
|
+
class MobilePhoneBastard
|
13
|
+
include ActiveModel::Validations
|
14
|
+
|
15
|
+
attr_accessor :phone
|
16
|
+
validates :phone, :presence => true, :phone => { mobile: true }
|
17
|
+
end
|
18
|
+
|
19
|
+
describe PhoneValidator do
|
20
|
+
before :each do
|
21
|
+
@phone= PhoneBastard.new
|
22
|
+
@mobile_phone = MobilePhoneBastard.new
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should be valid" do
|
26
|
+
@phone.should_not be_valid
|
27
|
+
@phone.phone = '680419796'
|
28
|
+
@phone.should be_valid
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should be invalid (too long)" do
|
32
|
+
@phone.should_not be_valid
|
33
|
+
@phone.phone = '69041979693'
|
34
|
+
@phone.should_not be_valid
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should be valid mobile" do
|
38
|
+
@mobile_phone.should_not be_valid
|
39
|
+
@mobile_phone.phone = '503419796'
|
40
|
+
@mobile_phone.should be_valid
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should be invalid with valid phone but not mobile " do
|
44
|
+
@mobile_phone.should_not be_valid
|
45
|
+
@mobile_phone.phone = '990302513'
|
46
|
+
@mobile_phone.should_not be_valid
|
47
|
+
end
|
48
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: valideez
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-08-
|
12
|
+
date: 2011-08-16 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &70223006009980 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70223006009980
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rake
|
27
|
-
requirement: &
|
27
|
+
requirement: &70223006009560 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70223006009560
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: activemodel
|
38
|
-
requirement: &
|
38
|
+
requirement: &70223006009020 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,7 +43,7 @@ dependencies:
|
|
43
43
|
version: 3.0.0
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70223006009020
|
47
47
|
description: Your favourite validations with Ruby
|
48
48
|
email:
|
49
49
|
- oh@zaiste.net
|
@@ -66,12 +66,15 @@ files:
|
|
66
66
|
- lib/valideez/pesel.rb
|
67
67
|
- lib/valideez/pesel_validator.rb
|
68
68
|
- lib/valideez/phone.rb
|
69
|
+
- lib/valideez/phone_validator.rb
|
69
70
|
- lib/valideez/version.rb
|
70
71
|
- spec/bastard_spec.rb
|
71
72
|
- spec/nip_spec.rb
|
72
73
|
- spec/nip_validator_spec.rb
|
73
74
|
- spec/pesel_spec.rb
|
74
75
|
- spec/pesel_validator_spec.rb
|
76
|
+
- spec/phone_spec.rb
|
77
|
+
- spec/phone_validator_spec.rb
|
75
78
|
- spec/spec_helper.rb
|
76
79
|
- valideez.gemspec
|
77
80
|
homepage: http://dev.zaiste.net/gem/valideez
|
@@ -88,7 +91,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
88
91
|
version: '0'
|
89
92
|
segments:
|
90
93
|
- 0
|
91
|
-
hash: -
|
94
|
+
hash: -1982026989948108565
|
92
95
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
93
96
|
none: false
|
94
97
|
requirements:
|
@@ -97,7 +100,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
97
100
|
version: '0'
|
98
101
|
segments:
|
99
102
|
- 0
|
100
|
-
hash: -
|
103
|
+
hash: -1982026989948108565
|
101
104
|
requirements: []
|
102
105
|
rubyforge_project: valideez
|
103
106
|
rubygems_version: 1.8.6
|
@@ -110,4 +113,6 @@ test_files:
|
|
110
113
|
- spec/nip_validator_spec.rb
|
111
114
|
- spec/pesel_spec.rb
|
112
115
|
- spec/pesel_validator_spec.rb
|
116
|
+
- spec/phone_spec.rb
|
117
|
+
- spec/phone_validator_spec.rb
|
113
118
|
- spec/spec_helper.rb
|