mask_validator 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 +4 -0
- data/Gemfile +4 -0
- data/MIT-LICENSE +20 -0
- data/README.markdown +25 -0
- data/Rakefile +11 -0
- data/lib/mask_validator.rb +28 -0
- data/mask_validator.gemspec +23 -0
- data/test/mask_validator_test.rb +50 -0
- data/test/test_helper.rb +12 -0
- metadata +78 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009-2011 Gabriel Sobrinho
|
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.markdown
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# MaskValidator
|
2
|
+
|
3
|
+
This gem was inspired in the Sobrinho's gems to validate simple things inside of ActiveModel.
|
4
|
+
|
5
|
+
Use mask in inputs and validate then in the models.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
`gem "mask_validator"`
|
10
|
+
|
11
|
+
## Usage:
|
12
|
+
|
13
|
+
`validates :phone, mask: "(99) 9999-9999"`
|
14
|
+
|
15
|
+
`validates :acronym, mask: "***"`
|
16
|
+
|
17
|
+
* a - Represents an alpha character (A-Z,a-z)
|
18
|
+
* 9 - Represents a numeric character (0-9)
|
19
|
+
* * - Represents an alphanumeric character (A-Z,a-z,0-9)
|
20
|
+
|
21
|
+
For more information about masks in the form inputs check the jquery plugin [Masked input](http://digitalbush.com/projects/masked-input-plugin/)
|
22
|
+
|
23
|
+
## License
|
24
|
+
|
25
|
+
Copyright © 2011 Marcelo Cajueiro, released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
class MaskValidator < ActiveModel::EachValidator
|
2
|
+
|
3
|
+
#
|
4
|
+
# This validator use the characters 'a', '9' and '*'
|
5
|
+
# to validate the form of the content
|
6
|
+
#
|
7
|
+
# Example using in models:
|
8
|
+
# validates :phone, mask: "(99) 9999-9999"
|
9
|
+
# validates :acronym, mask: "***"
|
10
|
+
#
|
11
|
+
# Where:
|
12
|
+
# a - Represents an alpha character (A-Z,a-z)
|
13
|
+
# 9 - Represents a numeric character (0-9)
|
14
|
+
# * - Represents an alphanumeric character (A-Z,a-z,0-9)
|
15
|
+
#
|
16
|
+
# TODO: refactoring
|
17
|
+
def validate_each(record, attribute, value)
|
18
|
+
return true if value.nil? || value.empty?
|
19
|
+
|
20
|
+
definitions = { "9" => "[0-9]", "a" => "[a-zA-Z]", "*" => "[a-zA-Z0-9]" }
|
21
|
+
regex = /#{(options[:with].to_s.each_char.collect { |char| definitions[char] || "\\#{char}" }).join}/
|
22
|
+
|
23
|
+
match = value.match(regex)
|
24
|
+
unless match && match.to_s == value
|
25
|
+
record.errors.add(attribute, options[:message])
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "mask_validator"
|
6
|
+
s.version = "0.1"
|
7
|
+
s.platform = Gem::Platform::RUBY
|
8
|
+
s.authors = ["Marcelo Cajueiro"]
|
9
|
+
s.email = ["marcelocajueiro@gmail.com"]
|
10
|
+
s.homepage = "https://github.com/marcelocajueiro/mask_validator"
|
11
|
+
s.summary = %q{Input Mask validation for ActiveModel}
|
12
|
+
|
13
|
+
s.rubyforge_project = "mask_validator"
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split("\n")
|
16
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
17
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
18
|
+
s.require_paths = ["lib"]
|
19
|
+
|
20
|
+
s.add_dependency 'activemodel', ">= 3.0"
|
21
|
+
s.add_development_dependency 'rake', '>= 0.8.7'
|
22
|
+
|
23
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class MaskValidatorTest < Test::Unit::TestCase
|
4
|
+
def test_phones
|
5
|
+
create_asserts('phone')
|
6
|
+
|
7
|
+
assert_invalid '4852458787'
|
8
|
+
assert_invalid '48 9865 4879'
|
9
|
+
assert_invalid "(48)98956698"
|
10
|
+
assert_invalid "(48) 9874-45169"
|
11
|
+
assert_valid "(48) 9874-4569"
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_acronyms
|
15
|
+
create_asserts('acronym')
|
16
|
+
|
17
|
+
assert_invalid '1qw1'
|
18
|
+
assert_valid '1gd'
|
19
|
+
assert_valid '666'
|
20
|
+
assert_valid 'zzz'
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_alphanumerics
|
24
|
+
create_asserts('alphanumeric')
|
25
|
+
|
26
|
+
assert_invalid '999999'
|
27
|
+
assert_invalid 'QQQQQQ'
|
28
|
+
assert_invalid '666AAAA'
|
29
|
+
assert_invalid '666AAA'
|
30
|
+
assert_valid 'AAA666'
|
31
|
+
end
|
32
|
+
|
33
|
+
protected
|
34
|
+
|
35
|
+
def create_asserts(attr)
|
36
|
+
class_eval <<-RUBY
|
37
|
+
def assert_valid(value)
|
38
|
+
assert person(:#{attr} => value).valid?
|
39
|
+
end
|
40
|
+
|
41
|
+
def assert_invalid(value)
|
42
|
+
assert person(:#{attr} => value).invalid?
|
43
|
+
end
|
44
|
+
RUBY
|
45
|
+
end
|
46
|
+
|
47
|
+
def person(attributes = {})
|
48
|
+
Person.new(attributes)
|
49
|
+
end
|
50
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'active_model'
|
4
|
+
require 'mask_validator'
|
5
|
+
require 'ostruct'
|
6
|
+
|
7
|
+
class Person < OpenStruct
|
8
|
+
include ActiveModel::Validations
|
9
|
+
validates :phone, mask: "(99) 9999-9999"
|
10
|
+
validates :acronym, mask: "***"
|
11
|
+
validates :alphanumeric, mask: "aaa999"
|
12
|
+
end
|
metadata
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mask_validator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.1'
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Marcelo Cajueiro
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-09-07 00:00:00.000000000 -03:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: activemodel
|
17
|
+
requirement: &78039790 !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ! '>='
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '3.0'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: *78039790
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: rake
|
28
|
+
requirement: &78033190 !ruby/object:Gem::Requirement
|
29
|
+
none: false
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.8.7
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: *78033190
|
37
|
+
description:
|
38
|
+
email:
|
39
|
+
- marcelocajueiro@gmail.com
|
40
|
+
executables: []
|
41
|
+
extensions: []
|
42
|
+
extra_rdoc_files: []
|
43
|
+
files:
|
44
|
+
- .gitignore
|
45
|
+
- Gemfile
|
46
|
+
- MIT-LICENSE
|
47
|
+
- README.markdown
|
48
|
+
- Rakefile
|
49
|
+
- lib/mask_validator.rb
|
50
|
+
- mask_validator.gemspec
|
51
|
+
- test/mask_validator_test.rb
|
52
|
+
- test/test_helper.rb
|
53
|
+
has_rdoc: true
|
54
|
+
homepage: https://github.com/marcelocajueiro/mask_validator
|
55
|
+
licenses: []
|
56
|
+
post_install_message:
|
57
|
+
rdoc_options: []
|
58
|
+
require_paths:
|
59
|
+
- lib
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
|
+
none: false
|
68
|
+
requirements:
|
69
|
+
- - ! '>='
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
requirements: []
|
73
|
+
rubyforge_project: mask_validator
|
74
|
+
rubygems_version: 1.6.2
|
75
|
+
signing_key:
|
76
|
+
specification_version: 3
|
77
|
+
summary: Input Mask validation for ActiveModel
|
78
|
+
test_files: []
|